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;
}
還有一件事我忘了說,當我強迫運行它似乎工作服務frfom任務管理器。