问题现象:守护进程A无法拉起工作进程B。而且在关闭守护进程服务时系统会提示“服务没有及时响应启动或控制要求”。服务器是Windows Server2012。
问题分析:进程A创建进程B,调用的是Windows系统的ShellExecuteEx函数。函数前后加上日志,结果显示ShellExecuteEx函数没有返回。
特别的是问题只出现在这台电脑,初步怀疑是权限问题,但是当前已经是管理员登录中。查看相关相关资料,发现ShellExecuteEx可以以管理员权限启动子进程。
解决方法:
加入代码 shExecInfo.lpVerb = "runas"; 代码中已经标准
1 SHELLEXECUTEINFO shExecInfo; 2 ZeroMemory( &shExecInfo, sizeof( SHELLEXECUTEINFO ) ); 3 shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); 4 shExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; 5 shExecInfo.hwnd = NULL; 6 shExecInfo.lpVerb = "runas"; // 加入此句代码 7 shExecInfo.lpFile = path.c_str(); 8 shExecInfo.lpParameters = ""; 9 shExecInfo.lpDirectory = NULL; 10 shExecInfo.nShow = SW_HIDE; 11 shExecInfo.hInstApp = NULL; 12 ShellExecuteEx(&shExecInfo);
运行后解决问题。