ShellExecute并获取其他exe的状态

ShellExecute并获取其他exe的状态

本文介绍了ShellExecute并获取其他exe的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我需要检查外部应用程序是否结束.
我的意思是我正在按照以下步骤操作:

HiI 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(**);



我的问题是我想避免使用锁定应用程序的WaitForSingleObject.另外创建一个仅调用WaitForSingleObject的新线程看起来不是一个很好的解决方案.

我已经有一个计时器,可以用来做其他一些事情,在那里我需要知道检查外部进程的结果,以了解它是否正在运行.
那么,有没有办法检查给定句柄的状态"(来自ShellExecuteEx)?
我的意思是:
ShExecInfo.hProcess

ShExecInfo.hInstApp



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

推荐答案


句柄必须具有PROCESS_QUERY_INFORMATION或PROCESS_QUERY_LIMITED_INFORMATION访问权限.有关更多信息,请参见进程安全性和访问权限.

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


您可以使用CreateProcess而不是ShellExecuteEx来询问这样的权限.


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


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");


这篇关于ShellExecute并获取其他exe的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:16