ShellExecute并获取其他exe的状态 [英] ShellExecute and get status of the other exe

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

问题描述

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

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



我的问题是我想避免使用锁定应用程序的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

推荐答案

是的,有一种方法:
GetExitCodeProcess [
Yes, there is a way:
GetExitCodeProcess[^].

Since the documentation states:


句柄必须具有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.



感谢您提供信息.

CreateProcess + GetExitCodeProcess有效.
以下示例代码看起来像预期的那样.
问题只是我还没有找到一种方法来告诉CreateProcess标志PROCESS_QUERY_INFORMATION.
出于某种原因,不需要标记... mmmm ...有人知道正确的设置方法吗?


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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆