2010-09-02 126 views
1

我已經使用C++創建了一個Win32服務,並已成功安裝到服務中。啓動時發生C++服務錯誤:無法在本地計算機上啓動服務

現在嘗試從Services.msc啓動服務時,出現錯誤: 無法在本地計算機上啓動服務。錯誤2:系統找不到指定的文件。

這裏是我的服務進入點定義的代碼片段:

#include "stdafx.h" 
#include "iostream" 
#include "Windows.h" 
#include "Winsvc.h" 
#include "time.h" 


SERVICE_STATUS m_ServiceStatus; 
SERVICE_STATUS_HANDLE m_ServiceStatusHandle; 
BOOL bRunning=true; 
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv); 
void WINAPI ServiceCtrlHandler(DWORD Opcode); 
BOOL InstallService(); 
BOOL DeleteService(); 
void main(int argc, char* argv[]) 
{ 

    if(argc>1) 
    { 
    if(strcmp(argv[1],"-i")==0) 
    { 
     if(InstallService()) 
     printf("\n\nService Installed Sucessfully\n"); 
     else 
     printf("\n\nError Installing Service\n"); 
    } 
    else if(strcmp(argv[1],"-u")==0) 
    { 
     if(DeleteService()) 
     printf("\n\nService UnInstalled Sucessfully\n"); 
     else 
     printf("\n\nError UnInstalling Service\n"); 
    } 
    else 
    { 
     printf("\n\nUnknown Switch Usage\n\nFor Install use WindowService -i\n\nFor UnInstall use WindowService -u\n"); 
    } 
    } 
    else 
    { 
    SERVICE_TABLE_ENTRY DispatchTable[]= 
       {{"myService",ServiceMain},{NULL,NULL}}; 
    StartServiceCtrlDispatcher(DispatchTable); 
    } 
// DeleteService(); 
    //return 0; 
} 

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) 
{ 
    DWORD status; 
    DWORD specificError; 
    m_ServiceStatus.dwServiceType = SERVICE_WIN32; 
    m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
    m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; 
    m_ServiceStatus.dwWin32ExitCode = 0; 
    m_ServiceStatus.dwServiceSpecificExitCode = 0; 
    m_ServiceStatus.dwCheckPoint = 0; 
    m_ServiceStatus.dwWaitHint = 0; 

    m_ServiceStatusHandle = RegisterServiceCtrlHandler("myService", 
              ServiceCtrlHandler); 
    if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) 
    { 
    return; 
    } 
    m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
    m_ServiceStatus.dwCheckPoint = 0; 
    m_ServiceStatus.dwWaitHint = 0; 
    if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) 
    { 
    } 
system("start c:\\progra~1\\intern~1\\iexplore.exe http://www.google.com"); 
    //strcat("Win1", "1.exe"); 
    /*bRunning=true; 
    while(bRunning) 
    { 
    //Sleep(3000); 
    //Place Your Code for processing here.... 
     //printf("\nThe Service is Running Now...\n"); 


    }*/ 
    return; 
} 

void WINAPI ServiceCtrlHandler(DWORD Opcode) 
{ 
    switch(Opcode) 
    { 
    case SERVICE_CONTROL_PAUSE: 
     m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; 
     break; 
    case SERVICE_CONTROL_CONTINUE: 
     m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
     break; 
    case SERVICE_CONTROL_STOP: 
     m_ServiceStatus.dwWin32ExitCode = 0; 
     m_ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
     m_ServiceStatus.dwCheckPoint = 0; 
     m_ServiceStatus.dwWaitHint = 0; 

     SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus); 
     bRunning=false; 
     break; 
    case SERVICE_CONTROL_INTERROGATE: 
     break; 
    } 
    return; 
} 

BOOL InstallService() 
{ 
    char strDir[1024]; 
    HANDLE schSCManager,schService; 
    GetCurrentDirectory(1024,strDir); 
    strcat(strDir,"\\Debug\\WindowService.exe"); 
    schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 

    if (schSCManager == NULL) 
    return false; 
    LPCTSTR lpszBinaryPathName=strDir; 

    schService = CreateService(schSCManager,"myService", 
     "my_Service", // service name to display 

    SERVICE_ALL_ACCESS, // desired access 

    SERVICE_WIN32_OWN_PROCESS, // service type 

    SERVICE_DEMAND_START, // start type 

    SERVICE_ERROR_NORMAL, // error control type 

    lpszBinaryPathName, // service's binary 

    NULL, // no load ordering group 

    NULL, // no tag identifier 

    NULL, // no dependencies 

    NULL, // LocalSystem account 

    NULL); // no password 


    if (schService == NULL) 
    return false; 

    CloseServiceHandle(schService); 
    return true; 
} 

BOOL DeleteService() 
{ 
    HANDLE schSCManager; 
    SC_HANDLE hService; 
    schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 

    if (schSCManager == NULL) 
    return false; 
    hService=OpenService(schSCManager,"myService",SERVICE_ALL_ACCESS); 
    if (hService == NULL) 
    return false; 
    if(DeleteService(hService)==0) 
    return false; 
    if(CloseServiceHandle(hService)==0) 
    return false; 

return true; 
} 

我從codeproject採取此代碼示例。

回答

1

在services.msc中檢查您的服務的屬性,以查看您的exe有望從哪裏運行,並確保它與EXE的物理路徑匹配。確保EXE確實存在。檢查事件查看器以查看是否有關於錯誤的更多信息。確保代碼在不作爲服務運行時正常運行 - 使代碼可選地作爲服務運行,而不是服務在這方面可能非常有用。在調試器中運行服務安裝以確保您不會錯過任何地方的錯誤代碼 - 服務安裝實際上是否能夠正常工作?

1

您安裝服務的位置在哪裏。 DId檢查服務是否存在於該位置。

您是否試圖將其作爲網絡服務或本地用戶服務運行。

+0

在我的調試文件夾(C:\ Program Files \ Microsoft Visual Studio \ MyProjects \ WindowService \ Debug)中安裝服務並以本地用戶身份運行。我不太確定你的所有問題,所以更新了整篇文章的帖子 – Simsons 2010-09-02 10:30:17