Be more Windowsy.
[nssm.git] / nssm.cpp
index 86a1827..02ad21f 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -24,7 +24,8 @@ void strip_basename(TCHAR *buffer) {
 \r
 /* How to use me correctly */\r
 int usage(int ret) {\r
-  print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
+  if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
+  else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
   return(ret);\r
 }\r
 \r
@@ -39,13 +40,35 @@ void check_admin() {
   FreeSid(AdministratorsGroup);\r
 }\r
 \r
+/* See if we were launched from a console window. */\r
+static void check_console() {\r
+  /* If we're running in a service context there will be no console window. */\r
+  HWND console = GetConsoleWindow();\r
+  if (! console) return;\r
+\r
+  unsigned long pid;\r
+  if (! GetWindowThreadProcessId(console, &pid)) return;\r
+\r
+  /*\r
+    If the process associated with the console window handle is the same as\r
+    this process, we were not launched from an existing console.  The user\r
+    probably double-clicked our executable.\r
+  */\r
+  if (GetCurrentProcessId() != pid) return;\r
+\r
+  /* We close our new console so that subsequent messages appear in a popup. */\r
+  FreeConsole();\r
+}\r
+\r
 int _tmain(int argc, TCHAR **argv) {\r
+  check_console();\r
+\r
   /* Remember if we are admin */\r
   check_admin();\r
 \r
   /* Elevate */\r
   if (argc > 1) {\r
-    /* Valid commands are install or remove */\r
+    /* Valid commands are install, edit or remove */\r
     if (str_equiv(argv[1], _T("install"))) {\r
       if (! is_admin) {\r
         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r
@@ -53,6 +76,13 @@ int _tmain(int argc, TCHAR **argv) {
       }\r
       exit(pre_install_service(argc - 2, argv + 2));\r
     }\r
+    if (str_equiv(argv[1], _T("edit"))) {\r
+      if (! is_admin) {\r
+        print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);\r
+        exit(100);\r
+      }\r
+      exit(pre_edit_service(argc - 2, argv + 2));\r
+    }\r
     if (str_equiv(argv[1], _T("remove"))) {\r
       if (! is_admin) {\r
         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);\r