Windows API: How to get something's process ID

Posted by: tfabris

Windows API: How to get something's process ID - 05/04/2004 15:57

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.
Posted by: wfaulk

Re: Windows API: How to get something's process ID - 05/04/2004 16:10

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.
Posted by: tfabris

Re: Windows API: How to get something's process ID - 05/04/2004 16:44

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.
Posted by: tfabris

Re: Windows API: How to get something's process ID - 05/04/2004 17:07

I'm currently looking into how to use the "EnumProcesses" function to see if that's what I'm looking for....
Posted by: g_attrill

Re: Windows API: How to get something's process ID - 05/04/2004 17:22

This KB seems to explain it - I expect you have found it though:

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

Re: Windows API: How to get something's process ID - 05/04/2004 17:22

Sounds like you are already reading this, but here's Microsoft's how-to on what you're trying to do.

- trs
Posted by: tfabris

Re: Windows API: How to get something's process ID - 05/04/2004 17:36

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.
Posted by: jules

Re: Windows API: How to get something's process ID - 06/04/2004 10:30

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;
}

Posted by: Roger

Re: Windows API: How to get something's process ID - 07/04/2004 02:07

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).
Posted by: tfabris

Re: Windows API: How to get something's process ID - 07/04/2004 10:26

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.