Re: test if a program is running
- Posted by GeorgeWalters Jul 13, 2009
- 1358 views
Sorry, rather stupid to not mention the environment. It's windows and I use win32lib. So what is the win32 api call that I should take a look at?
sample code tested on windows xp pro sp3 using euphoria 3.1 exwc.exe
-- how to get all running processes names without warning include dll.e include machine.e include wildcard.e constant kernel32=open_dll("kernel32.dll") constant psapi=open_dll("psapi.dll") constant iGetLastError=define_c_func(kernel32,"GetLastError",{},C_INT) constant iEnumProcesses=define_c_func(psapi,"EnumProcesses",{C_UINT,C_UINT,C_UINT},C_INT) constant iGetProcessImageFileName=define_c_func(psapi,"GetProcessImageFileNameA",{C_POINTER,C_POINTER,C_UINT},C_INT) constant iOpenProcess=define_c_func(kernel32,"OpenProcess",{C_UINT,C_INT,C_UINT},C_POINTER) constant PROCESS_QUERY_LIMITED_INFORMATION=#1000 constant PROCESS_QUERY_INFORMATION=#400 constant BUFFER_SIZE=4096 global function GetAllProcessesNames() object fnVal atom pBuffer, pCount, pHandle integer bCount, try, p sequence pids, names pCount = allocate(4) -- first get processes ids -- to ensure we get all processes ids we try until count is -- smaller than buffer size. try=1 bCount=BUFFER_SIZE while bCount=BUFFER_SIZE do pBuffer = allocate(try*BUFFER_SIZE) poke4(pCount,try*BUFFER_SIZE) fnVal = c_func(iEnumProcesses,{pBuffer,BUFFER_SIZE,pCount}) if not fnVal then free(pBuffer) free(pCount) return -1 end if bCount = peek4u(pCount) if bCount = try*BUFFER_SIZE then free(pBuffer) try += 1 end if end while free(pCount) pids = peek4u({pBuffer,bCount/4}) free(pBuffer) -- now open each process to get an handle to and query for is name pBuffer=allocate(BUFFER_SIZE) names={} for i = 1 to length(pids) do pHandle=c_func(iOpenProcess,{PROCESS_QUERY_INFORMATION,0,pids[i]}) if pHandle then -- query process name fnVal = c_func(iGetProcessImageFileName,{pHandle,pBuffer,BUFFER_SIZE}) if fnVal then names &={peek({pBuffer,fnVal})} p = find(0,names[$]) if p then names[$] = names[$][1..p-1] end if end if end if end for free(pBuffer) free(pCount) return names end function function name_only(sequence fqn) integer p p = find('\\',reverse(fqn)) if p then return fqn[length(fqn)-p+2..$] else return fqn end if end function object list list= GetAllProcessesNames() if sequence(list) then for i = 1 to length(list) do if length(list[i]) then puts(1,name_only(list[i])&"\n") end if end for end if
Jacques
Thanks, I'll give this a try.
George
forgot something to the sample code, you should add the following lines.
-- add this binding constant iCloseHandle=define_c_proc(kernel32,"CloseHandle",{C_POINTER}) -- add this line after the call to GetProcessImageFileName c_proc(iCloseHandle,{pHandle})
Jacques
I put this code in a program usine EU 2.4 (fixing the '$') and win32lib 59.1 and I get a machine level exception every time. What did this break??
runningProcesses = getList() if find("XCharge.exe", runningProcesses) then tmp = message_box("Xcharge credit card process already running","",0) return end if
It crashes at the message_box statment
A machine-level exception occurred during execution of this statement text = {88'X',99'c',104'h',97'a',114'r',103'g',101'e',32' ',99'c',114'r', 101'e',100'd',105'i',116't',32' ',99'c',97'a',114'r',100'd',32' ',112'p', 114'r',111'o',99'c',101'e',115's',115's',32' ',97'a',108'l',114'r',101'e', 97'a',100'd',121'y',32' ',114'r',117'u',110'n',110'n',105'i',110'n',103'g'} title = {} style = 0 or_style = 0 text_ptr = 16700528 title_ptr = 16691096 ret = <no value>
... called from C:\AcuTrack\source\cse010.exw:2518 in procedure processInvoice() tmp1 = <no value> tmp2 = <no value> fileNbr = <no value> msg = <no value> xchargeArgument = <no value> tranType = <no value>