我正在编写Windows服务程序,它会在启动时调用外部exe文件,例如notepad.exe
。但它始终以"unhandled win32 exception occured"
我的步骤:
exe
文件:MemoryStatus.exe
。sc create MemoryStatus binpath=c:\MyServices\MemoryStatus.exe
Start
。任何人都可以帮忙表明我做错了吗?
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
}
}
答案 0 :(得分:0)
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"));