FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Process is running?
Posts: 283
Joined: Sat Oct 15, 2005 06:40 AM
Process is running?
Posted: Tue Nov 24, 2009 05: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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Process is running?
Posted: Tue Nov 24, 2009 06: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
Posts: 283
Joined: Sat Oct 15, 2005 06:40 AM
Re: Process is running?
Posted: Wed Nov 25, 2009 07: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
Posts: 90
Joined: Wed Nov 07, 2007 08:56 AM
Re: Process is running?
Posted: Wed Nov 25, 2009 01:41 PM
Hi Ugo, this

Code (fw): Select all Collapse
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 (fw): Select all Collapse
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.
Posts: 283
Joined: Sat Oct 15, 2005 06:40 AM
Re: Process is running?
Posted: Wed Nov 25, 2009 04: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
Posts: 90
Joined: Wed Nov 07, 2007 08:56 AM
Re: Process is running?
Posted: Wed Nov 25, 2009 04:35 PM
I don't see an api for find the process by name, i resolve a similar problem with

Code (fw): Select all Collapse
      
     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.
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Process is running?
Posted: Wed Nov 25, 2009 07: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
Posts: 283
Joined: Sat Oct 15, 2005 06:40 AM
Re: Process is running?
Posted: 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
Posts: 283
Joined: Sat Oct 15, 2005 06:40 AM
Re: Process is running?
Posted: 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 (fw): Select all Collapse
      
     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
Posts: 284
Joined: Mon Oct 24, 2005 08:04 PM
Re: Process is running?
Posted: Wed Jun 02, 2010 03: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

Posts: 284
Joined: Mon Oct 24, 2005 08:04 PM
Re: Process is running?
Posted: Thu Jun 03, 2010 02:51 PM

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

Randal

Continue the discussion