2
0

gui.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. #include "nssm.h"
  2. 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 };
  3. static HWND tablist[NSSM_NUM_TABS];
  4. static int selected_tab;
  5. static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function, LPARAM l) {
  6. /* The caller will deal with GetLastError()... */
  7. HRSRC resource = FindResourceEx(0, RT_DIALOG, templ, GetUserDefaultLangID());
  8. if (! resource) {
  9. if (GetLastError() != ERROR_RESOURCE_LANG_NOT_FOUND) return 0;
  10. resource = FindResourceEx(0, RT_DIALOG, templ, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  11. if (! resource) return 0;
  12. }
  13. HGLOBAL ret = LoadResource(0, resource);
  14. if (! ret) return 0;
  15. return CreateDialogIndirectParam(0, (DLGTEMPLATE *) ret, parent, function, l);
  16. }
  17. static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function) {
  18. return dialog(templ, parent, function, 0);
  19. }
  20. int nssm_gui(int resource, nssm_service_t *service) {
  21. /* Create window */
  22. HWND dlg = dialog(MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);
  23. if (! dlg) {
  24. popup_message(0, MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
  25. return 1;
  26. }
  27. /* Load the icon. */
  28. HANDLE icon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_NSSM), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0);
  29. if (icon) SendMessage(dlg, WM_SETICON, ICON_SMALL, (LPARAM) icon);
  30. icon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_NSSM), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
  31. if (icon) SendMessage(dlg, WM_SETICON, ICON_BIG, (LPARAM) icon);
  32. /* Remember what the window is for. */
  33. SetWindowLongPtr(dlg, GWLP_USERDATA, (LONG_PTR) resource);
  34. /* Display the window */
  35. centre_window(dlg);
  36. ShowWindow(dlg, SW_SHOW);
  37. /* Set service name if given */
  38. if (service->name[0]) {
  39. SetDlgItemText(dlg, IDC_NAME, service->name);
  40. /* No point making user click remove if the name is already entered */
  41. if (resource == IDD_REMOVE) {
  42. HWND button = GetDlgItem(dlg, IDC_REMOVE);
  43. if (button) {
  44. SendMessage(button, WM_LBUTTONDOWN, 0, 0);
  45. SendMessage(button, WM_LBUTTONUP, 0, 0);
  46. }
  47. }
  48. }
  49. if (resource == IDD_EDIT) {
  50. /* We'll need the service handle later. */
  51. SetWindowLongPtr(dlg, DWLP_USER, (LONG_PTR) service);
  52. /* Service name can't be edited. */
  53. EnableWindow(GetDlgItem(dlg, IDC_NAME), 0);
  54. SetFocus(GetDlgItem(dlg, IDOK));
  55. /* Set existing details. */
  56. HWND combo;
  57. /* Application tab. */
  58. if (service->native) SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->image);
  59. else SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe);
  60. SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir);
  61. SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags);
  62. /* Details tab. */
  63. SetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname);
  64. SetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description);
  65. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  66. SendMessage(combo, CB_SETCURSEL, service->startup, 0);
  67. /* Log on tab. */
  68. if (service->username) {
  69. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_ACCOUNT);
  70. SetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_USERNAME, service->username);
  71. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_INTERACT), 0);
  72. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), 1);
  73. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), 1);
  74. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), 1);
  75. }
  76. else {
  77. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  78. if (service->type & SERVICE_INTERACTIVE_PROCESS) SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_INTERACT, BM_SETCHECK, BST_CHECKED, 0);
  79. }
  80. /* Shutdown tab. */
  81. if (! (service->stop_method & NSSM_STOP_METHOD_CONSOLE)) {
  82. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_UNCHECKED, 0);
  83. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE), 0);
  84. }
  85. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, service->kill_console_delay, 0);
  86. if (! (service->stop_method & NSSM_STOP_METHOD_WINDOW)) {
  87. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_UNCHECKED, 0);
  88. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW), 0);
  89. }
  90. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, service->kill_window_delay, 0);
  91. if (! (service->stop_method & NSSM_STOP_METHOD_THREADS)) {
  92. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_UNCHECKED, 0);
  93. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS), 0);
  94. }
  95. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, service->kill_threads_delay, 0);
  96. if (! (service->stop_method & NSSM_STOP_METHOD_TERMINATE)) {
  97. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_UNCHECKED, 0);
  98. }
  99. /* Restart tab. */
  100. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, service->throttle_delay, 0);
  101. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  102. SendMessage(combo, CB_SETCURSEL, service->default_exit_action, 0);
  103. /* I/O tab. */
  104. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDIN, service->stdin_path);
  105. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDOUT, service->stdout_path);
  106. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDERR, service->stderr_path);
  107. /* Rotation tab. */
  108. if (service->stdout_disposition == CREATE_ALWAYS) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_SETCHECK, BST_CHECKED, 0);
  109. if (service->rotate_files) {
  110. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_SETCHECK, BST_CHECKED, 0);
  111. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), 1);
  112. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), 1);
  113. }
  114. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, service->rotate_seconds, 0);
  115. if (! service->rotate_bytes_high) SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, service->rotate_bytes_low, 0);
  116. /* Check if advanced settings are in use. */
  117. if (service->stdout_disposition ^ service->stderr_disposition || service->stdout_disposition & ~CREATE_ALWAYS || service->stderr_disposition & ~CREATE_ALWAYS) popup_message(dlg, MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_STDIO);
  118. if (service->rotate_bytes_high) popup_message(dlg, MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ROTATE_BYTES);
  119. /* Environment tab. */
  120. TCHAR *env;
  121. unsigned long envlen;
  122. if (service->env_extralen) {
  123. env = service->env_extra;
  124. envlen = service->env_extralen;
  125. }
  126. else {
  127. env = service->env;
  128. envlen = service->envlen;
  129. if (envlen) SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_SETCHECK, BST_CHECKED, 0);
  130. }
  131. if (envlen) {
  132. TCHAR *formatted;
  133. unsigned long newlen;
  134. if (format_environment(env, envlen, &formatted, &newlen)) {
  135. popup_message(dlg, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));
  136. }
  137. else {
  138. SetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, formatted);
  139. HeapFree(GetProcessHeap(), 0, formatted);
  140. }
  141. }
  142. if (service->envlen && service->env_extralen) popup_message(dlg, MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ENVIRONMENT);
  143. }
  144. /* Go! */
  145. MSG message;
  146. while (GetMessage(&message, 0, 0, 0)) {
  147. if (IsDialogMessage(dlg, &message)) continue;
  148. TranslateMessage(&message);
  149. DispatchMessage(&message);
  150. }
  151. return (int) message.wParam;
  152. }
  153. void centre_window(HWND window) {
  154. HWND desktop;
  155. RECT size, desktop_size;
  156. unsigned long x, y;
  157. if (! window) return;
  158. /* Find window size */
  159. if (! GetWindowRect(window, &size)) return;
  160. /* Find desktop window */
  161. desktop = GetDesktopWindow();
  162. if (! desktop) return;
  163. /* Find desktop window size */
  164. if (! GetWindowRect(desktop, &desktop_size)) return;
  165. /* Centre window */
  166. x = (desktop_size.right - size.right) / 2;
  167. y = (desktop_size.bottom - size.bottom) / 2;
  168. MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
  169. }
  170. static inline void check_stop_method(nssm_service_t *service, unsigned long method, unsigned long control) {
  171. if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) return;
  172. service->stop_method &= ~method;
  173. }
  174. static inline void check_number(HWND tab, unsigned long control, unsigned long *timeout) {
  175. BOOL translated;
  176. unsigned long configured = GetDlgItemInt(tab, control, &translated, 0);
  177. if (translated) *timeout = configured;
  178. }
  179. static inline void set_timeout_enabled(unsigned long control, unsigned long dependent) {
  180. unsigned char enabled = 0;
  181. if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  182. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], dependent), enabled);
  183. }
  184. static inline void set_logon_enabled(unsigned char enabled) {
  185. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_INTERACT), ! enabled);
  186. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), enabled);
  187. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), enabled);
  188. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), enabled);
  189. }
  190. static inline void set_rotation_enabled(unsigned char enabled) {
  191. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), enabled);
  192. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), enabled);
  193. }
  194. static inline void check_io(HWND owner, TCHAR *name, TCHAR *buffer, unsigned long len, unsigned long control) {
  195. if (! SendMessage(GetDlgItem(tablist[NSSM_TAB_IO], control), WM_GETTEXTLENGTH, 0, 0)) return;
  196. if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) len)) return;
  197. popup_message(owner, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, name);
  198. ZeroMemory(buffer, len * sizeof(TCHAR));
  199. }
  200. /* Set service parameters. */
  201. int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service) {
  202. if (! service) return 1;
  203. set_nssm_service_defaults(service);
  204. if (orig_service) {
  205. service->native = orig_service->native;
  206. service->handle = orig_service->handle;
  207. }
  208. /* Get service name. */
  209. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  210. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  211. cleanup_nssm_service(service);
  212. return 2;
  213. }
  214. /* Get executable name */
  215. if (! service->native) {
  216. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, _countof(service->exe))) {
  217. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
  218. return 3;
  219. }
  220. /* Get startup directory. */
  221. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, _countof(service->dir))) {
  222. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  223. strip_basename(service->dir);
  224. }
  225. /* Get flags. */
  226. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
  227. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, _countof(service->flags))) {
  228. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
  229. return 4;
  230. }
  231. }
  232. }
  233. /* Get details. */
  234. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME), WM_GETTEXTLENGTH, 0, 0)) {
  235. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname, _countof(service->displayname))) {
  236. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DISPLAYNAME);
  237. return 5;
  238. }
  239. }
  240. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION), WM_GETTEXTLENGTH, 0, 0)) {
  241. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description, _countof(service->description))) {
  242. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DESCRIPTION);
  243. return 5;
  244. }
  245. }
  246. HWND combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  247. service->startup = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  248. if (service->startup == CB_ERR) service->startup = 0;
  249. /* Get logon stuff. */
  250. if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  251. if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_INTERACT, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  252. service->type |= SERVICE_INTERACTIVE_PROCESS;
  253. }
  254. if (service->username) HeapFree(GetProcessHeap(), 0, service->username);
  255. service->username = 0;
  256. service->usernamelen = 0;
  257. if (service->password) {
  258. SecureZeroMemory(service->password, service->passwordlen);
  259. HeapFree(GetProcessHeap(), 0, service->password);
  260. }
  261. service->password = 0;
  262. service->passwordlen = 0;
  263. }
  264. else {
  265. /* Username. */
  266. service->usernamelen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), WM_GETTEXTLENGTH, 0, 0);
  267. if (! service->usernamelen) {
  268. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_USERNAME);
  269. return 6;
  270. }
  271. service->usernamelen++;
  272. service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->usernamelen * sizeof(TCHAR));
  273. if (! service->username) {
  274. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("account name"), _T("install()"));
  275. return 6;
  276. }
  277. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_USERNAME, service->username, (int) service->usernamelen)) {
  278. HeapFree(GetProcessHeap(), 0, service->username);
  279. service->username = 0;
  280. service->usernamelen = 0;
  281. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_USERNAME);
  282. return 6;
  283. }
  284. /*
  285. Special case LOCALSYSTEM.
  286. Ignore the password if we're editing and the username hasn't changed.
  287. */
  288. if (str_equiv(service->username, NSSM_LOCALSYSTEM_ACCOUNT)) {
  289. HeapFree(GetProcessHeap(), 0, service->username);
  290. service->username = 0;
  291. service->usernamelen = 0;
  292. }
  293. else {
  294. /* Password. */
  295. service->passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), WM_GETTEXTLENGTH, 0, 0);
  296. size_t passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), WM_GETTEXTLENGTH, 0, 0);
  297. if (! orig_service || ! orig_service->username || ! str_equiv(service->username, orig_service->username) || service->passwordlen || passwordlen) {
  298. if (! service->passwordlen) {
  299. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  300. return 6;
  301. }
  302. if (passwordlen != service->passwordlen) {
  303. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  304. return 6;
  305. }
  306. service->passwordlen++;
  307. /* Temporary buffer for password validation. */
  308. TCHAR *password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  309. if (! password) {
  310. HeapFree(GetProcessHeap(), 0, service->username);
  311. service->username = 0;
  312. service->usernamelen = 0;
  313. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password confirmation"), _T("install()"));
  314. return 6;
  315. }
  316. /* Actual password buffer. */
  317. service->password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  318. if (! service->password) {
  319. HeapFree(GetProcessHeap(), 0, password);
  320. HeapFree(GetProcessHeap(), 0, service->username);
  321. service->username = 0;
  322. service->usernamelen = 0;
  323. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password"), _T("install()"));
  324. return 6;
  325. }
  326. /* Get first password. */
  327. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1, service->password, (int) service->passwordlen)) {
  328. HeapFree(GetProcessHeap(), 0, password);
  329. SecureZeroMemory(service->password, service->passwordlen);
  330. HeapFree(GetProcessHeap(), 0, service->password);
  331. service->password = 0;
  332. service->passwordlen = 0;
  333. HeapFree(GetProcessHeap(), 0, service->username);
  334. service->username = 0;
  335. service->usernamelen = 0;
  336. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  337. return 6;
  338. }
  339. /* Get confirmation. */
  340. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2, password, (int) service->passwordlen)) {
  341. SecureZeroMemory(password, service->passwordlen);
  342. HeapFree(GetProcessHeap(), 0, password);
  343. SecureZeroMemory(service->password, service->passwordlen);
  344. HeapFree(GetProcessHeap(), 0, service->password);
  345. service->password = 0;
  346. service->passwordlen = 0;
  347. HeapFree(GetProcessHeap(), 0, service->username);
  348. service->username = 0;
  349. service->usernamelen = 0;
  350. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  351. return 6;
  352. }
  353. /* Compare. */
  354. if (_tcsncmp(password, service->password, service->passwordlen)) {
  355. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  356. SecureZeroMemory(password, service->passwordlen);
  357. HeapFree(GetProcessHeap(), 0, password);
  358. SecureZeroMemory(service->password, service->passwordlen);
  359. HeapFree(GetProcessHeap(), 0, service->password);
  360. service->password = 0;
  361. service->passwordlen = 0;
  362. HeapFree(GetProcessHeap(), 0, service->username);
  363. service->username = 0;
  364. service->usernamelen = 0;
  365. return 6;
  366. }
  367. }
  368. }
  369. }
  370. /* Remaining tabs are only for services we manage. */
  371. if (service->native) return 0;
  372. /* Get stop method stuff. */
  373. check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);
  374. check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);
  375. check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS);
  376. check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE);
  377. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay);
  378. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay);
  379. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay);
  380. /* Get exit action stuff. */
  381. check_number(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay);
  382. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  383. service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  384. if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;
  385. /* Get I/O stuff. */
  386. check_io(window, _T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);
  387. check_io(window, _T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);
  388. check_io(window, _T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);
  389. /* Override stdout and/or stderr. */
  390. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  391. if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS;
  392. if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS;
  393. }
  394. /* Get rotation stuff. */
  395. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  396. service->rotate_files = true;
  397. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);
  398. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);
  399. }
  400. /* Get environment. */
  401. unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0);
  402. if (envlen) {
  403. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR));
  404. if (! env) {
  405. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  406. cleanup_nssm_service(service);
  407. return 5;
  408. }
  409. if (! GetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, env, envlen + 1)) {
  410. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  411. HeapFree(GetProcessHeap(), 0, env);
  412. cleanup_nssm_service(service);
  413. return 5;
  414. }
  415. TCHAR *newenv;
  416. unsigned long newlen;
  417. if (unformat_environment(env, envlen, &newenv, &newlen)) {
  418. HeapFree(GetProcessHeap(), 0, env);
  419. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  420. cleanup_nssm_service(service);
  421. return 5;
  422. }
  423. HeapFree(GetProcessHeap(), 0, env);
  424. env = newenv;
  425. envlen = newlen;
  426. /* Test the environment is valid. */
  427. if (test_environment(env)) {
  428. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  429. HeapFree(GetProcessHeap(), 0, env);
  430. cleanup_nssm_service(service);
  431. return 5;
  432. }
  433. if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  434. service->env = env;
  435. service->envlen = envlen;
  436. }
  437. else {
  438. service->env_extra = env;
  439. service->env_extralen = envlen;
  440. }
  441. }
  442. return 0;
  443. }
  444. /* Install the service. */
  445. int install(HWND window) {
  446. if (! window) return 1;
  447. nssm_service_t *service = alloc_nssm_service();
  448. if (service) {
  449. int ret = configure(window, service, 0);
  450. if (ret) return ret;
  451. }
  452. /* See if it works. */
  453. switch (install_service(service)) {
  454. case 1:
  455. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()"));
  456. cleanup_nssm_service(service);
  457. return 1;
  458. case 2:
  459. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  460. cleanup_nssm_service(service);
  461. return 2;
  462. case 3:
  463. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  464. cleanup_nssm_service(service);
  465. return 3;
  466. case 4:
  467. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  468. cleanup_nssm_service(service);
  469. return 4;
  470. case 5:
  471. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  472. cleanup_nssm_service(service);
  473. return 5;
  474. case 6:
  475. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  476. cleanup_nssm_service(service);
  477. return 6;
  478. }
  479. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  480. cleanup_nssm_service(service);
  481. return 0;
  482. }
  483. /* Remove the service */
  484. int remove(HWND window) {
  485. if (! window) return 1;
  486. /* See if it works */
  487. nssm_service_t *service = alloc_nssm_service();
  488. if (service) {
  489. /* Get service name */
  490. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  491. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  492. cleanup_nssm_service(service);
  493. return 2;
  494. }
  495. /* Confirm */
  496. if (popup_message(window, MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  497. cleanup_nssm_service(service);
  498. return 0;
  499. }
  500. }
  501. switch (remove_service(service)) {
  502. case 1:
  503. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()"));
  504. cleanup_nssm_service(service);
  505. return 1;
  506. case 2:
  507. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  508. cleanup_nssm_service(service);
  509. return 2;
  510. case 3:
  511. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  512. return 3;
  513. cleanup_nssm_service(service);
  514. case 4:
  515. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  516. cleanup_nssm_service(service);
  517. return 4;
  518. }
  519. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  520. cleanup_nssm_service(service);
  521. return 0;
  522. }
  523. int edit(HWND window, nssm_service_t *orig_service) {
  524. if (! window) return 1;
  525. nssm_service_t *service = alloc_nssm_service();
  526. if (service) {
  527. int ret = configure(window, service, orig_service);
  528. if (ret) return ret;
  529. }
  530. switch (edit_service(service, true)) {
  531. case 1:
  532. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("edit()"));
  533. cleanup_nssm_service(service);
  534. return 1;
  535. case 3:
  536. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  537. cleanup_nssm_service(service);
  538. return 3;
  539. case 4:
  540. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  541. cleanup_nssm_service(service);
  542. return 4;
  543. case 5:
  544. case 6:
  545. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_EDIT_PARAMETERS_FAILED);
  546. cleanup_nssm_service(service);
  547. return 6;
  548. }
  549. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_EDITED, service->name);
  550. cleanup_nssm_service(service);
  551. return 0;
  552. }
  553. static TCHAR *browse_filter(int message) {
  554. switch (message) {
  555. case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd");
  556. case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T(".");
  557. case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */
  558. default: return _T("*.*");
  559. }
  560. }
  561. UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) {
  562. switch (message) {
  563. case WM_INITDIALOG:
  564. return 1;
  565. }
  566. return 0;
  567. }
  568. /* Browse for application */
  569. void browse(HWND window, TCHAR *current, unsigned long flags, ...) {
  570. if (! window) return;
  571. va_list arg;
  572. size_t bufsize = 256;
  573. size_t len = bufsize;
  574. int i;
  575. OPENFILENAME ofn;
  576. ZeroMemory(&ofn, sizeof(ofn));
  577. ofn.lStructSize = sizeof(ofn);
  578. ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR));
  579. /* XXX: Escaping nulls with FormatMessage is tricky */
  580. if (ofn.lpstrFilter) {
  581. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  582. len = 0;
  583. /* "Applications" + NULL + "*.exe" + NULL */
  584. va_start(arg, flags);
  585. while (i = va_arg(arg, int)) {
  586. TCHAR *localised = message_string(i);
  587. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised);
  588. len += _tcslen(localised) + 1;
  589. LocalFree(localised);
  590. TCHAR *filter = browse_filter(i);
  591. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter);
  592. len += _tcslen(filter) + 1;
  593. }
  594. va_end(arg);
  595. /* Remainder of the buffer is already zeroed */
  596. }
  597. ofn.lpstrFile = new TCHAR[MAX_PATH];
  598. if (flags & OFN_NOVALIDATE) {
  599. /* Directory hack. */
  600. _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES));
  601. }
  602. else _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T("%s"), current);
  603. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  604. ofn.nMaxFile = MAX_PATH;
  605. ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags;
  606. if (GetOpenFileName(&ofn)) {
  607. /* Directory hack. */
  608. if (flags & OFN_NOVALIDATE) strip_basename(ofn.lpstrFile);
  609. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  610. }
  611. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  612. delete[] ofn.lpstrFile;
  613. }
  614. INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
  615. switch (message) {
  616. case WM_INITDIALOG:
  617. return 1;
  618. /* Button was pressed or control was controlled. */
  619. case WM_COMMAND:
  620. HWND dlg;
  621. TCHAR buffer[MAX_PATH];
  622. unsigned char enabled;
  623. switch (LOWORD(w)) {
  624. /* Browse for application. */
  625. case IDC_BROWSE:
  626. dlg = GetDlgItem(tab, IDC_PATH);
  627. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  628. browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  629. /* Fill in startup directory if it wasn't already specified. */
  630. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  631. if (! buffer[0]) {
  632. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  633. strip_basename(buffer);
  634. SetDlgItemText(tab, IDC_DIR, buffer);
  635. }
  636. break;
  637. /* Browse for startup directory. */
  638. case IDC_BROWSE_DIR:
  639. dlg = GetDlgItem(tab, IDC_DIR);
  640. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  641. browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);
  642. break;
  643. /* Log on. */
  644. case IDC_LOCALSYSTEM:
  645. set_logon_enabled(0);
  646. break;
  647. case IDC_ACCOUNT:
  648. set_logon_enabled(1);
  649. break;
  650. /* Shutdown methods. */
  651. case IDC_METHOD_CONSOLE:
  652. set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE);
  653. break;
  654. case IDC_METHOD_WINDOW:
  655. set_timeout_enabled(LOWORD(w), IDC_KILL_WINDOW);
  656. break;
  657. case IDC_METHOD_THREADS:
  658. set_timeout_enabled(LOWORD(w), IDC_KILL_THREADS);
  659. break;
  660. /* Browse for stdin. */
  661. case IDC_BROWSE_STDIN:
  662. dlg = GetDlgItem(tab, IDC_STDIN);
  663. GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));
  664. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  665. break;
  666. /* Browse for stdout. */
  667. case IDC_BROWSE_STDOUT:
  668. dlg = GetDlgItem(tab, IDC_STDOUT);
  669. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  670. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  671. /* Fill in stderr if it wasn't already specified. */
  672. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  673. if (! buffer[0]) {
  674. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  675. SetDlgItemText(tab, IDC_STDERR, buffer);
  676. }
  677. break;
  678. /* Browse for stderr. */
  679. case IDC_BROWSE_STDERR:
  680. dlg = GetDlgItem(tab, IDC_STDERR);
  681. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  682. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  683. break;
  684. /* Rotation. */
  685. case IDC_ROTATE:
  686. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  687. else enabled = 0;
  688. set_rotation_enabled(enabled);
  689. break;
  690. }
  691. return 1;
  692. }
  693. return 0;
  694. }
  695. /* Install/remove dialogue callback */
  696. INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  697. nssm_service_t *service;
  698. switch (message) {
  699. /* Creating the dialogue */
  700. case WM_INITDIALOG:
  701. service = (nssm_service_t *) l;
  702. SetFocus(GetDlgItem(window, IDC_NAME));
  703. HWND tabs;
  704. HWND combo;
  705. tabs = GetDlgItem(window, IDC_TAB1);
  706. if (! tabs) return 0;
  707. /* Set up tabs. */
  708. TCITEM tab;
  709. ZeroMemory(&tab, sizeof(tab));
  710. tab.mask = TCIF_TEXT;
  711. selected_tab = 0;
  712. /* Application tab. */
  713. if (service->native) tab.pszText = message_string(NSSM_GUI_TAB_NATIVE);
  714. else tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION);
  715. tab.cchTextMax = (int) _tcslen(tab.pszText);
  716. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
  717. if (service->native) {
  718. tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
  719. EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);
  720. EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);
  721. }
  722. else tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
  723. ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
  724. /* Details tab. */
  725. tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
  726. tab.cchTextMax = (int) _tcslen(tab.pszText);
  727. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
  728. tablist[NSSM_TAB_DETAILS] = dialog(MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
  729. ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
  730. /* Set defaults. */
  731. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  732. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC));
  733. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED));
  734. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL));
  735. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED));
  736. SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0);
  737. /* Logon tab. */
  738. tab.pszText = message_string(NSSM_GUI_TAB_LOGON);
  739. tab.cchTextMax = (int) _tcslen(tab.pszText);
  740. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);
  741. tablist[NSSM_TAB_LOGON] = dialog(MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
  742. ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);
  743. /* Set defaults. */
  744. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  745. set_logon_enabled(0);
  746. /* Remaining tabs are only for services we manage. */
  747. if (service->native) return 1;
  748. /* Shutdown tab. */
  749. tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
  750. tab.cchTextMax = (int) _tcslen(tab.pszText);
  751. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
  752. tablist[NSSM_TAB_SHUTDOWN] = dialog(MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
  753. ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
  754. /* Set defaults. */
  755. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  756. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, NSSM_KILL_CONSOLE_GRACE_PERIOD, 0);
  757. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_CHECKED, 0);
  758. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, NSSM_KILL_WINDOW_GRACE_PERIOD, 0);
  759. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_CHECKED, 0);
  760. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, NSSM_KILL_THREADS_GRACE_PERIOD, 0);
  761. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_CHECKED, 0);
  762. /* Restart tab. */
  763. tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
  764. tab.cchTextMax = (int) _tcslen(tab.pszText);
  765. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
  766. tablist[NSSM_TAB_EXIT] = dialog(MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
  767. ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
  768. /* Set defaults. */
  769. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, NSSM_RESET_THROTTLE_RESTART, 0);
  770. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  771. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_RESTART, (LPARAM) message_string(NSSM_GUI_EXIT_RESTART));
  772. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_IGNORE, (LPARAM) message_string(NSSM_GUI_EXIT_IGNORE));
  773. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));
  774. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));
  775. SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);
  776. /* I/O tab. */
  777. tab.pszText = message_string(NSSM_GUI_TAB_IO);
  778. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  779. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
  780. tablist[NSSM_TAB_IO] = dialog(MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
  781. ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
  782. /* Rotation tab. */
  783. tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
  784. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  785. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
  786. tablist[NSSM_TAB_ROTATION] = dialog(MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
  787. ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
  788. /* Set defaults. */
  789. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);
  790. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);
  791. set_rotation_enabled(0);
  792. /* Environment tab. */
  793. tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
  794. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  795. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
  796. tablist[NSSM_TAB_ENVIRONMENT] = dialog(MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
  797. ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
  798. return 1;
  799. /* Tab change. */
  800. case WM_NOTIFY:
  801. NMHDR *notification;
  802. notification = (NMHDR *) l;
  803. switch (notification->code) {
  804. case TCN_SELCHANGE:
  805. HWND tabs;
  806. int selection;
  807. tabs = GetDlgItem(window, IDC_TAB1);
  808. if (! tabs) return 0;
  809. selection = (int) SendMessage(tabs, TCM_GETCURSEL, 0, 0);
  810. if (selection != selected_tab) {
  811. ShowWindow(tablist[selected_tab], SW_HIDE);
  812. ShowWindow(tablist[selection], SW_SHOWDEFAULT);
  813. SetFocus(GetDlgItem(window, IDOK));
  814. selected_tab = selection;
  815. }
  816. return 1;
  817. }
  818. return 0;
  819. /* Button was pressed or control was controlled */
  820. case WM_COMMAND:
  821. switch (LOWORD(w)) {
  822. /* OK button */
  823. case IDOK:
  824. if ((int) GetWindowLongPtr(window, GWLP_USERDATA) == IDD_EDIT) {
  825. if (! edit(window, (nssm_service_t *) GetWindowLongPtr(window, DWLP_USER))) PostQuitMessage(0);
  826. }
  827. else if (! install(window)) PostQuitMessage(0);
  828. break;
  829. /* Cancel button */
  830. case IDCANCEL:
  831. DestroyWindow(window);
  832. break;
  833. /* Remove button */
  834. case IDC_REMOVE:
  835. if (! remove(window)) PostQuitMessage(0);
  836. break;
  837. }
  838. return 1;
  839. /* Window closing */
  840. case WM_CLOSE:
  841. DestroyWindow(window);
  842. return 0;
  843. case WM_DESTROY:
  844. PostQuitMessage(0);
  845. }
  846. return 0;
  847. }