简体   繁体   English

如何使用Windows服务启动程序?

[英]how to start a program using a windows service?

I created a windows service in c++ using visual studios and now I want the service to run an exe file. 我使用Visual Studio在C ++中创建了Windows服务,现在我希望该服务运行exe文件。 The service is set to start every time the computer starts 该服务设置为在每次计算机启动时启动

i know i need to use code to locate the path of the exe like C:\\MyDirectory\\MyFile.exe but how to I actually run the file from the service? 我知道我需要使用代码来定位exe的路径,例如C:\\MyDirectory\\MyFile.exe但如何实际从服务中运行文件?

i read about the process start method here but i am not sure how to use it 在这里阅读了有关流程启动方法的信息,但是我不确定如何使用它

You can use createprocess function in your service to run an exe. 您可以在服务中使用createprocess函数来运行exe。

TCHAR* path = L"C:\\MyDirectory\\MyFile.exe";

STARTUPINFO info;
PROCESS_INFORMATION processInfo;

ZeroMemory( &info, sizeof(info) );
info.cb = sizeof(info);
ZeroMemory( &processInfo, sizeof(processInfo) );


if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
    ::WaitForSingleObject(processInfo.hProcess, INFINITE);
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置Windows启动时自动启动的C ++程序?(通过Windows Service解决方案) - How to set a C++ program to start automatically when windows starts up?(By windows service solution) Windows 7驱动程序服务启动 - Windows 7 driver service start 如何在Windows服务程序中捕获SERVICE_CONTROL_SHUTDOWN代码 - How to catch SERVICE_CONTROL_SHUTDOWN code in Windows Service Program 通过串口使用Windows服务的LG TV Control在PC启动后大约30秒运行 - 如何更快地启动服务? - LG TV Control through serial using Windows service runs about 30 seconds after PC is powered on - how to start the service sooner? Windows命令行:如何在特定文件夹中启动程序 - Windows command line: how to start a program in a specific folder 如何从Windows服务在系统帐户下启动单独的进程? - how to start separate process under system account from a windows service? 如何检查程序是否已通过Windows Service启动并正常运行? - how to check if a program is started and running perfectly by windows service? 如何在一个程序中运行多个 Windows 服务 - how to run more than one windows service in a single program 当C++中的Windows中的服务崩溃时如何运行程序? - How to run a program when a service crashes in Windows in C++? 如何使用QT在Windows中启动进程? - How to start a process in Windows using QT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM