Support starting processes with a console on Windows 10.
[nssm.git] / io.cpp
diff --git a/io.cpp b/io.cpp
index dd2641c..0ba7e18 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -305,6 +305,7 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign
 \r
 int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {\r
   if (! si) return 1;\r
+  bool inherit_handles = false;\r
 \r
   /* Allocate a new console so we get a fresh stdin, stdout and stderr. */\r
   alloc_console(service);\r
@@ -316,6 +317,8 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);\r
       return 2;\r
     }\r
+\r
+    inherit_handles = true;\r\r
   }\r
 \r
   /* stdout */\r
@@ -341,6 +344,8 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
     }\r
 \r
     if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_si"), _T("stdout"))) close_handle(&service->stdout_thread);\r
+\r
+    inherit_handles = true;\r\r
   }\r
 \r
   /* stderr */\r
@@ -379,26 +384,15 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
     }\r
 \r
     if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_si"), _T("stderr"))) close_handle(&service->stderr_thread);\r
+\r
+    inherit_handles = true;\r\r
   }\r
 \r
   /*\r
     We need to set the startup_info flags to make the new handles\r
     inheritable by the new process.\r
   */\r
-  si->dwFlags |= STARTF_USESTDHANDLES;\r
-\r
-  if (service->no_console) return 0;\r
-\r
-  /* Redirect other handles. */\r
-  if (! si->hStdInput) {\r
-    if (dup_handle(GetStdHandle(STD_INPUT_HANDLE), &si->hStdInput, _T("STD_INPUT_HANDLE"), _T("stdin"))) return 8;\r
-  }\r
-  if (! si->hStdOutput) {\r
-    if (dup_handle(GetStdHandle(STD_OUTPUT_HANDLE), &si->hStdOutput, _T("STD_OUTPUT_HANDLE"), _T("stdout"))) return 9;\r
-  }\r
-  if (! si->hStdError)  {\r
-    if (dup_handle(GetStdHandle(STD_ERROR_HANDLE), &si->hStdError, _T("STD_ERROR_HANDLE"), _T("stderr"))) return 10;\r
-  }\r
+  if (inherit_handles) si->dwFlags |= STARTF_USESTDHANDLES;\r
 \r
   return 0;\r
 }\r