X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=gui.cpp;h=80d74ef7b97167b1ac10fe2d90a4bc6671ccb599;hb=6a12d5bf24211cfee30c47354b32befc3abae5b8;hp=9c5c57d74c9fffdecd0b254704f213527538dc1c;hpb=1d02256aa5bba3f3fd752c199adf77141428445f;p=nssm.git diff --git a/gui.cpp b/gui.cpp index 9c5c57d..80d74ef 100644 --- a/gui.cpp +++ b/gui.cpp @@ -1,10 +1,10 @@ #include "nssm.h" -static enum { NSSM_TAB_APPLICATION, NSSM_TAB_SHUTDOWN, NSSM_TAB_EXIT, NSSM_TAB_IO, NSSM_TAB_ENVIRONMENT, NSSM_NUM_TABS }; +static enum { NSSM_TAB_APPLICATION, NSSM_TAB_DETAILS, NSSM_TAB_LOGON, NSSM_TAB_SHUTDOWN, NSSM_TAB_EXIT, NSSM_TAB_IO, NSSM_TAB_ROTATION, NSSM_TAB_ENVIRONMENT, NSSM_NUM_TABS }; static HWND tablist[NSSM_NUM_TABS]; static int selected_tab; -int nssm_gui(int resource, char *name) { +int nssm_gui(int resource, TCHAR *name) { /* Create window */ HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg); if (! dlg) { @@ -49,7 +49,7 @@ void centre_window(HWND window) { /* Find window size */ if (! GetWindowRect(window, &size)) return; - + /* Find desktop window */ desktop = GetDesktopWindow(); if (! desktop) return; @@ -68,17 +68,34 @@ static inline void check_stop_method(nssm_service_t *service, unsigned long meth service->stop_method &= ~method; } -static inline void check_method_timeout(HWND tab, unsigned long control, unsigned long *timeout) { +static inline void check_number(HWND tab, unsigned long control, unsigned long *timeout) { BOOL translated; unsigned long configured = GetDlgItemInt(tab, control, &translated, 0); if (translated) *timeout = configured; } -static inline void check_io(char *name, char *buffer, size_t bufsize, unsigned long control) { +static inline void set_timeout_enabled(unsigned long control, unsigned long dependent) { + unsigned char enabled = 0; + if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1; + EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], dependent), enabled); +} + +static inline void set_logon_enabled(unsigned char enabled) { + EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), enabled); + EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), enabled); + EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), enabled); +} + +static inline void set_rotation_enabled(unsigned char enabled) { + EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), enabled); + EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), enabled); +} + +static inline void check_io(TCHAR *name, TCHAR *buffer, unsigned long len, unsigned long control) { if (! SendMessage(GetDlgItem(tablist[NSSM_TAB_IO], control), WM_GETTEXTLENGTH, 0, 0)) return; - if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) bufsize)) return; + if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) len)) return; popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, name); - ZeroMemory(buffer, bufsize); + ZeroMemory(buffer, len * sizeof(TCHAR)); } /* Install the service. */ @@ -90,63 +107,196 @@ int install(HWND window) { set_nssm_service_defaults(service); /* Get service name. */ - if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) { + if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) { popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME); cleanup_nssm_service(service); return 2; } /* Get executable name */ - if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, sizeof(service->exe))) { + if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, _countof(service->exe))) { popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH); return 3; } /* Get startup directory. */ - if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, sizeof(service->dir))) { - memmove(service->dir, service->exe, sizeof(service->dir)); + if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, _countof(service->dir))) { + _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe); strip_basename(service->dir); } /* Get flags. */ if (SendMessage(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) { - if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, sizeof(service->flags))) { + if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, _countof(service->flags))) { popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS); return 4; } } + /* Get details. */ + if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME), WM_GETTEXTLENGTH, 0, 0)) { + if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname, _countof(service->displayname))) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DISPLAYNAME); + return 5; + } + } + + if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION), WM_GETTEXTLENGTH, 0, 0)) { + if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description, _countof(service->description))) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DESCRIPTION); + return 5; + } + } + + HWND combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP); + service->startup = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0); + if (service->startup == CB_ERR) service->startup = 0; + + /* Get logon stuff. */ + if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, BM_GETCHECK, 0, 0) & BST_CHECKED) { + if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_INTERACT, BM_GETCHECK, 0, 0) & BST_CHECKED) { + service->type |= SERVICE_INTERACTIVE_PROCESS; + } + } + else { + /* Username. */ + service->usernamelen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), WM_GETTEXTLENGTH, 0, 0); + if (! service->usernamelen) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_USERNAME); + return 6; + } + service->usernamelen++; + + service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->usernamelen * sizeof(TCHAR)); + if (! service->username) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("account name"), _T("install()")); + return 6; + } + if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_USERNAME, service->username, (int) service->usernamelen)) { + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_USERNAME); + return 6; + } + + /* Password. */ + service->passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), WM_GETTEXTLENGTH, 0, 0); + if (! service->passwordlen) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD); + return 6; + } + if (SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), WM_GETTEXTLENGTH, 0, 0) != service->passwordlen) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD); + return 6; + } + service->passwordlen++; + + /* Temporary buffer for password validation. */ + TCHAR *password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR)); + if (! password) { + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password confirmation"), _T("install()")); + return 6; + } + + /* Actual password buffer. */ + service->password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR)); + if (! service->password) { + HeapFree(GetProcessHeap(), 0, password); + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password"), _T("install()")); + return 6; + } + + /* Get first password. */ + if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1, service->password, (int) service->passwordlen)) { + HeapFree(GetProcessHeap(), 0, password); + SecureZeroMemory(service->password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, service->password); + service->password = 0; + service->passwordlen = 0; + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD); + return 6; + } + + /* Get confirmation. */ + if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2, password, (int) service->passwordlen)) { + SecureZeroMemory(password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, password); + SecureZeroMemory(service->password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, service->password); + service->password = 0; + service->passwordlen = 0; + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD); + return 6; + } + + /* Compare. */ + if (_tcsncmp(password, service->password, service->passwordlen)) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD); + SecureZeroMemory(password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, password); + SecureZeroMemory(service->password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, service->password); + service->password = 0; + service->passwordlen = 0; + HeapFree(GetProcessHeap(), 0, service->username); + service->username = 0; + service->usernamelen = 0; + return 6; + } + } + /* Get stop method stuff. */ check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE); check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW); check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS); check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE); - check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay); - check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay); - check_method_timeout(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay); + check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay); + check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay); + check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay); /* Get exit action stuff. */ - check_method_timeout(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay); - HWND combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT); + check_number(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay); + combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT); service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0); if (service->default_exit_action == CB_ERR) service->default_exit_action = 0; /* Get I/O stuff. */ - check_io("stdin", service->stdin_path, sizeof(service->stdin_path), IDC_STDIN); - check_io("stdout", service->stdout_path, sizeof(service->stdout_path), IDC_STDOUT); - check_io("stderr", service->stderr_path, sizeof(service->stderr_path), IDC_STDERR); + check_io(_T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN); + check_io(_T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT); + check_io(_T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR); + /* Override stdout and/or stderr. */ - if (SendDlgItemMessage(tablist[NSSM_TAB_IO], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) { + if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) { if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS; if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS; } + /* Get rotation stuff. */ + if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) { + service->rotate_files = true; + check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds); + check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low); + } + /* Get environment. */ unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0); if (envlen) { - char *env = (char *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, envlen + 2); + TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR)); if (! env) { - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "environment", "install()"); + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()")); cleanup_nssm_service(service); return 5; } @@ -161,21 +311,21 @@ int install(HWND window) { /* Strip CR and replace LF with NULL. */ unsigned long newlen = 0; unsigned long i, j; - for (i = 0; i < envlen; i++) if (env[i] != '\r') newlen++; + for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++; /* Must end with two NULLs. */ newlen += 2; - char *newenv = (char *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen); + TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR)); if (! newenv) { HeapFree(GetProcessHeap(), 0, env); - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "environment", "install()"); + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()")); cleanup_nssm_service(service); return 5; } for (i = 0, j = 0; i < envlen; i++) { - if (env[i] == '\r') continue; - if (env[i] == '\n') newenv[j] = '\0'; + if (env[i] == _T('\r')) continue; + if (env[i] == _T('\n')) newenv[j] = _T('\0'); else newenv[j] = env[i]; j++; } @@ -185,25 +335,12 @@ int install(HWND window) { envlen = newlen; /* Test the environment is valid. */ - char path[MAX_PATH]; - GetModuleFileName(0, path, sizeof(path)); - STARTUPINFO si; - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - PROCESS_INFORMATION pi; - ZeroMemory(&pi, sizeof(pi)); - - if (! CreateProcess(0, path, 0, 0, 0, CREATE_SUSPENDED, env, 0, &si, &pi)) { - unsigned long error = GetLastError(); - if (error == ERROR_INVALID_PARAMETER) { - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT); - HeapFree(GetProcessHeap(), 0, env); - envlen = 0; - } + if (test_environment(env)) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT); + HeapFree(GetProcessHeap(), 0, env); cleanup_nssm_service(service); return 5; } - TerminateProcess(pi.hProcess, 0); if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) { service->env = env; @@ -219,7 +356,7 @@ int install(HWND window) { /* See if it works. */ switch (install_service(service)) { case 1: - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "install()"); + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()")); cleanup_nssm_service(service); return 1; @@ -262,7 +399,7 @@ int remove(HWND window) { nssm_service_t *service = alloc_nssm_service(); if (service) { /* Get service name */ - if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) { + if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) { popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME); cleanup_nssm_service(service); return 2; @@ -277,7 +414,7 @@ int remove(HWND window) { switch (remove_service(service)) { case 1: - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "remove()"); + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()")); cleanup_nssm_service(service); return 1; @@ -302,12 +439,12 @@ int remove(HWND window) { return 0; } -static char *browse_filter(int message) { +static TCHAR *browse_filter(int message) { switch (message) { - case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return "*.exe;*.bat;*.cmd"; - case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return "."; + case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd"); + case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T("."); case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */ - default: return "*.*"; + default: return _T("*.*"); } } @@ -321,7 +458,7 @@ UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) { } /* Browse for application */ -void browse(HWND window, char *current, unsigned long flags, ...) { +void browse(HWND window, TCHAR *current, unsigned long flags, ...) { if (! window) return; va_list arg; @@ -332,7 +469,7 @@ void browse(HWND window, char *current, unsigned long flags, ...) { OPENFILENAME ofn; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); - ofn.lpstrFilter = (char *) HeapAlloc(GetProcessHeap(), 0, bufsize); + ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR)); /* XXX: Escaping nulls with FormatMessage is tricky */ if (ofn.lpstrFilter) { ZeroMemory((void *) ofn.lpstrFilter, bufsize); @@ -340,23 +477,23 @@ void browse(HWND window, char *current, unsigned long flags, ...) { /* "Applications" + NULL + "*.exe" + NULL */ va_start(arg, flags); while (i = va_arg(arg, int)) { - char *localised = message_string(i); - _snprintf_s((char *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised); - len += strlen(localised) + 1; + TCHAR *localised = message_string(i); + _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised); + len += _tcslen(localised) + 1; LocalFree(localised); - char *filter = browse_filter(i); - _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "%s", filter); - len += strlen(filter) + 1; + TCHAR *filter = browse_filter(i); + _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter); + len += _tcslen(filter) + 1; } va_end(arg); /* Remainder of the buffer is already zeroed */ } - ofn.lpstrFile = new char[MAX_PATH]; + ofn.lpstrFile = new TCHAR[MAX_PATH]; if (flags & OFN_NOVALIDATE) { /* Directory hack. */ - _snprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, ":%s:", message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES)); + _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES)); } - else _snprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, "%s", current); + else _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T("%s"), current); ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE); ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags; @@ -379,46 +516,69 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) { /* Button was pressed or control was controlled. */ case WM_COMMAND: HWND dlg; - char buffer[MAX_PATH]; + TCHAR buffer[MAX_PATH]; + unsigned char enabled; switch (LOWORD(w)) { /* Browse for application. */ case IDC_BROWSE: dlg = GetDlgItem(tab, IDC_PATH); - GetDlgItemText(tab, IDC_PATH, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer)); browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0); /* Fill in startup directory if it wasn't already specified. */ - GetDlgItemText(tab, IDC_DIR, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer)); if (! buffer[0]) { - GetDlgItemText(tab, IDC_PATH, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer)); strip_basename(buffer); SetDlgItemText(tab, IDC_DIR, buffer); } break; - /* Browse for startup directory. */ + /* Browse for startup directory. */ case IDC_BROWSE_DIR: dlg = GetDlgItem(tab, IDC_DIR); - GetDlgItemText(tab, IDC_DIR, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer)); browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0); break; + /* Log on. */ + case IDC_LOCALSYSTEM: + set_logon_enabled(0); + break; + + case IDC_ACCOUNT: + set_logon_enabled(1); + break; + + /* Shutdown methods. */ + case IDC_METHOD_CONSOLE: + set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE); + break; + + case IDC_METHOD_WINDOW: + set_timeout_enabled(LOWORD(w), IDC_KILL_WINDOW); + break; + + case IDC_METHOD_THREADS: + set_timeout_enabled(LOWORD(w), IDC_KILL_THREADS); + break; + /* Browse for stdin. */ case IDC_BROWSE_STDIN: dlg = GetDlgItem(tab, IDC_STDIN); - GetDlgItemText(tab, IDC_STDIN, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer)); browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0); break; /* Browse for stdout. */ case IDC_BROWSE_STDOUT: dlg = GetDlgItem(tab, IDC_STDOUT); - GetDlgItemText(tab, IDC_STDOUT, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer)); browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0); /* Fill in stderr if it wasn't already specified. */ - GetDlgItemText(tab, IDC_STDERR, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer)); if (! buffer[0]) { - GetDlgItemText(tab, IDC_STDOUT, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer)); SetDlgItemText(tab, IDC_STDERR, buffer); } break; @@ -426,9 +586,16 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) { /* Browse for stderr. */ case IDC_BROWSE_STDERR: dlg = GetDlgItem(tab, IDC_STDERR); - GetDlgItemText(tab, IDC_STDERR, buffer, sizeof(buffer)); + GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer)); browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0); break; + + /* Rotation. */ + case IDC_ROTATE: + if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1; + else enabled = 0; + set_rotation_enabled(enabled); + break; } return 1; } @@ -455,14 +622,40 @@ INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) { /* Application tab. */ tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION); - tab.cchTextMax = (int) strlen(tab.pszText); + tab.cchTextMax = (int) _tcslen(tab.pszText); SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab); tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg); ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW); + /* Details tab. */ + tab.pszText = message_string(NSSM_GUI_TAB_DETAILS); + tab.cchTextMax = (int) _tcslen(tab.pszText); + SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab); + tablist[NSSM_TAB_DETAILS] = CreateDialog(0, MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg); + ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE); + + /* Set defaults. */ + combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP); + SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC)); + SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED)); + SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL)); + SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED)); + SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0); + + /* Logon tab. */ + tab.pszText = message_string(NSSM_GUI_TAB_LOGON); + tab.cchTextMax = (int) _tcslen(tab.pszText); + SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab); + tablist[NSSM_TAB_LOGON] = CreateDialog(0, MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg); + ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE); + + /* Set defaults. */ + CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM); + set_logon_enabled(0); + /* Shutdown tab. */ tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN); - tab.cchTextMax = (int) strlen(tab.pszText); + tab.cchTextMax = (int) _tcslen(tab.pszText); SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab); tablist[NSSM_TAB_SHUTDOWN] = CreateDialog(0, MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg); ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE); @@ -478,7 +671,7 @@ INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) { /* Restart tab. */ tab.pszText = message_string(NSSM_GUI_TAB_EXIT); - tab.cchTextMax = (int) strlen(tab.pszText); + tab.cchTextMax = (int) _tcslen(tab.pszText); SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab); tablist[NSSM_TAB_EXIT] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg); ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE); @@ -494,14 +687,26 @@ INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) { /* I/O tab. */ tab.pszText = message_string(NSSM_GUI_TAB_IO); - tab.cchTextMax = (int) strlen(tab.pszText) + 1; + tab.cchTextMax = (int) _tcslen(tab.pszText) + 1; SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab); tablist[NSSM_TAB_IO] = CreateDialog(0, MAKEINTRESOURCE(IDD_IO), window, tab_dlg); ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE); + /* Rotation tab. */ + tab.pszText = message_string(NSSM_GUI_TAB_ROTATION); + tab.cchTextMax = (int) _tcslen(tab.pszText) + 1; + SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab); + tablist[NSSM_TAB_ROTATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg); + ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE); + + /* Set defaults. */ + SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0); + SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0); + set_rotation_enabled(0); + /* Environment tab. */ tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT); - tab.cchTextMax = (int) strlen(tab.pszText) + 1; + tab.cchTextMax = (int) _tcslen(tab.pszText) + 1; SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab); tablist[NSSM_TAB_ENVIRONMENT] = CreateDialog(0, MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg); ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);