Click to See Complete Forum and Search --> : GetProcessStatus ? How can i do that


tuxp3
March 9th, 2003, 09:44 AM
I wanna get the status of a process to sewe if it is running or not.
i got some sample source code from here however it was very out of date and i got lots of errors when testing it. i believe it was b/c of it only working on winNT/95 ...
i wanna be able to see if a proces is still runnin by doing osomethign like GetProcessStatus("process name")
and haveing it return true or false
any suggestions are helpful.
Thankz
TuX

kuphryn
March 9th, 2003, 11:58 AM
Under Win32, one solution is OpenProcess().

Kuphryn

Mick
March 9th, 2003, 12:06 PM
Well what OS are you using? Look at the PSAPI Functions...

PaulWendt
March 9th, 2003, 12:26 PM
You can't really just use "process name" because more than one
process of the same name can be run. Having said that, you can
probably muck something together. There's a function called
GetExitCodeProcess() that will tell you if a process is running or
not [given its handle]. Here's a link to all of the WIN32 process
and thread functions; they ought to be able to help you:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/process_and_thread_functions.asp?frame=true

tuxp3
March 9th, 2003, 02:22 PM
the progrma im using as a process was ported from linux ot windows so i doint know if the geterrorcodeprocess will work.. however im intersted in open process
thankz for help..

PaulWendt
March 9th, 2003, 03:30 PM
The functions I gave you are WIN32 API functions; as long as you
are running on WIN32, it doesn't matter where the code was
originally developed. The only thing you need to worry about with
GetExitCodeProcess() is that the process doesn't return
PROCESS_STILL_ACTIVE as an error code.

--Paul

Mick
March 9th, 2003, 03:42 PM
Though it's still not being clear on whether they have the handle to the process or not...eg: did they create the process, or do they need to enum the processes...or maybe I'm just reading it wrong :)

PaulWendt
March 9th, 2003, 07:12 PM
EnumProcesses() returns an array of PIDs. OpenProcess() will
yield a process handle, given its PID. Thus, they don't need to
have created the process in order to query its termination state.

--Paul

Mick
March 9th, 2003, 08:15 PM
Thus my question on whether they created the process or not which directly leads to if they need to enumprocesses or not...guess we pretty much covered the bases though... :) :) :)

tuxp3
March 10th, 2003, 02:12 PM
i created the process it is a 16-bit program thats been ported form linux via cygwin1.dll if that makes a difference.
thankz
TuX

PaulWendt
March 10th, 2003, 02:55 PM
Did you try the functions like I suggested? Unless you try those,
I don't have any other advice. Before you reply by saying "they
didn't work" be sure to give us your sample code and pertinent
error codes that were returned so that we can better assist you.

tuxp3
March 10th, 2003, 08:33 PM
ive been trying to use EnumProcess() and i have been having some luck


BOOL runnin;
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded );
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i++ ) {
char szProcessName[MAX_PATH] = "unknown";
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, aProcesses[i] );

// Get the process name.
if (NULL != hProcess ) {
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) ) {
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
}
if( szProcessName == "myprogram.exe" ) {
runnin = true;
printf("fhshsh\n"); //lemme know if it get here
}


printf( "%s (Process ID: %u)\n", szProcessName, aProcesses[i] ); // dispay process info

CloseHandle( hProcess );
}
i can see the process myprogram.exe running but the program doesnt pick it up... it tells me on "dispay process info" except on the line before if( szProcessName == "myprogram.exe" ) {
it doesnt work.. maybe im doing something wrong...

btw i havent had any major probelms compiling it just stupid stuff..

PaulWendt
March 11th, 2003, 06:51 AM
What do you mean "It doesn't pick it up" ? What is "it"? Which
function isn't working for you? Is the function returning an error
message?

--Paul

Mick
March 11th, 2003, 07:52 AM
Stop and take a moment to analyze your code :) Stop overwriting szProcessName with unknown, and also try doing:

if( strcmp(szProcessName,"myprog.exe") == 0 )
{

}

Maybe it just looks like noodles cause of the forum but if you still insist on if () { over
if ()
{
}

then have fun reading your own code down the line.