Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#212330 - 05/04/2004 15:57 Windows API: How to get something's process ID
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
I've got a function called ShellAndWait that does a very good job of launching a third party application, then waiting for said application to close before continuing.

It works by using the "Shell" api function, then getting the handle that was returned by the Shell function and doing an OpenProcess(PROCESS_QUERY_INFORMATION, False, <handle>), then calling GetExitCodeProcess in a loop until the shelled program exits.

All is well, that function works great.

Unfortunately, I now want to do exactly the same thing, but with a program that I did not launch myself. How do I get the process handle of a third-party application that I didn't launch?

Alternatively, how could I do a loop that peeks at the list of things in the Task Manager and waits until a certain name drops off that list? Note: The app in question is not a visible window, it sits in the system tray as a tiny icon next to the clock. So trolling for visible window names will not work, I must look at actual running processes as they are listed in the task manager.
_________________________
Tony Fabris

Top
#212331 - 05/04/2004 16:10 Re: Windows API: How to get something's process ID [Re: tfabris]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
I don't have an answer to your question, but doing a busywait in your first instance seems like a bad idea. Doesn't Win32 have a waitpid() function?

Edit: Yeah. It looks like spawn() takes a bool argument to tell it whether it should wait or not.


Edited by wfaulk (05/04/2004 16:12)
_________________________
Bitt Faulk

Top
#212332 - 05/04/2004 16:44 Re: Windows API: How to get something's process ID [Re: wfaulk]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
Doesn't Win32 have a waitpid() function?
I actually am doing other stuff in the loop, so I want the function to return and let my program do its thing. But I still need to know when the other app goes away, hence the loop.
_________________________
Tony Fabris

Top
#212333 - 05/04/2004 17:07 Re: Windows API: How to get something's process ID [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
I'm currently looking into how to use the "EnumProcesses" function to see if that's what I'm looking for....
_________________________
Tony Fabris

Top
#212334 - 05/04/2004 17:22 Re: Windows API: How to get something's process ID [Re: tfabris]
g_attrill
old hand

Registered: 14/04/2002
Posts: 1172
Loc: Hants, UK
This KB seems to explain it - I expect you have found it though:

http://support.microsoft.com/default.aspx?scid=kb;en-us;175030

Top
#212335 - 05/04/2004 17:22 Re: Windows API: How to get something's process ID [Re: tfabris]
trs24
old hand

Registered: 20/03/2002
Posts: 729
Loc: Palo Alto, CA
Sounds like you are already reading this, but here's Microsoft's how-to on what you're trying to do.

- trs
_________________________
- trs

Top
#212336 - 05/04/2004 17:36 Re: Windows API: How to get something's process ID [Re: trs24]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
Yeah, that was it. I got the information from a different place than that KB article, but EnumProcesses() was definitely the thing I was looking for.

My problem was that I was looking in the Visual Studio 6.0 help library at the "OpenProcess" api, pressing on the "See Also: Process and Thread Functions" link and expecting to see the name of a process-enumerator function in that list. That help was not comprehensive enough to list the EnumProcesses() function, so I was totally missing the existence of that function.
_________________________
Tony Fabris

Top
#212337 - 06/04/2004 10:30 Re: Windows API: How to get something's process ID [Re: tfabris]
jules
member

Registered: 21/01/2002
Posts: 174
Loc: Indiana USA
I don't know if you already took care of this, but coincidentially I am working on something that needs to do the same. Here's the function I use:

#include <tlhelp32.h>

int GetProcessID(const char* szProcessExe)
{
HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD |TH32CS_SNAPPROCESS,NULL);

PROCESSENTRY32 processEntry;
processEntry.dwSize=sizeof(PROCESSENTRY32);

DWORD dwProcessID=0;;

if( Process32First(hSnapshot,&processEntry))
{ do
{ if(!stricmp(szProcessExe,processEntry.szExeFile))
{
dwProcessID=processEntry.th32ProcessID;
break;
}
processEntry.dwSize=sizeof(PROCESSENTRY32);
} while(Process32Next(hSnapshot,&processEntry));
}


CloseHandle(hSnapshot);

return dwProcessID;
}


Top
#212338 - 07/04/2004 02:07 Re: Windows API: How to get something's process ID [Re: jules]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5682
Loc: London, UK
According to the MSDN documentation, EnumProcesses is not supported on Win95/98/Me. You need to use the ToolHelp API on those platforms (which is also supported on NT/2K/XP).
_________________________
-- roger

Top
#212339 - 07/04/2004 10:26 Re: Windows API: How to get something's process ID [Re: Roger]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31578
Loc: Seattle, WA
Ooo, thanks for that tidbit. Fortunately, the target platforms for this particular project are Win2k at a minimum. I'll keep it in mind for the future.
_________________________
Tony Fabris

Top