Don't expand parameters when editing a service.
[nssm.git] / nssm.cpp
index 3c7545a..6456c97 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -55,26 +55,6 @@ 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 num_cpus() {\r
   DWORD_PTR i, affinity, system_affinity;\r
   if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;\r
@@ -85,6 +65,15 @@ int num_cpus() {
 int _tmain(int argc, TCHAR **argv) {\r
   check_console();\r
 \r
+#ifdef UNICODE\r
+  /*\r
+    Ensure we write in UTF-16 mode, so that non-ASCII characters don't get\r
+    mangled.  If we were compiled in ANSI mode it won't work.\r
+   */\r
+  _setmode(_fileno(stdout), _O_U16TEXT);\r
+  _setmode(_fileno(stderr), _O_U16TEXT);\r
+#endif\r
+\r
   /* Remember if we are admin */\r
   check_admin();\r
 \r
@@ -94,11 +83,17 @@ int _tmain(int argc, TCHAR **argv) {
       Valid commands are:\r
       start, stop, pause, continue, install, edit, get, set, reset, unset, remove\r
     */\r
-    if (str_equiv(argv[1], _T("start"))) exit(control_service(0, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("restart"))) {\r
+      int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2);\r
+      if (ret) exit(ret);\r
+      exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
+    }\r
     if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("install"))) {\r
       if (! is_admin) {\r
         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r