Solved

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

Posted on 2015-02-23
3
180 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 52
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 60
PL SQL Developer 7 90
Winapi.Windows.hpp problem 7 46
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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