Solved

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

Posted on 2015-02-23
3
172 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 33

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 33

Accepted Solution

by:
sarabande earned 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

820 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