c++ - 无法使用 net stop 命令停止 Windows 服务

标签 c++ winapi service

我已经通过执行以下操作创建了一个运行良好的 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 标志。

SERVICE_STATUS structure

dwControlsAccepted
The control codes the service accepts and processes in its handler function (see Handler and HandlerEx). A user interface process can control a service by specifying a control command in the ControlService or ControlServiceEx 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 the RegisterDeviceNotification function.

The following are the control codes.

...

SERVICE_ACCEPT_STOP
0x00000001

The 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/

相关文章:

winapi - 如何将 Win32 C + MASM "solution"从 Visual Studio 2015 升级到 VS 2017/2019

android - 从 Android 应用程序调用 Web 服务

c++ - 屏幕截图特定窗口

android - Parcel.readException 将对象传递给远程服务

javascript - Angular : Resetting Factory or Service

c++ - DBF文件并行读取

c++ - timer_create 和 timerid,这允许吗?

c++ - 在知道变量是什么数据类型之前如何声明变量?

C++ 迭代器相等

c - WinAPI 消息队列示例