我已经通过执行以下操作创建了一个运行良好的 Windows 服务。
SC_HANDLE hService = ::CreateService(*m_ServiceConfig, // SCM database
name.c_str(), // name of service
displayname.c_str(), // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type (interactive for debug)
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
path.c_str(), // path to service's binary
nullptr, // no load ordering group
nullptr, // no tag identifier
dependencies, // dependencies
nullptr, // LocalSystem account
nullptr); // no password
如您所见,我将访问权限指定为 SERVICE_ALL_ACCESS
在网上,这个常量表明您有权暂停、继续和停止服务,
然而,当我运行命令时 net stop <service-name>
我得到以下输出
the requested pause continue or stop is not valid for this service
我的问题是我创建服务的方式有什么问题吗?
服务控制处理函数
DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID /*lpEventData*/, LPVOID /*lpContext*/)
{
// Handle the requested control code.
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
TVLOG_INFO(L"SERVICE_CONTROL_STOP");
{
MessageBoxA(NULL, "Please stop the service", "Uninstall service error!", MB_OK | MB_ICONERROR);
}
最佳答案
您在 CreateService()
的 dwDesiredAccess
参数中指定的访问权限对 net stop
命令没有影响。访问权限仅适用于返回的 SC_HANDLE
并影响该句柄如何与后续 API 调用交互。
您看到的错误很可能是由您调用 SetServiceStatus()
引起的不包括 SERVICE_STATUS::dwControlsAccepted
字段中的 SERVICE_ACCEPT_STOP
标志。
dwControlsAccepted
The control codes the service accepts and processes in its handler function (seeHandler
andHandlerEx
). A user interface process can control a service by specifying a control command in theControlService
orControlServiceEx
function. By default, all services accept the SERVICE_CONTROL_INTERROGATE value. To accept the SERVICE_CONTROL_DEVICEEVENT value, the service must register to receive device events by using theRegisterDeviceNotification
function.The following are the control codes.
...
SERVICE_ACCEPT_STOP
0x00000001The service can be stopped.
This control code allows the service to receive SERVICE_CONTROL_STOP notifications.
关于c++ - 无法使用 net stop 命令停止 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411829/