Detecting a process

Started by
15 comments, last by doctorsixstring 21 years, 9 months ago
GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode) returns STILL_ACTIVE if the process is still running.

bool IsProcRunning(HANDLE hProcess){	DWORD dwExitCode;	GetExitCodeProcess(hProcess, &dwExitCode);	return (STILL_ACTIVE == dwExitCode);} 


Regards Mats
Advertisement
quote:Original post by doctorsixstring
So what am I doing wrong?

According to the fine print at MSDN:
quote:
You must pass at least one argument, arg0 or argv[0], to the spawned process. By convention, this argument is the name of the program as you would type it on the command line. A different value does not produce an error.


Try passing the empty string "" instead of NULL and see if that works.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
If you know the number of arguments you''re going to be passing to the spawned proc (looks like 0 in this case), I would recommend using _spawnlp:

int result = _spawnlp(_P_WAIT, "setup.exe", "setup.exe", NULL);if (result != 0)    // handle errors... 


However, if you really want to use _spawnvp it would look something like this:

char* args[2];args[0] = "setup.exe";args[1] = NULL;int result = _spawnvp( _P_WAIT, args[0], args); 



Thanks for all the replies, so far. I have been playing around with the _spawn functions and CreateProcess. Here is the code I have been using for each:

//////////
_spawn**()
//////////

  _spawnlp(_P_WAIT, strSetupDir, strSetupDir, NULL);  


//////////
CreateProcess()
//////////

  STARTUPINFO si;PROCESS_INFORMATION pi;memset(&si, 0, sizeof(STARTUPINFO));si.cb =	sizeof(STARTUPINFO); CreateProcess(strFilenameWithPath,NULL,NULL,NULL,FALSE,0,NULL, NULL, &si, π);DWORD dwExitCode = STILL_ACTIVE;while(dwExitCode == STILL_ACTIVE)	GetExitCodeProcess( pi.hProcess, &dwExitCode );  


Both methods of launching the new program seem to work at first, but my program only seems to wait until the new program initializes before it resumes.

I should probably go into more detail about the program I am launching. My app is basically a simple launcher to choose between multiple setup programs. Depending upon the user''s selection, the appropriate Setup.exe file is launched. All of the setup files were created with InstallShield. I was thinking that maybe an InstallShield app launches another file, thus causing my original program to think that the setup is complete.

I hope I explained this well enough...
-Mike

It seems like InstallShield setup programs are actually multiple executables, with the primary file "setup.exe" launching another setup program. In this case, my program is correct when it sees that "setup.exe" has quit. Is there any way to track processes created by processes that my program creates? I don''t have much hope, but if anyone has a solution, I would be very happy.

-Mike
I can''t speak much to the spawn code. Here''s my take on the CreateProcess code.


  DWORD ExecuteCommand(char *strFilenameWithPath, int cmdShow){ int processStarted; DWORD dwExitCode = STILL_ACTIVE;       STARTUPINFO si; PROCESS_INFORMATION procinfo; // changed from pi due to html  formatting memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);  si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = cmdShow; processStarted = CreateProcess(strFilenameWithPath,NULL,NULL,NULL,FALSE,0,NULL, NULL, &si, &procinfo); if ( processStarted == 0 ) {  // error! }// while(dwExitCode == STILL_ACTIVE) // this while loop is not the way to do it WaitForSingleObject(procinfo.hProcess, INFINITE); GetExitCodeProcess( procinfo.hProcess, &dwExitCode ); return dwExitCode;}  

quote:Original post by doctorsixstring
I was thinking that maybe an InstallShield app launches another file, thus causing my original program to think that the setup is complete.


That might well be the case. Head over to sysinternals.com and grab a copy of ProcExplorer, it will show you if the InstallShield spawns any children or not.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
quote:Original post by doctorsixstring
It seems like InstallShield setup programs are actually multiple executables, with the primary file "setup.exe" launching another setup program. In this case, my program is correct when it sees that "setup.exe" has quit. Is there any way to track processes created by processes that my program creates? I don''t have much hope, but if anyone has a solution, I would be very happy.

-Mike


Take a look at the Toolhelp Api''s - and if you need your prog to work on NT4 then also look at the PSAPI.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement