Detect if we were double-clicked.
[nssm.git] / nssm.cpp
index 7c95611..7e1e7ce 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(MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
   return(ret);\r
 }\r
 \r
@@ -39,7 +40,29 @@ 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