Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

doctorsixstring

Detecting a process

This topic is 5903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to detect whether a process is running or not? In my app, I use ShellExecuteEx() to launch an executable. ShellExecuteEx() returns a handle to a process, which I want to use to find out when the launched app is done running. I have looked at many of the process handling functions, but none of them seem to allow me to see whether a given app is currently running. Any thoughts? -Mike

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the rapid replies! I meant that I need my app (the computer) to detect if a process is currently running. I''m looking into WaitForSingleObject - it may be what I''m looking for. More ideas are welcome.

Share this post


Link to post
Share on other sites
If the process handle returned by ShellExecute is a "pseudo" handle, you can use DuplicateHandle on it to get a "real" handle. You can also get a "real" handle to a spawned process through the process information structure passed in to CreateProcess. CreateProcess also allows for passing i/o handles to the spawned process.

WaitForSingleObject lets you wait on the spawned prog. If it''s a GUI prog and you want to wait for the spawn to initialize, check out WaitForInputIdle.

If you want to check all the currently running processes to see if the program is already running, look into the Toolhelp functions.

Share this post


Link to post
Share on other sites
When I call FormatMessage() after running WaitForSingleObject(), it returns "The handle is invalid." as an error message. Right before running WaitForSingleObject(), ShellExecuteEx() runs. Is the process that ShellExecuteEx considered a "pseudo" handle then?

Also, WaitForSingleObject() talks about waiting for a handle to be in the specified "signaled state". What exactly does it mean to be "signaled"?

-Mike

[edited by - doctorsixstring on July 24, 2002 9:45:08 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by doctorsixstring
When I call FormatMessage() after running WaitForSingleObject(), it returns "The handle is invalid." as an error message. Right before running WaitForSingleObject(), ShellExecuteEx() runs. Is the process that ShellExecuteEx considered a "pseudo" handle then?


I don''t know for certain as I usually use CreateProcess instead of ShellExecuteEx. According to the doc regarding SHELLEXECUTEINFO:
quote:
hProcess
Handle to the newly started application. This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Microsoft® Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.



quote:
Original post by doctorsixstring
Also, WaitForSingleObject() talks about waiting for a handle to be in the specified "signaled state". What exactly does it mean to be "signaled"?


That means that the object the handle refers to has gained the threads attention and the Wait... function will return WAIT_OBJECT_0+n - that is "success". A nonsignaled object remains in a wait state. There are several different kinds of objects that can be used with the Wait... functions: Change notification, Console input, Event, Job, Mutex, Process, Semaphore, Thread, Waitable timer. Each object signals for different reasons.

In the case of a process, the process signals when it exits, so if you wait on it, the calling thread will remain blocked until either the process exits or the timeout expires. That''s why I mentioned the WaitForInputIdle function as it enables a thread to suspend its execution until the specified process has finished its initialization and is waiting for user input with no input pending. Iirc, WaitForInputIdle does not work with console based programs.

Share this post


Link to post
Share on other sites
Check out the _spawn functions. If you want to spawn a new process and have the calling thread wait until the spawned process is finished (sounds like what you need), you can specify _P_WAIT and the calling thread will be blocked until the spawned process terminates.

[edited by - noparity on July 24, 2002 1:49:39 PM]

Share this post


Link to post
Share on other sites
The _spawn family seems to be what I want. However, it is giving me errors:

When I run the following code:

_spawnvp( _P_WAIT, "Setup.exe", NULL);

I get a "debug assertion failed" error, with the following message: "argv!=NULL". I would assume this means that the system doesn''t like seeing NULL where the argument list should be. However, I don''t need to pass any parameters, so I figured I could set it to NULL.

So what am I doing wrong?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!