Browse Source

fixed service handling code

valenok 14 years ago
parent
commit
1aae5bbef9
1 changed files with 15 additions and 9 deletions
  1. 15 9
      main.c

+ 15 - 9
main.c

@@ -271,6 +271,7 @@ static void start_mongoose(int argc, char *argv[]) {
 #ifdef _WIN32
 #ifdef _WIN32
 static SERVICE_STATUS ss;
 static SERVICE_STATUS ss;
 static SERVICE_STATUS_HANDLE hStatus;
 static SERVICE_STATUS_HANDLE hStatus;
+static const char *service_magic_argument = "--";
 
 
 static void WINAPI ControlHandler(DWORD code) {
 static void WINAPI ControlHandler(DWORD code) {
   if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
   if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
@@ -346,7 +347,8 @@ static void show_error(void) {
 static int manage_service(int action) {
 static int manage_service(int action) {
   static const char *service_name = "Mongoose";
   static const char *service_name = "Mongoose";
   SC_HANDLE hSCM = NULL, hService = NULL;
   SC_HANDLE hSCM = NULL, hService = NULL;
-  char path[PATH_MAX];
+  SERVICE_DESCRIPTION descr = {server_name};
+  char path[PATH_MAX + 20];  // Path to executable plus magic argument
   int success = 1;
   int success = 1;
 
 
   if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
   if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
@@ -355,12 +357,14 @@ static int manage_service(int action) {
     show_error();
     show_error();
   } else if (action == ID_INSTALL_SERVICE) {
   } else if (action == ID_INSTALL_SERVICE) {
     GetModuleFileName(NULL, path, sizeof(path));
     GetModuleFileName(NULL, path, sizeof(path));
+    strncat(path, " ", sizeof(path));
+    strncat(path, service_magic_argument, sizeof(path));
     hService = CreateService(hSCM, service_name, service_name,
     hService = CreateService(hSCM, service_name, service_name,
                              SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
                              SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
                              SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                              SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                              path, NULL, NULL, NULL, NULL, NULL);
                              path, NULL, NULL, NULL, NULL, NULL);
     if (hService) {
     if (hService) {
-      ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, server_name);
+      ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
     } else {
     } else {
       show_error();
       show_error();
     }
     }
@@ -374,8 +378,8 @@ static int manage_service(int action) {
     success = 0;
     success = 0;
   }
   }
 
 
-  //CloseServiceHandle(hService);
-  //CloseServiceHandle(hSCM);
+  CloseServiceHandle(hService);
+  CloseServiceHandle(hSCM);
 
 
   return success;
   return success;
 }
 }
@@ -387,17 +391,19 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
     {NULL, NULL}
     {NULL, NULL}
   };
   };
   int service_installed;
   int service_installed;
-  char buf[200];
+  char buf[200], *service_argv[] = {__argv[0], NULL};
   POINT pt;
   POINT pt;
   HMENU hMenu;
   HMENU hMenu;
 
 
   switch (msg) {
   switch (msg) {
     case WM_CREATE:
     case WM_CREATE:
-      // Win32 runtime must prepare __argc and __argv for us
-      start_mongoose(__argc, __argv);
-      // TODO(lsm): figure out why this executes long time
-      if (StartServiceCtrlDispatcher(service_table)) {
+      if (__argv[1] != NULL &&
+          !strcmp(__argv[1], service_magic_argument)) {
+        start_mongoose(1, service_argv);
+        StartServiceCtrlDispatcher(service_table);
         exit(EXIT_SUCCESS);
         exit(EXIT_SUCCESS);
+      } else {
+        start_mongoose(__argc, __argv);
       }
       }
       break;
       break;
     case WM_COMMAND:
     case WM_COMMAND: