Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CreateProcessW, WaitForSingleObject - can't wait for it to exit without WAIT_FAILURE

Posted on 2015-02-23
3
Medium Priority
?
195 Views
Last Modified: 2015-02-24
Hi Experts,

I'm running a process (and it does run - I see its output in its entirety - console app outputs text), with CreateProcessW.  When I wait on the process information object's handle, I get back that it's an invalid handle.  What am I doing wrong here?

Makes me think I can't wait on it because the process is already gone before the Wait?  I don't see why that would affect the signaling of the process info object though... So I think that can't be it.

Thanks!
Mike

STARTUPINFOW si;
PROCESS_INFORMATION pi;
BOOL bRes;
DWORD dwCode = 0;

ZeroMemory ( &si, sizeof ( STARTUPINFOW));

si.cb = sizeof ( STARTUPINFOW);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = bShow ? SW_SHOWNORMAL : SW_HIDE;

CStringW strCommand = L"c:\\test\\runme.exe";
bRes = CreateProcessW(NULL, strCommand.GetBuffer(), NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);

while(TRUE)
	{
		DWORD dwWaitResult = WaitForSingleObject(&pi.hProcess, 3000);

		// if the process finished, we break out
		if(dwWaitResult == WAIT_OBJECT_0)
			break;

		else if(dwWaitResult == WAIT_FAILED)
		{
			// ALWAYS GETTING HERE - GetLastError() returns 6- (INVALID HANDLE).  How come?
		}

                // ......
      }

Open in new window

0
Comment
Question by:thready
  • 2
3 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 40627556
you may check for the bRes returned from CreateProcess. you probably have to add a little delay (by using Sleep) between CreateProcess and WaitForSingleObject because the thread might not be started. so in my opinion it is not because the thread has already exited but has not started yet.

Sara

p.s.
you also may check if pi.hProcess is a valid handle.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 40627567
WaitForSingleObject( &pi.hProcess, 3000);
the first argument is not a pointer to handle but a handle:

WaitForSingleObject( pi.hProcess, 3000);

Open in new window



if you do

STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 };

Open in new window


you can omit calls to ZeroMemory then.

see also https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx 

Sara
0
 
LVL 1

Author Closing Comment

by:thready
ID: 40628234
Thank you!  :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question