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