I have an NT Service whos job it is to start an application. All it does it watches the application running, and catches its return code. When the service is told to stop, it has to tell the application to stop.
This is a snapshot of the WinMain() of the application
Code:
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{
//-----------------------------------------------------------------------------------------
// Step 1: Do stuff we must do as a windows application
//-----------------------------------------------------------------------------------------
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1), &wsaData);
HWND Hwnd; // Handle for this window
MSG Message; // Current message
WNDCLASSEX WndClassEx = {0}; // Window Class
WndClassEx.cbSize = sizeof(WndClassEx); // Size of itself
WndClassEx.lpfnWndProc = WinProc; // Message Handler
WndClassEx.hInstance = hinstance; // Instance of this window class
WndClassEx.lpszClassName = "SWEBSWindowClass"; // Class Name
Then it goes into a loop that continues "while (SERVER_STOP == false)"
This is the WinProc function of the application:
Code:
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_DESTROY:
case WM_CLOSE:
SERVER_STOP = true;
PostQuitMessage(0);
break;
default:
break;
}
// Report current status
return DefWindowProc(hwnd, message, wparam, lparam);
}
Here is the ControlHandler function of the server, which just handles the stop/shutdown service messages:
Code:
case SERVICE_CONTROL_STOP:
SERVER_STOP = true;
SendMessage((HWND)pi.hProcess , WM_CLOSE, NULL, NULL);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
SERVER_STOP = true;
SendMessage((HWND)pi.hProcess, WM_CLOSE, NULL, NULL);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
Where 'pi' is PROCESS_INFORMATION structure that was passed to the CreateProcess call when the application was started. Its supposed to hold things like handles to the started process (supposedly).
When the service is told to stop, it does, but the application doesn't. What should I do to make sure the application is shut down properly?
This is the call to CreateProcess:
Code:
if( !CreateProcess( EnginePath.c_str(), // No module name (use command line).
NULL, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
AppPath.c_str(), // Starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
TestLog("Could not create process for SWEBSEngine.exe");
}
SERVER_STOP = false;
while (SERVER_STOP == false)
{
Sleep(3000);
}
GetExitCodeProcess(pi.hProcess, &ExitCode);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
How can I tell the application to close properly? It doesn't seem to be working my way