0

当我尝试像这样安装它时,我遇到了服务问题:

sc create MemoryStatus binpath= "C:\Users\rock\Documents\Visual Studio 2010\Projects\ServiceInC\Release\ServiceInC.exe"

我在 cmd 中收到一条消息,表明服务已成功安装,但是当我通过任务管理器查看它时,它始终处于停止状态。另一件事是该服务甚至没有启动,因为我没有看到任何日志消息。

好像权限有问题或者类似的问题,你能帮我解决一下吗?

我的主要服务功能是:

void ServiceMain(int argc, char** argv) 
{ 
    int error; 

    ServiceStatus.dwServiceType        = SERVICE_WIN32; 
    ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
    ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    ServiceStatus.dwWin32ExitCode      = 0; 
    ServiceStatus.dwServiceSpecificExitCode = 0; 
    ServiceStatus.dwCheckPoint         = 0; 
    ServiceStatus.dwWaitHint           = 0; 

    hStatus = RegisterServiceCtrlHandler(
        "MemoryStatus", 
        (LPHANDLER_FUNCTION)ControlHandler); 
    if (hStatus == (SERVICE_STATUS_HANDLE)0) 
    { 
        // Registering Control Handler failed
        WriteToLog("Registering Control Handler failed");
        return; 
    }  
    // Initialize Service 
    error = InitService(); 
    if (error) 
    {
        // Initialization failed
        WriteToLog("Initialization failed");
        ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
        ServiceStatus.dwWin32ExitCode      = -1; 
        SetServiceStatus(hStatus, &ServiceStatus); 
        return; 
    } 
    // We report the running status to SCM. 
    ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
    SetServiceStatus (hStatus, &ServiceStatus);

    MEMORYSTATUS memory;
    // The worker loop of a service
    while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
    {
        char buffer[16];
        GlobalMemoryStatus(&memory);
        sprintf(buffer, "%d", memory.dwAvailPhys);
        int result = WriteToLog(buffer);
        if (result)
        {
            ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
            ServiceStatus.dwWin32ExitCode      = -1; 
            SetServiceStatus(hStatus, &ServiceStatus);
            return;
        }

        Sleep(SLEEP_TIME);
    }
    return; 
}

我忘了说的另一件事,当我强制运行 service frfom 任务管理器时,它似乎可以工作。

4

1 回答 1

3

该命令sc create...仅创建服务,不会尝试启动它。来自sc /?

create----------创建服务。(将其添加到注册表中)。

要尝试启动服务,请通过服务控制小程序或运行:

sc 启动内存状态

于 2012-08-27T14:51:10.483 回答