Solved

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

Posted on 2015-02-23
3
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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