Page 1 of 1
Process is running?
Posted: Tue Nov 24, 2009 5:18 pm
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.
Re: Process is running?
Posted: Tue Nov 24, 2009 6:54 pm
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
Re: Process is running?
Posted: Wed Nov 25, 2009 7:42 am
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.
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?
Re: Process is running?
Posted: Wed Nov 25, 2009 1:41 pm
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.
Re: Process is running?
Posted: Wed Nov 25, 2009 4:17 pm
by Ugo
Hi Patrizio,
many thanks for your help.
Is possible to know the id from the ProcessName?
F.E. GetProcessId( cProcessName )
Re: Process is running?
Posted: Wed Nov 25, 2009 4:35 pm
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.
Re: Process is running?
Posted: Wed Nov 25, 2009 7:40 pm
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
Re: Process is running?
Posted: Thu Nov 26, 2009 10:42 am
by Ugo
Antonio,
Antonio Linares wrote:Does the process uses a window ?
No, it does not use a window!
Re: Process is running?
Posted: Thu Nov 26, 2009 10:46 am
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.
Re: Process is running?
Posted: Wed Jun 02, 2010 3:33 pm
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
Re: Process is running?
Posted: Thu Jun 03, 2010 2:51 pm
by Randal
Never mind. I resolved the problem by linking psapi.lib.
Randal