如何以编程方式获取服务的状态(正在运行/已停止)

发布于 2024-12-10 13:33:02 字数 49 浏览 0 评论 0原文

我需要在我的 C++ 应用程序中获取 Windows“打印后台处理程序”服务的状态。

I need to get the status of Windows "print spooler" service in my C++ application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-12-17 13:33:02

@shikarssj提供的功能运行良好,加载服务时只需要管理员权限。

这是一个不需要完全许可的版本:

#include <Windows.h>

int GetServiceStatus( const char* name )
{
    SC_HANDLE theService, scm;
    SERVICE_STATUS m_SERVICE_STATUS;
    SERVICE_STATUS_PROCESS ssStatus;
    DWORD dwBytesNeeded;


    scm = OpenSCManager( nullptr, nullptr, SC_MANAGER_ENUMERATE_SERVICE );
    if( !scm ) {
        return 0;
    }

    theService = OpenService( scm, name, SERVICE_QUERY_STATUS );
    if( !theService ) {
        CloseServiceHandle( scm );
        return 0;
    }

    auto result = QueryServiceStatusEx( theService, SC_STATUS_PROCESS_INFO,
        reinterpret_cast<LPBYTE>( &ssStatus ), sizeof( SERVICE_STATUS_PROCESS ),
        &dwBytesNeeded );

    CloseServiceHandle( theService );
    CloseServiceHandle( scm );

    if( result == 0 ) {
        return 0;
    }

    return ssStatus.dwCurrentState;
}

The function that @shikarssj provided is working perfectly, it only requires admin rights when loading the service.

Here is a version that does not ask for full permission:

#include <Windows.h>

int GetServiceStatus( const char* name )
{
    SC_HANDLE theService, scm;
    SERVICE_STATUS m_SERVICE_STATUS;
    SERVICE_STATUS_PROCESS ssStatus;
    DWORD dwBytesNeeded;


    scm = OpenSCManager( nullptr, nullptr, SC_MANAGER_ENUMERATE_SERVICE );
    if( !scm ) {
        return 0;
    }

    theService = OpenService( scm, name, SERVICE_QUERY_STATUS );
    if( !theService ) {
        CloseServiceHandle( scm );
        return 0;
    }

    auto result = QueryServiceStatusEx( theService, SC_STATUS_PROCESS_INFO,
        reinterpret_cast<LPBYTE>( &ssStatus ), sizeof( SERVICE_STATUS_PROCESS ),
        &dwBytesNeeded );

    CloseServiceHandle( theService );
    CloseServiceHandle( scm );

    if( result == 0 ) {
        return 0;
    }

    return ssStatus.dwCurrentState;
}
深居我梦 2024-12-17 13:33:02

我找不到任何使用 WinApi 和 C++ 的好例子。我尝试并编译了以下内容,它可以在 Borland 中运行。希望这对某人有帮助。

int getServiceStatus(char* name) 
{
   SC_HANDLE theService,scm;
   SERVICE_STATUS m_SERVICE_STATUS;
   SERVICE_STATUS_PROCESS ssStatus;
   DWORD dwBytesNeeded;

   scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
   if (!scm) {
     ShowErr();
     return 0;
   }


   theService = OpenService(scm, name, SERVICE_ALL_ACCESS);
   if (!theService) {
     CloseServiceHandle(scm);
     ShowErr();
     return 0;
   }

   int result = QueryServiceStatusEx(theService, SC_STATUS_PROCESS_INFO, (LPBYTE)       
                                   &ssStatus, sizeof(SERVICE_STATUS_PROCESS), 
                                   &dwBytesNeeded);

CloseServiceHandle(theService);
CloseServiceHandle(scm);

if (result == 0) return 0; // fail query status

return ssStatus.dwCurrentState;

}

I couldn't find any good example using WinApi and C++. I tried and compiled the following and it works in Borland. Hope this helps someone.

int getServiceStatus(char* name) 
{
   SC_HANDLE theService,scm;
   SERVICE_STATUS m_SERVICE_STATUS;
   SERVICE_STATUS_PROCESS ssStatus;
   DWORD dwBytesNeeded;

   scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
   if (!scm) {
     ShowErr();
     return 0;
   }


   theService = OpenService(scm, name, SERVICE_ALL_ACCESS);
   if (!theService) {
     CloseServiceHandle(scm);
     ShowErr();
     return 0;
   }

   int result = QueryServiceStatusEx(theService, SC_STATUS_PROCESS_INFO, (LPBYTE)       
                                   &ssStatus, sizeof(SERVICE_STATUS_PROCESS), 
                                   &dwBytesNeeded);

CloseServiceHandle(theService);
CloseServiceHandle(scm);

if (result == 0) return 0; // fail query status

return ssStatus.dwCurrentState;

}

冬天旳寂寞 2024-12-17 13:33:02

使用 QueryServiceStatus 或<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684941%28v=VS.85%29.aspx" rel="noreferrer">QueryServiceStatusEx。网络上有很多关于如何使用它们的示例。

Use QueryServiceStatus or QueryServiceStatusEx. There are plenty of examples on the web on how these are used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文