X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=7e1e7ceed027ad493a956b25c824dd46fc137049;hb=dd0e555ba889a74ae14487ca0b4a5c62cfa4d873;hp=7c956112065144957696bcc1e0dc6b85a2df413f;hpb=7aa403b0a0d8ecf1e59c9d4ab3709181941e2f4c;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index 7c95611..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,7 +40,29 @@ 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();