2

我正在编写一个 Windows 服务程序,它会在启动时调用一个外部 exe 文件,例如notepad.exe. 但它总是失败"unhandled win32 exception occured"

我的步骤:

  1. 将代码编译成一个exe文件:MemoryStatus.exe.
  2. 使用命令安装服务:sc create MemoryStatus binpath=c:\MyServices\MemoryStatus.exe
  3. 从 Windows 控制面板中找到该服务,然后单击Start
  4. 错误发生...

谁能帮忙指出我做错了什么?

void main()
{
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = _T("MemoryStatus_new_3");
    ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)**ServiceMain**;

    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;

    StartServiceCtrlDispatcher(ServiceTable);

}
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(   _T("MemoryStatus_new_3"),   (LPHANDLER_FUNCTION)ControlHandler);
    if (hStatus == (SERVICE_STATUS_HANDLE)0)
    {
        // Registering Control Handler failed
        WriteToLog("Registering Control Handler failed\n");
        return;
    }

    // We report the running status to SCM.
    ServiceStatus.dwCurrentState =  SERVICE_RUNNING;
    SetServiceStatus (hStatus, &ServiceStatus);

    // Initialize Service
        startSvc();

    return;
}

void startSvc()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &pi, sizeof(pi) );
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    // Start the child process
    if(CreateProcess(_T("C:\\Program Files\\Source Insight 3\\Insight3.exe"), _T(""), NULL, NULL, FALSE, 0, FALSE, NULL, &si, &pi))
     {
      CloseHandle( pi.hProcess );
      CloseHandle( pi.hThread );
    }
     else 
     {
        hProcess = GetCurrentProcess();//get current process
        TerminateProcess(hProcess,0);         //close process
     }
}
4

1 回答 1

1
CreateProcess(_T("C:\\Program Files\\Source Insight 3\\Insight3.exe"),

这里存在一个问题。

将进程名称存储在一个可变数组中。

TCHAR szAppName[MAX_PATH];
StringCchCat(szAppName, _countof(szAppName), _T("C:\\Program Files\\Source Insight 3\\Insight3.exe"));
于 2013-03-11T03:49:31.840 回答