为什么我不能在Windows Server 2008中使用System()从服务启动批处理文件
Why can't I start a batch file from a service using system() in Windows Server 2008?
我有一个C 程序,该程序在64位Windows Server 2008计算机上运行。该程序试图使用以下命令启动批处理文件:
system(C:pathtofilefile.bat)
在32位Windows Server 2003中,此功能良好(执行了批处理文件),但是在Windows Server 2008上,批处理文件未执行,我获得了0xc000000142文件作为测试以查看是否执行)。即使我尝试执行不存在的东西,我实际上也会得到相同的回报值。
我在Windows Server 2008中阅读了有关Session 0隔离的信息,因此我使用PSEXEC在会话0中启动命令提示符为同一域用户,该域用户被列为" log as on a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a as"用户:
psexec -i 0 -u DOMAINserviceuser -p passwd cmd.exe
然后,我能够从命令提示符成功执行批处理文件。
被列为"服务用户"的登录的域用户在管理员组中。另外,如果我手动启动C 应用程序(不是作为服务),它将启动批处理文件。
那么,关于会话0隔离是否有一些使System()调用作为服务运行时不起作用的东西?还是关于行为变化的其他一些解释?我知道System()不一定是这样做的最佳方法,但是我正在寻找这种不再起作用的实际原因。
从我现在对问题的了解中,您无法使用system()在Windows Server 2008中从服务运行批处理文件的原因是因为Windows考虑了任何尝试调用CMD的尝试.exe(就像运行批处理文件一样)作为启动交互式服务的尝试。由于会议0隔离,这是不允许的,即使您可能不会将批处理文件视为互动或有GUI(这使我失望),也只会失败。
我弄清楚了如何获得使用CreateProcess的效果。这里的关键是您必须将dwCreationFlags参数设置为create_no_window。因此,我(简化的)呼叫最终看起来像这样:
CreateProcess(NULL, // No module name (use command line)
L"C:\Windows\System32\cmd.exe /C myfile.bat", // Call cmd.exe with /C flag
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // Use CREATE_NO_WINDOW!!!
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 如何在c++中使用system()来运行包含空格的python脚本
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 检测win32服务创建和删除的最佳方法
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- gRPC 生成的代码缺少服务实现
- C# DLL 从C++应用程序调用 SOAP Web 服务
- 在 Winrt/C++ 中使用应用服务
- 将第二个 GATT 服务添加到 Movesense 容器
- 程序在使用 system() 启动另一个可执行文件时停止
- 为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?
- 从运行服务的应用程序代码中提取窗口句柄
- 为什么"using System;"不被视为不良做法?
- C++ 合并字符串以'system'函数错误
- 对于使用高级服务的分钟数和应付金额,我不断得到相同的数字
- 为什么我不能在Windows Server 2008中使用System()从服务启动批处理文件
- 我可以从Windows服务调用system()吗