Process is running?

Post Reply
User avatar
Ugo
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Process is running?

Post by Ugo »

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
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Process is running?

Post by Antonio Linares »

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 ... S.85).aspx
and then, once you have the process id call to TerminateProcess():
http://msdn.microsoft.com/en-us/library ... S.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Ugo
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Post by Ugo »

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 ... S.85).aspx
and then, once you have the process id call to TerminateProcess():
http://msdn.microsoft.com/en-us/library ... S.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
Patrizio
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy
Contact:

Re: Process is running?

Post by Patrizio »

Hi Ugo, this

Code: Select all

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

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.
User avatar
Ugo
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Post by Ugo »

Hi Patrizio,
many thanks for your help.

Is possible to know the id from the ProcessName?
F.E. GetProcessId( cProcessName )
Ciao, best regards,
Ugo
Patrizio
Posts: 90
Joined: Wed Nov 07, 2007 8:56 am
Location: Italy
Contact:

Re: Process is running?

Post by Patrizio »

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

Code: Select all

      
     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.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Process is running?

Post by Antonio Linares »

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 ... S.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Ugo
Posts: 283
Joined: Sat Oct 15, 2005 6:40 am
Location: Turin, Italy

Re: Process is running?

Post by Ugo »

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?

Post by Ugo »

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

Code: Select all

      
     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
Randal
Posts: 250
Joined: Mon Oct 24, 2005 8:04 pm

Re: Process is running?

Post by Randal »

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: 250
Joined: Mon Oct 24, 2005 8:04 pm

Re: Process is running?

Post by Randal »

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

Randal
Post Reply