gui.cpp 39 KB

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