Run commands using CreateProcess in NT Services
Author
Zhou Renjian
Create@
2004-11-14 13:35
void CSimpleHttpConn::restartPeanutService() {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
"net stop \"Peanut Hull Client Service\"", // Command line.
//"net stop \"Messenger\"",
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to FALSE.
NORMAL_PRIORITY_CLASS, // No creation flags.
NULL, // Use parent's environment block.
"F:\\Program Files\\PeanutHull\\", // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
m_eventLog->Write(EVENTLOG_ERROR_TYPE, "net stop \"Peanut Hull Client Service\" fails.");
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
"net start \"Peanut Hull Client Service\"", // Command line.
//"net start \"Messenger\"",
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to FALSE.
NORMAL_PRIORITY_CLASS, // No creation flags.
NULL, // Use parent's environment block.
"F:\\Program Files\\PeanutHull\\", // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
m_eventLog->Write(EVENTLOG_ERROR_TYPE, "net start \"Peanut Hull Client Service\" fails.");
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}