source: gsdl/trunk/runtime-src/src/w32apachectl/stophttpd.cpp@ 19345

Last change on this file since 19345 was 19345, checked in by ak19, 15 years ago

New folder w32apachectl written by Dr Bainbridge to start and stop a Windows apache webserver (httpd.exe). Special start and stop required since launching httpd.exe starts two instances of itself, and getting the PID only terminates one of them (but not the child). Sending control-break only terminates the child which the parent httpd.exe instance then restarts. These starthttpd and stophttpd programs will be used for the GS2 Windows Apache webserver, so that they can be started and stopped from the server.jar code.

File size: 2.5 KB
Line 
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
12void 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
58extern "C" int WINAPI _tWinMain(HINSTANCE hinst, HINSTANCE hinstExePrev,
59 LPTSTR pszCmdLine, int nShowCmd)
60{
61 if(__argc != 2) {
62 MessageBox(NULL, __TEXT("Usage: stophttpd.exe <event-name>"),
63 TEXT("Error"), MB_OK);
64 return(1);
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) {
73 // do error popup ExitError
74
75 ErrorExit(TEXT("Failed to send termination event: httpd.exe needs to be terminated manually."));
76 fprintf(stderr, "Failed to send termination event: httpd.exe needs to be terminated manually.\n");
77 }
78
79 //Sleep(3000);
80
81 return(0);
82}
83
Note: See TracBrowser for help on using the repository browser.