C++ 以编程方式重新启动 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6126443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically restart a Windows Service
提问by Nick
I know this can be done in C#/.Net but I was wondering whether it can be done calling the Windows API?
我知道这可以在 C#/.Net 中完成,但我想知道它是否可以通过调用 Windows API 来完成?
I have a service which will service DHCP requests, when I want to update the configuration for the service I need to stop the service, rewrite its INI file and then start the service again.
我有一个服务可以为 DHCP 请求提供服务,当我想更新该服务的配置时,我需要停止该服务,重写其 INI 文件,然后再次启动该服务。
Any help appreciated!
任何帮助表示赞赏!
回答by Ferruccio
- Open the service control manager with OpenSCManager.
- Open the service you want to control with OpenService.
- Use ControlServiceor ControlServiceExwith a SERVICE_CONTROL_STOP parameter to stop the service.
- Do whatever you need to do.
- Use StartServiceto restart the service.
- Use CloseServiceHandleto close the service and SCM handles.
- 使用OpenSCManager打开服务控制管理器。
- 使用OpenService打开要控制的服务。
- 使用带有 SERVICE_CONTROL_STOP 参数的ControlService或ControlServiceEx来停止服务。
- 做任何你需要做的事情。
- 使用StartService重新启动服务。
- 使用CloseServiceHandle关闭服务和 SCM 句柄。
回答by Nick
Sure, there's a whole bunch of C API functions see for example http://msdn.microsoft.com/en-us/library/ms682108%28v=vs.85%29.aspx.
当然,有一大堆 C API 函数,例如http://msdn.microsoft.com/en-us/library/ms682108%28v=vs.85%29.aspx。
回答by Micha?l Roy
Sometimes you want to restart the service from the service itself. This solution worked for me. I omitted the rather trivial timer code for clarity. Note that this solution works only because the caller is a service and runs at an elevated level.
有时您想从服务本身重新启动服务。这个解决方案对我有用。为清楚起见,我省略了相当琐碎的计时器代码。请注意,此解决方案之所以有效,仅是因为调用方是服务并在较高级别上运行。
void Watchdog::OnTimeout()
{
STARTUPINFO si = { };
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
PROCESS_INFORMATION pi = { };
// is modified by the call to CreateProcess (unicode version).
TCHAR szCmdLine[] = _T("cmd.exe /C \"net stop <yourservicenamehere> & net start <yourservicenamehere>\"");
// send shell command to restart our service.
if (!CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
{
// do some error reporting...
LOG("*** ERROR *** Watchdog timeout. Restart attempt failed. Last error: 0x%x\n", GetLastError());
}
}
回答by Itaypk
I'm late for the party, but I'd suggest the official detailed examples for both starting and stopping a service. The advantage over the other answers here is that they include error handling, edge cases and dependent services -
我参加聚会迟到了,但我建议使用官方详细示例来启动和停止服务。与此处其他答案相比的优势在于它们包括错误处理、边缘情况和相关服务 -
Stop: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686335(v=vs.85).aspxStart: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686315(v=vs.85).aspx
停止:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms686335(v= vs.85).aspx开始:http: //msdn.microsoft.com/en-us/library/ windows/桌面/ms686315(v=vs.85).aspx
回答by Robb
Are you looking for something like this: Starting and stopping services?
您是否正在寻找这样的东西:启动和停止服务?
回答by Adrian Conlon
You'll need to open the service control manager (OpenSCManager), then open the service itself (OpenService) and finally ask it to start itself (StartService). Close off all the handles you've used along the way, then you're done.
您需要打开服务控制管理器 (OpenSCManager),然后打开服务本身 (OpenService),最后要求它自行启动 (StartService)。关闭您沿途使用的所有手柄,然后就完成了。
Wow, handling services in C++ really takes me back...
哇,用 C++ 处理服务真的让我回来了......
Good luck,
祝你好运,
回答by takrl
In a dos box, net start [service]
would do what you need. If you don't find an API solution, you could always start a process for net.exe
using start [service]
as parameters.
在 dos 框中,net start [service]
会做你需要的。如果你没有找到一个API解决方案,你总是可以启动一个进程net.exe
使用start [service]
的参数。
回答by Nemo
You can also use WMIfor this. The sample code at that link is VB, but it should be pretty easy to port to just about any language on Windows.
您也可以为此使用 WMI。该链接上的示例代码是 VB,但它应该很容易移植到 Windows 上的几乎任何语言。
If you are into automation on Windows, WMI is good to learn because it provides a language-neutral object-oriented interface to most of the interesting objects on the system.
如果您对 Windows 上的自动化感兴趣,那么学习 WMI 会很好,因为它为系统上的大多数有趣对象提供了一个与语言无关的面向对象的接口。