Re: test if a program is running

new topic     » goto parent     » topic index » view thread      » older message » newer message
GeorgeWalters said...
jacques_desch said...
GeorgeWalters said...

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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu