-int main(int argc, char **argv) {\r
- /* Require an argument since users may try to run nssm directly */\r
- if (argc == 1) exit(usage(1));\r
-\r
- /* Valid commands are install or remove */\r
- if (str_equiv(argv[1], "install")) exit(install_service(argv[2]));\r
- if (str_equiv(argv[1], "remove")) exit(remove_service(argv[2]));\r
- /* Undocumented: "run" is used to actually do service stuff */\r
- if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));\r
-\r
- /* Start service magic */\r
- SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
- if (! StartServiceCtrlDispatcher(table)) {\r
- char *message = error_string(GetLastError());\r
- eventprintf(EVENTLOG_ERROR_TYPE, "StartServiceCtrlDispatcher() failed: %s", message);\r
- if (message) LocalFree(message);\r
- return 100;\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, 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
+ exit(100);\r
+ }\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
+ exit(100);\r
+ }\r
+ exit(pre_remove_service(argc - 2, argv + 2));\r
+ }\r
+ }\r
+\r
+ /* Thread local storage for error message buffer */\r
+ tls_index = TlsAlloc();\r
+\r
+ /* Register messages */\r
+ if (is_admin) create_messages();\r
+\r
+ /*\r
+ Optimisation for Windows 2000:\r
+ When we're run from the command line the StartServiceCtrlDispatcher() call\r
+ will time out after a few seconds on Windows 2000. On newer versions the\r
+ call returns instantly. Check for stdin first and only try to call the\r
+ function if there's no input stream found. Although it's possible that\r
+ we're running with input redirected it's much more likely that we're\r
+ actually running as a service.\r
+ This will save time when running with no arguments from a command prompt.\r
+ */\r
+ if (_fileno(stdin) < 0) {\r
+ /* Set up function pointers. */\r
+ if (get_imports()) exit(111);\r
+\r
+ /* Start service magic */\r
+ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
+ if (! StartServiceCtrlDispatcher(table)) {\r
+ unsigned long error = GetLastError();\r
+ /* User probably ran nssm with no argument */\r
+ if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
+ free_imports();\r
+ exit(100);\r
+ }\r