Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need to check if an external application is end.
I mean that I am following something like these steps:

SHELLEXECUTEINFO ShExecInfo;
...
ShExecInfo.fMask=SEE_MASK_NOCLOSEPROCESS;
...
ShellExecuteEx(&ShExecInfo);   // This starts the other exe
...
WaitForSingleObject(**);


My problem is that I want to avoid to use WaitForSingleObject that locks the application. Also create a new thread only to call WaitForSingleObject does not looks a nice solution.

I already have a timer that I use to do some other stuff, and there I need to know the result of a check of the external process to know if it is running or not.
So, is there a way to check the 'status' of the given handles (comes from ShellExecuteEx)?
I mean:
ShExecInfo.hProcess
or
ShExecInfo.hInstApp
Posted

Yes, there is a way:
GetExitCodeProcess[^].

Since the documentation states:


The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right. For more information, see Process Security and Access Rights.

you may use CreateProcess instead of ShellExecuteEx for asking such rights.

 
Share this answer
 
Hi,
thanks for the info.

CreateProcess+GetExitCodeProcess works.
The following sample code looks give what expected.
The problem is only that I have not found a way to tell CreateProcess the flag PROCESS_QUERY_INFORMATION.
For some reason that flag was not necessary ...mmmm ... anyone knows the right way to set it?


SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd, PROCESS_QUERY_INFORMATION);
SetSecurityDescriptorDacl(&sd,TRUE,(PACL) NULL,FALSE);

SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = &sd;
//sa.lpSecurityDescriptor = PROCESS_QUERY_INFORMATION;
sa.bInheritHandle = FALSE;

STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

if(! CreateProcess(
			NULL,
			Msg_Exe,
			NULL,	// &sa, Generates error
			NULL,	// &sa, Generates error
			FALSE,
			0,
			NULL,
			NULL,
			&si,
			&pi
)){
	// Error
	int breakpoint;
	breakpoint=0;
}

do{
	DWORD exitCode;
	if(! GetExitCodeProcess(pi.hProcess, &exitCode)){
		// Handle error.
		int breakpoint;
		breakpoint=0;
	}else{
		if(exitCode!=STILL_ACTIVE)
			break;
		printf("Program is running\r\n");
	}
}while(1);
printf("Program is stop\r\n");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900