Process is running?

Process is running?

Postby Ugo » Tue Nov 24, 2009 5:18 pm

Dear friends,

I need to found if a process is running.

I test GetTasks and GetWindow without result.

The process is visible in process folder of task manager and, for example, with pslist.exe of sysinternals freeware utility.

pslist -e <ProcessName> that return:

This solution is not very professional!

Any suggestion is appreciate.
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Postby Antonio Linares » Tue Nov 24, 2009 6:54 pm

Ugo,

Are you able to find the window of the task using ?

hWnd := FindWindow( 0, cWindowTitle )
MsgInfo( hWnd )

In case that the task does not has a window, then you have to find its process id using EnumProcesses():
http://msdn.microsoft.com/en-us/library/ms682629(VS.85).aspx
and then, once you have the process id call to TerminateProcess():
http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Process is running?

Postby Ugo » Wed Nov 25, 2009 7:42 am

Antonio,
Antonio Linares wrote:Are you able to find the window of the task using ?

The process is in background!

Antonio Linares wrote:hWnd := FindWindow( 0, cWindowTitle )
MsgInfo( hWnd )

I tested without result!
I tested also with GetTasks and GetWindow but nothing.

Antonio Linares wrote:In case that the task does not has a window, then you have to find its process id using EnumProcesses():
http://msdn.microsoft.com/en-us/library/ms682629(VS.85).aspx
and then, once you have the process id call to TerminateProcess():
http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx

This is the case.
I need to find when the process is terminated.
My function test continuosly if is running or there are other method?
Where I found documentation of EnumProcesses() for xHarbour and Fivewin?
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Postby Patrizio » Wed Nov 25, 2009 1:41 pm

Hi Ugo, this

Code: Select all  Expand view

HB_FUNC( GETPROCESSLISTARRAY )
{
   DWORD aProcesses[1024];
   DWORD nBytes;
   DWORD nProcesses;
   unsigned int i;
   if ( EnumProcesses( aProcesses, sizeof(aProcesses), &nBytes ) )
   {
      nProcesses = nBytes / sizeof(DWORD);
      hb_reta( nProcesses - 1 );
      for ( i = 0; i < nProcesses; i++ )
      {
         if ( aProcesses[i] != 0 )
         {
            hb_stornl(aProcesses[i],-1,i);
         }
      }
   }
   else
   {
      hb_reta( 0 );
   }
}
 


return an array with all process id and this

Code: Select all  Expand view
HB_FUNC( GETPROCESSNAME )
{
   DWORD cbNeeded;
   HANDLE hProcess;
   HMODULE hMod;
   TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
   hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE, (DWORD) hb_parnl(1) );
   if ( hProcess != NULL )
   {
      if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
      {
         GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
      }
   }
   CloseHandle(hProcess);
   hb_retc(szProcessName);
}
 


return the name of the id process if your user can access.
Patrizio
 
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy

Re: Process is running?

Postby Ugo » Wed Nov 25, 2009 4:17 pm

Hi Patrizio,
many thanks for your help.

Is possible to know the id from the ProcessName?
F.E. GetProcessId( cProcessName )
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Postby Patrizio » Wed Nov 25, 2009 4:35 pm

I don't see an api for find the process by name, i resolve a similar problem with

Code: Select all  Expand view
     
     FUNC MyFunction(cProcessName)
      LOCAL aProcess, hHandle
      aProcess    := GetProcessListArray()
      FOR EACH nHandle IN aProcess
         IF AllTrim(Upper(GetProcessName(nHandle))) == AllTrim(Upper(cProcessName))
           // ... insert here what you want to do with the process handle
         ENDIF
      NEXT
 


You can have more processes with the same name, in example svchost.exe.
Patrizio
 
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy

Re: Process is running?

Postby Antonio Linares » Wed Nov 25, 2009 7:40 pm

Ugo,

Does the process uses a window ?

Even if it has no caption, we can find it using FindWindow( cClassName, 0 ) if we know what Windows classname uses to register its window

http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Process is running?

Postby Ugo » Thu Nov 26, 2009 10:42 am

Antonio,
Antonio Linares wrote:Does the process uses a window ?


No, it does not use a window! :-(
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Postby Ugo » Thu Nov 26, 2009 10:46 am

Patrizio,
Patrizio wrote:I don't see an api for find the process by name, i resolve a similar problem with

Code: Select all  Expand view
     
     FUNC MyFunction(cProcessName)
      LOCAL aProcess, hHandle
      aProcess    := GetProcessListArray()
      FOR EACH nHandle IN aProcess
         IF AllTrim(Upper(GetProcessName(nHandle))) == AllTrim(Upper(cProcessName))
           // ... insert here what you want to do with the process handle
         ENDIF
      NEXT
 

I also thought about this solution. ;-)

Patrizio wrote:You can have more processes with the same name, in example svchost.exe.

Ok, it is right!

Many thanks for your help.
Ciao, best regards,
Ugo
User avatar
Ugo
 
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Postby Randal » Wed Jun 02, 2010 3:33 pm

I'm having a problem compiling these code samples. I'm getting 'unresolved external symbol' on _EnumProcesses, _EnumProcessModules, _GetModuleBaseName.

Using xHarbour/xBuilder. Do I need to define these functions with DLL command? If so, does anyone have an example of the correct DLL definition?

Thanks,
Randal
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Re: Process is running?

Postby Randal » Thu Jun 03, 2010 2:51 pm

Never mind. I resolved the problem by linking psapi.lib.

Randal
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 71 guests