Solved

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

Posted on 2015-02-23
3
144 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 32

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now