NSSM 2.0.
[nssm.git] / gui.cpp
diff --git a/gui.cpp b/gui.cpp
index c094657..5041acd 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -69,7 +69,6 @@ int install(HWND window) {
   char name[STRING_SIZE];\r
   char exe[MAX_PATH];\r
   char flags[STRING_SIZE];\r
-  char dir[MAX_PATH];\r
 \r
   /* Get service name */\r
   if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {\r
@@ -92,56 +91,28 @@ int install(HWND window) {
   }\r
   else ZeroMemory(&flags, sizeof(flags));\r
 \r
-  /* Work out directory name */\r
-  unsigned int len = strlen(exe);\r
-  unsigned int i;\r
-  for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);\r
-  memmove(dir, exe, i);\r
-  dir[i] = '\0';\r
-\r
-  /* Open service manager */\r
-  SC_HANDLE services = open_service_manager();\r
-  if (! services) {\r
-    MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
-    return 2;\r
-  }\r
-  \r
-  /* Get path of this program */\r
-  char path[MAX_PATH];\r
-  GetModuleFileName(0, path, MAX_PATH);\r
-\r
-  /* Construct command */\r
-  char command[MAX_PATH];\r
-  int runlen = strlen(NSSM_RUN);\r
-  int pathlen = strlen(path);\r
-  if (pathlen + runlen + 2 >= MAX_PATH) {\r
-    MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);\r
-    return 3;\r
-  }\r
-  if (snprintf(command, sizeof(command), "%s %s", path, NSSM_RUN) < 0) {\r
-     MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen.  You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);\r
-     return 4;\r
-  }\r
+  /* See if it works */\r
+  switch (install_service(name, exe, flags)) {\r
+    case 2:\r
+      MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
+      return 2;\r
 \r
-  /* Create the service */\r
-  SC_HANDLE service = CreateService(services, name, name, SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, command, 0, 0, 0, 0, 0);\r
-  if (! service) {\r
-    MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);\r
-    CloseServiceHandle(services);\r
-    return 5;\r
-  }\r
+    case 3:\r
+      MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);\r
+      return 3;\r
 \r
-  /* Now we need to put the parameters into the registry */\r
-  if (create_parameters(name, exe, flags, dir)) {\r
-    MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);\r
-    DeleteService(service);\r
-    CloseServiceHandle(services);\r
-    return 6;\r
-  }\r
+    case 4:\r
+      MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen.  You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);\r
+      return 4;\r
 \r
-  /* Cleanup */\r
-  CloseServiceHandle(service);\r
-  CloseServiceHandle(services);\r
+    case 5:\r
+      MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);\r
+      return 5;\r
+\r
+    case 6:\r
+      MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);\r
+      return 6;\r
+  }\r
 \r
   MessageBox(0, "Service successfully installed!", NSSM, MB_OK);\r
   return 0;\r
@@ -167,32 +138,20 @@ int remove(HWND window) {
   }\r
   else if (MessageBox(0, blurb, NSSM, MB_YESNO) != IDYES) return 0;\r
 \r
-  /* Open service manager */\r
-  SC_HANDLE services = open_service_manager();\r
-  if (! services) {\r
-    MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
-    return 2;\r
-  }\r
-  \r
-  /* Try to open the service */\r
-  SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);\r
-  if (! service) {\r
-    MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);\r
-    CloseServiceHandle(services);\r
-    return 3;\r
-  }\r
+  /* See if it works */\r
+  switch (remove_service(name)) {\r
+    case 2:\r
+      MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
+      return 2;\r
 \r
-  /* Try to delete the service */\r
-  if (! DeleteService(service)) {\r
-    MessageBox(0, "Can't delete service!  Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);\r
-    CloseServiceHandle(service);\r
-    CloseServiceHandle(services);\r
-    return 4;\r
-  }\r
+    case 3:\r
+      MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);\r
+      return 3;\r
 \r
-  /* Cleanup */\r
-  CloseServiceHandle(service);\r
-  CloseServiceHandle(services);\r
+    case 4:\r
+      MessageBox(0, "Can't delete service!  Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);\r
+      return 4;\r
+  }\r
 \r
   MessageBox(0, "Service successfully removed!", NSSM, MB_OK);\r
   return 0;\r