GetServiceState function

获取指定服务的状态


Function:

int UAPI GetServiceState(
        int hService
);

Parameters:

  • (int) hService:服务句柄

Return value:

  • Type:int
  • Text:成功返回服务状态标志,失败返回 INVALID(-1)
Constant Value Notes
SERVICE_STATE_RUN 0x01 服务正在运行
SERVICE_STATE_STOP 0x02 服务已停止
SERVICE_STATE_NULL 0x00 服务是无效的

Remarks:

一个良好的用户扩展程序应在启动或停止其服务前使用 GetServiceState function 获取其服务目前的状态,避免重复的请求


C++ Code:

#include <h.studio>
#include <h.service>

int hello(){
    int hService = CreateService("\\\test_service",SERVICE_OPENEXIST,NULL);
    if(IsServiceByHandle(hService)){
        int dwState = GetServiceState(hService); // get service state descriptor
        if(dwState == SERVICE_STATE_RUN){        // to be friendly, EPL uses nested if instead of switch
            output("The service is running");
        }if(dwState == SERVICE_STATE_STOP){
            output("The service is stopped");
        }if(dwState == SERVICE_STATE_NULL){
            output("The service is invalid or unreadable");
        }else{
            output("operation failed");
        }
        CloseService(hService);
    }else{
        output("Failed to open service \\test_service");
    }
    return 0;
}

results matching ""

    No results matching ""