[19345] | 1 | #define STRICT
|
---|
| 2 | #include <Windows.h>
|
---|
| 3 | #include <process.h>
|
---|
| 4 | #include <tchar.h>
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 |
|
---|
| 7 | #ifdef PLATFORM_SDK
|
---|
| 8 | #include <strsafe.h>
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | // Helper function for displaying error codes
|
---|
| 12 | void ErrorExit(char* lpszFunction)
|
---|
| 13 | {
|
---|
| 14 | // Retrieve the system error message for the last-error code
|
---|
| 15 |
|
---|
| 16 | char* lpMsgBuf = NULL;
|
---|
| 17 | char* lpDisplayBuf = NULL;
|
---|
| 18 | DWORD dw = GetLastError();
|
---|
| 19 |
|
---|
| 20 | FormatMessage(
|
---|
| 21 | FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
| 22 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
| 23 | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
| 24 | NULL,
|
---|
| 25 | dw,
|
---|
| 26 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
| 27 | (char*) &lpMsgBuf,
|
---|
| 28 | 0, NULL );
|
---|
| 29 |
|
---|
| 30 | // Display the error message and exit the process
|
---|
| 31 | //lpDisplayBuf = new char[(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)];
|
---|
| 32 | lpDisplayBuf = (char*)LocalAlloc(LMEM_ZEROINIT,
|
---|
| 33 | (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
|
---|
| 34 |
|
---|
| 35 | #ifdef PLATFORM_SDK
|
---|
| 36 | StringCchPrintf((LPTSTR)lpDisplayBuf,
|
---|
| 37 | LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
---|
| 38 | TEXT("%s Failed with error %d: %s"),
|
---|
| 39 | lpszFunction, dw, lpMsgBuf);
|
---|
| 40 | #else
|
---|
| 41 | _stprintf(lpDisplayBuf, __TEXT("%s\nFailed with error %d: %s"), lpszFunction, dw, lpMsgBuf);
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
| 44 | MessageBox(NULL, lpDisplayBuf, TEXT("Error"), MB_OK);
|
---|
| 45 |
|
---|
| 46 | fprintf(stderr,"Cleaning up\n");
|
---|
| 47 |
|
---|
| 48 | LocalFree(lpMsgBuf);
|
---|
| 49 | LocalFree(lpDisplayBuf);
|
---|
| 50 | ExitProcess(dw);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // If using the following non-GUI method definition, need to launch
|
---|
| 54 | // the StartHttpd.exe program with: cmd /c
|
---|
| 55 | // (which would make it a detached process - with its own console)
|
---|
| 56 | // int _tmain( int argc, TCHAR *argv[] )
|
---|
| 57 |
|
---|
| 58 | extern "C" int WINAPI _tWinMain(HINSTANCE hinst, HINSTANCE hinstExePrev,
|
---|
| 59 | LPTSTR pszCmdLine, int nShowCmd)
|
---|
| 60 | {
|
---|
[20184] | 61 | if(__argc < 2 ||__argc > 3) {
|
---|
| 62 | MessageBox(NULL, __TEXT("Usage: stophttpd.exe <event-name> [SILENT]"),
|
---|
[19345] | 63 | TEXT("Error"), MB_OK);
|
---|
[20638] | 64 | return(-1);
|
---|
[19345] | 65 | }
|
---|
| 66 |
|
---|
| 67 | // open (get) the existing named event object that the StartHttpd.exe is listening for.
|
---|
| 68 | // The event object's name is what is given in the command line
|
---|
| 69 | HANDLE g_heventTerminateProcessGroup = OpenEvent(EVENT_ALL_ACCESS,TRUE, __argv[1]);
|
---|
| 70 |
|
---|
| 71 | // send the termination event
|
---|
| 72 | if (SetEvent(g_heventTerminateProcessGroup)==0) {
|
---|
[20184] | 73 | // no popup if in SILENT mode (3rd arg == silent or SILENT)
|
---|
| 74 | if(__argc != 3 || _stricmp(__argv[2], "SILENT\0") != 0) {
|
---|
| 75 | // do error popup ExitError
|
---|
| 76 |
|
---|
| 77 | ErrorExit(TEXT("Failed to send termination event: httpd.exe needs to be terminated manually."));
|
---|
| 78 | fprintf(stderr, "Failed to send termination event: httpd.exe needs to be terminated manually.\n");
|
---|
| 79 | }
|
---|
[19345] | 80 | }
|
---|
| 81 |
|
---|
| 82 | //Sleep(3000);
|
---|
| 83 |
|
---|
| 84 | return(0);
|
---|
| 85 | }
|
---|
| 86 |
|
---|