X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=43faa4143278f7d1d5a8c11d8cf540f345027892;hb=4b7c8eec508cc90d80355fad5df80163a181ddcb;hp=86a18275328f168a91e31c2103456b69ca5097b9;hpb=5b9e64a9ae1fbf1254c9c246e5b123d3aa77a37a;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 86a1827..43faa41 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -12,6 +12,17 @@ int str_equiv(const TCHAR *a, const TCHAR *b) { return 1; } +/* Convert a string to a number. */ +int str_number(const TCHAR *string, unsigned long *number) { + if (! string) return 1; + + TCHAR *bogus; + *number = _tcstoul(string, &bogus, 0); + if (*bogus) return 2; + + return 0; +} + /* Remove basename of a path. */ void strip_basename(TCHAR *buffer) { size_t len = _tcslen(buffer); @@ -24,7 +35,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(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE); return(ret); } @@ -39,13 +51,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, get, set, reset, unset or remove */ if (str_equiv(argv[1], _T("install"))) { if (! is_admin) { print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL); @@ -53,6 +87,16 @@ int _tmain(int argc, TCHAR **argv) { } exit(pre_install_service(argc - 2, argv + 2)); } + if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) { + if (! is_admin) { + print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT); + exit(100); + } + int ret = pre_edit_service(argc - 1, argv + 1); + /* There might be a password here. */ + for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR)); + exit(ret); + } if (str_equiv(argv[1], _T("remove"))) { if (! is_admin) { print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);