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. 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 {
  285. /* Password. */
  286. service->passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), WM_GETTEXTLENGTH, 0, 0);
  287. size_t passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), WM_GETTEXTLENGTH, 0, 0);
  288. if (! orig_service || ! orig_service->username || ! str_equiv(service->username, orig_service->username) || service->passwordlen || passwordlen) {
  289. if (! service->passwordlen) {
  290. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  291. return 6;
  292. }
  293. if (passwordlen != service->passwordlen) {
  294. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  295. return 6;
  296. }
  297. service->passwordlen++;
  298. /* Temporary buffer for password validation. */
  299. TCHAR *password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  300. if (! password) {
  301. HeapFree(GetProcessHeap(), 0, service->username);
  302. service->username = 0;
  303. service->usernamelen = 0;
  304. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password confirmation"), _T("install()"));
  305. return 6;
  306. }
  307. /* Actual password buffer. */
  308. service->password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  309. if (! service->password) {
  310. HeapFree(GetProcessHeap(), 0, password);
  311. HeapFree(GetProcessHeap(), 0, service->username);
  312. service->username = 0;
  313. service->usernamelen = 0;
  314. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password"), _T("install()"));
  315. return 6;
  316. }
  317. /* Get first password. */
  318. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1, service->password, (int) service->passwordlen)) {
  319. HeapFree(GetProcessHeap(), 0, password);
  320. SecureZeroMemory(service->password, service->passwordlen);
  321. HeapFree(GetProcessHeap(), 0, service->password);
  322. service->password = 0;
  323. service->passwordlen = 0;
  324. HeapFree(GetProcessHeap(), 0, service->username);
  325. service->username = 0;
  326. service->usernamelen = 0;
  327. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  328. return 6;
  329. }
  330. /* Get confirmation. */
  331. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2, password, (int) service->passwordlen)) {
  332. SecureZeroMemory(password, service->passwordlen);
  333. HeapFree(GetProcessHeap(), 0, password);
  334. SecureZeroMemory(service->password, service->passwordlen);
  335. HeapFree(GetProcessHeap(), 0, service->password);
  336. service->password = 0;
  337. service->passwordlen = 0;
  338. HeapFree(GetProcessHeap(), 0, service->username);
  339. service->username = 0;
  340. service->usernamelen = 0;
  341. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  342. return 6;
  343. }
  344. /* Compare. */
  345. if (_tcsncmp(password, service->password, service->passwordlen)) {
  346. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  347. SecureZeroMemory(password, service->passwordlen);
  348. HeapFree(GetProcessHeap(), 0, password);
  349. SecureZeroMemory(service->password, service->passwordlen);
  350. HeapFree(GetProcessHeap(), 0, service->password);
  351. service->password = 0;
  352. service->passwordlen = 0;
  353. HeapFree(GetProcessHeap(), 0, service->username);
  354. service->username = 0;
  355. service->usernamelen = 0;
  356. return 6;
  357. }
  358. }
  359. }
  360. }
  361. /* Remaining tabs are only for services we manage. */
  362. if (service->native) return 0;
  363. /* Get stop method stuff. */
  364. check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);
  365. check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);
  366. check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS);
  367. check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE);
  368. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay);
  369. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay);
  370. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay);
  371. /* Get exit action stuff. */
  372. check_number(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay);
  373. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  374. service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  375. if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;
  376. /* Get I/O stuff. */
  377. check_io(_T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);
  378. check_io(_T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);
  379. check_io(_T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);
  380. /* Override stdout and/or stderr. */
  381. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  382. if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS;
  383. if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS;
  384. }
  385. /* Get rotation stuff. */
  386. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  387. service->rotate_files = true;
  388. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);
  389. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);
  390. }
  391. /* Get environment. */
  392. unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0);
  393. if (envlen) {
  394. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR));
  395. if (! env) {
  396. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  397. cleanup_nssm_service(service);
  398. return 5;
  399. }
  400. if (! GetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, env, envlen + 1)) {
  401. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  402. HeapFree(GetProcessHeap(), 0, env);
  403. cleanup_nssm_service(service);
  404. return 5;
  405. }
  406. /* Strip CR and replace LF with NULL. */
  407. unsigned long newlen = 0;
  408. unsigned long i, j;
  409. for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++;
  410. /* Must end with two NULLs. */
  411. newlen += 2;
  412. TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));
  413. if (! newenv) {
  414. HeapFree(GetProcessHeap(), 0, env);
  415. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  416. cleanup_nssm_service(service);
  417. return 5;
  418. }
  419. for (i = 0, j = 0; i < envlen; i++) {
  420. if (env[i] == _T('\r')) continue;
  421. if (env[i] == _T('\n')) newenv[j] = _T('\0');
  422. else newenv[j] = env[i];
  423. j++;
  424. }
  425. HeapFree(GetProcessHeap(), 0, env);
  426. env = newenv;
  427. envlen = newlen;
  428. /* Test the environment is valid. */
  429. if (test_environment(env)) {
  430. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  431. HeapFree(GetProcessHeap(), 0, env);
  432. cleanup_nssm_service(service);
  433. return 5;
  434. }
  435. if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  436. service->env = env;
  437. service->envlen = envlen;
  438. }
  439. else {
  440. service->env_extra = env;
  441. service->env_extralen = envlen;
  442. }
  443. }
  444. return 0;
  445. }
  446. /* Install the service. */
  447. int install(HWND window) {
  448. if (! window) return 1;
  449. nssm_service_t *service = alloc_nssm_service();
  450. if (service) {
  451. int ret = configure(window, service, 0);
  452. if (ret) return ret;
  453. }
  454. /* See if it works. */
  455. switch (install_service(service)) {
  456. case 1:
  457. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()"));
  458. cleanup_nssm_service(service);
  459. return 1;
  460. case 2:
  461. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  462. cleanup_nssm_service(service);
  463. return 2;
  464. case 3:
  465. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  466. cleanup_nssm_service(service);
  467. return 3;
  468. case 4:
  469. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  470. cleanup_nssm_service(service);
  471. return 4;
  472. case 5:
  473. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  474. cleanup_nssm_service(service);
  475. return 5;
  476. case 6:
  477. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  478. cleanup_nssm_service(service);
  479. return 6;
  480. }
  481. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  482. cleanup_nssm_service(service);
  483. return 0;
  484. }
  485. /* Remove the service */
  486. int remove(HWND window) {
  487. if (! window) return 1;
  488. /* See if it works */
  489. nssm_service_t *service = alloc_nssm_service();
  490. if (service) {
  491. /* Get service name */
  492. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  493. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  494. cleanup_nssm_service(service);
  495. return 2;
  496. }
  497. /* Confirm */
  498. if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  499. cleanup_nssm_service(service);
  500. return 0;
  501. }
  502. }
  503. switch (remove_service(service)) {
  504. case 1:
  505. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()"));
  506. cleanup_nssm_service(service);
  507. return 1;
  508. case 2:
  509. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  510. cleanup_nssm_service(service);
  511. return 2;
  512. case 3:
  513. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  514. return 3;
  515. cleanup_nssm_service(service);
  516. case 4:
  517. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  518. cleanup_nssm_service(service);
  519. return 4;
  520. }
  521. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  522. cleanup_nssm_service(service);
  523. return 0;
  524. }
  525. int edit(HWND window, nssm_service_t *orig_service) {
  526. if (! window) return 1;
  527. nssm_service_t *service = alloc_nssm_service();
  528. if (service) {
  529. int ret = configure(window, service, orig_service);
  530. if (ret) return ret;
  531. }
  532. switch (edit_service(service, true)) {
  533. case 1:
  534. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("edit()"));
  535. cleanup_nssm_service(service);
  536. return 1;
  537. case 3:
  538. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  539. cleanup_nssm_service(service);
  540. return 3;
  541. case 4:
  542. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  543. cleanup_nssm_service(service);
  544. return 4;
  545. case 5:
  546. case 6:
  547. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_EDIT_PARAMETERS_FAILED);
  548. cleanup_nssm_service(service);
  549. return 6;
  550. }
  551. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_EDITED, service->name);
  552. cleanup_nssm_service(service);
  553. return 0;
  554. }
  555. static TCHAR *browse_filter(int message) {
  556. switch (message) {
  557. case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd");
  558. case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T(".");
  559. case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */
  560. default: return _T("*.*");
  561. }
  562. }
  563. UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) {
  564. switch (message) {
  565. case WM_INITDIALOG:
  566. return 1;
  567. }
  568. return 0;
  569. }
  570. /* Browse for application */
  571. void browse(HWND window, TCHAR *current, unsigned long flags, ...) {
  572. if (! window) return;
  573. va_list arg;
  574. size_t bufsize = 256;
  575. size_t len = bufsize;
  576. int i;
  577. OPENFILENAME ofn;
  578. ZeroMemory(&ofn, sizeof(ofn));
  579. ofn.lStructSize = sizeof(ofn);
  580. ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR));
  581. /* XXX: Escaping nulls with FormatMessage is tricky */
  582. if (ofn.lpstrFilter) {
  583. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  584. len = 0;
  585. /* "Applications" + NULL + "*.exe" + NULL */
  586. va_start(arg, flags);
  587. while (i = va_arg(arg, int)) {
  588. TCHAR *localised = message_string(i);
  589. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised);
  590. len += _tcslen(localised) + 1;
  591. LocalFree(localised);
  592. TCHAR *filter = browse_filter(i);
  593. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter);
  594. len += _tcslen(filter) + 1;
  595. }
  596. va_end(arg);
  597. /* Remainder of the buffer is already zeroed */
  598. }
  599. ofn.lpstrFile = new TCHAR[MAX_PATH];
  600. if (flags & OFN_NOVALIDATE) {
  601. /* Directory hack. */
  602. _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES));
  603. }
  604. else _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T("%s"), current);
  605. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  606. ofn.nMaxFile = MAX_PATH;
  607. ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags;
  608. if (GetOpenFileName(&ofn)) {
  609. /* Directory hack. */
  610. if (flags & OFN_NOVALIDATE) strip_basename(ofn.lpstrFile);
  611. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  612. }
  613. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  614. delete[] ofn.lpstrFile;
  615. }
  616. INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
  617. switch (message) {
  618. case WM_INITDIALOG:
  619. return 1;
  620. /* Button was pressed or control was controlled. */
  621. case WM_COMMAND:
  622. HWND dlg;
  623. TCHAR buffer[MAX_PATH];
  624. unsigned char enabled;
  625. switch (LOWORD(w)) {
  626. /* Browse for application. */
  627. case IDC_BROWSE:
  628. dlg = GetDlgItem(tab, IDC_PATH);
  629. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  630. browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  631. /* Fill in startup directory if it wasn't already specified. */
  632. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  633. if (! buffer[0]) {
  634. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  635. strip_basename(buffer);
  636. SetDlgItemText(tab, IDC_DIR, buffer);
  637. }
  638. break;
  639. /* Browse for startup directory. */
  640. case IDC_BROWSE_DIR:
  641. dlg = GetDlgItem(tab, IDC_DIR);
  642. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  643. browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);
  644. break;
  645. /* Log on. */
  646. case IDC_LOCALSYSTEM:
  647. set_logon_enabled(0);
  648. break;
  649. case IDC_ACCOUNT:
  650. set_logon_enabled(1);
  651. break;
  652. /* Shutdown methods. */
  653. case IDC_METHOD_CONSOLE:
  654. set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE);
  655. break;
  656. case IDC_METHOD_WINDOW:
  657. set_timeout_enabled(LOWORD(w), IDC_KILL_WINDOW);
  658. break;
  659. case IDC_METHOD_THREADS:
  660. set_timeout_enabled(LOWORD(w), IDC_KILL_THREADS);
  661. break;
  662. /* Browse for stdin. */
  663. case IDC_BROWSE_STDIN:
  664. dlg = GetDlgItem(tab, IDC_STDIN);
  665. GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));
  666. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  667. break;
  668. /* Browse for stdout. */
  669. case IDC_BROWSE_STDOUT:
  670. dlg = GetDlgItem(tab, IDC_STDOUT);
  671. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  672. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  673. /* Fill in stderr if it wasn't already specified. */
  674. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  675. if (! buffer[0]) {
  676. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  677. SetDlgItemText(tab, IDC_STDERR, buffer);
  678. }
  679. break;
  680. /* Browse for stderr. */
  681. case IDC_BROWSE_STDERR:
  682. dlg = GetDlgItem(tab, IDC_STDERR);
  683. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  684. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  685. break;
  686. /* Rotation. */
  687. case IDC_ROTATE:
  688. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  689. else enabled = 0;
  690. set_rotation_enabled(enabled);
  691. break;
  692. }
  693. return 1;
  694. }
  695. return 0;
  696. }
  697. /* Install/remove dialogue callback */
  698. INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  699. nssm_service_t *service;
  700. switch (message) {
  701. /* Creating the dialogue */
  702. case WM_INITDIALOG:
  703. service = (nssm_service_t *) l;
  704. SetFocus(GetDlgItem(window, IDC_NAME));
  705. HWND tabs;
  706. HWND combo;
  707. tabs = GetDlgItem(window, IDC_TAB1);
  708. if (! tabs) return 0;
  709. /* Set up tabs. */
  710. TCITEM tab;
  711. ZeroMemory(&tab, sizeof(tab));
  712. tab.mask = TCIF_TEXT;
  713. selected_tab = 0;
  714. /* Application tab. */
  715. if (service->native) tab.pszText = message_string(NSSM_GUI_TAB_NATIVE);
  716. else tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION);
  717. tab.cchTextMax = (int) _tcslen(tab.pszText);
  718. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
  719. if (service->native) {
  720. tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
  721. EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);
  722. EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);
  723. }
  724. else tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
  725. ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
  726. /* Details tab. */
  727. tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
  728. tab.cchTextMax = (int) _tcslen(tab.pszText);
  729. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
  730. tablist[NSSM_TAB_DETAILS] = CreateDialog(0, MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
  731. ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
  732. /* Set defaults. */
  733. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  734. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC));
  735. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED));
  736. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL));
  737. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED));
  738. SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0);
  739. /* Logon tab. */
  740. tab.pszText = message_string(NSSM_GUI_TAB_LOGON);
  741. tab.cchTextMax = (int) _tcslen(tab.pszText);
  742. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);
  743. tablist[NSSM_TAB_LOGON] = CreateDialog(0, MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
  744. ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);
  745. /* Set defaults. */
  746. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  747. set_logon_enabled(0);
  748. /* Remaining tabs are only for services we manage. */
  749. if (service->native) return 1;
  750. /* Shutdown tab. */
  751. tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
  752. tab.cchTextMax = (int) _tcslen(tab.pszText);
  753. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
  754. tablist[NSSM_TAB_SHUTDOWN] = CreateDialog(0, MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
  755. ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
  756. /* Set defaults. */
  757. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  758. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, NSSM_KILL_CONSOLE_GRACE_PERIOD, 0);
  759. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_CHECKED, 0);
  760. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, NSSM_KILL_WINDOW_GRACE_PERIOD, 0);
  761. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_CHECKED, 0);
  762. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, NSSM_KILL_THREADS_GRACE_PERIOD, 0);
  763. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_CHECKED, 0);
  764. /* Restart tab. */
  765. tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
  766. tab.cchTextMax = (int) _tcslen(tab.pszText);
  767. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
  768. tablist[NSSM_TAB_EXIT] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
  769. ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
  770. /* Set defaults. */
  771. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, NSSM_RESET_THROTTLE_RESTART, 0);
  772. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  773. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_RESTART, (LPARAM) message_string(NSSM_GUI_EXIT_RESTART));
  774. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_IGNORE, (LPARAM) message_string(NSSM_GUI_EXIT_IGNORE));
  775. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));
  776. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));
  777. SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);
  778. /* I/O tab. */
  779. tab.pszText = message_string(NSSM_GUI_TAB_IO);
  780. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  781. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
  782. tablist[NSSM_TAB_IO] = CreateDialog(0, MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
  783. ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
  784. /* Rotation tab. */
  785. tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
  786. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  787. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
  788. tablist[NSSM_TAB_ROTATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
  789. ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
  790. /* Set defaults. */
  791. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);
  792. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);
  793. set_rotation_enabled(0);
  794. /* Environment tab. */
  795. tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
  796. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  797. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
  798. tablist[NSSM_TAB_ENVIRONMENT] = CreateDialog(0, MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
  799. ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
  800. return 1;
  801. /* Tab change. */
  802. case WM_NOTIFY:
  803. NMHDR *notification;
  804. notification = (NMHDR *) l;
  805. switch (notification->code) {
  806. case TCN_SELCHANGE:
  807. HWND tabs;
  808. int selection;
  809. tabs = GetDlgItem(window, IDC_TAB1);
  810. if (! tabs) return 0;
  811. selection = (int) SendMessage(tabs, TCM_GETCURSEL, 0, 0);
  812. if (selection != selected_tab) {
  813. ShowWindow(tablist[selected_tab], SW_HIDE);
  814. ShowWindow(tablist[selection], SW_SHOWDEFAULT);
  815. SetFocus(GetDlgItem(window, IDOK));
  816. selected_tab = selection;
  817. }
  818. return 1;
  819. }
  820. return 0;
  821. /* Button was pressed or control was controlled */
  822. case WM_COMMAND:
  823. switch (LOWORD(w)) {
  824. /* OK button */
  825. case IDOK:
  826. if ((int) GetWindowLongPtr(window, GWLP_USERDATA) == IDD_EDIT) {
  827. if (! edit(window, (nssm_service_t *) GetWindowLongPtr(window, DWLP_USER))) PostQuitMessage(0);
  828. }
  829. else if (! install(window)) PostQuitMessage(0);
  830. break;
  831. /* Cancel button */
  832. case IDCANCEL:
  833. DestroyWindow(window);
  834. break;
  835. /* Remove button */
  836. case IDC_REMOVE:
  837. if (! remove(window)) PostQuitMessage(0);
  838. break;
  839. }
  840. return 1;
  841. /* Window closing */
  842. case WM_CLOSE:
  843. DestroyWindow(window);
  844. return 0;
  845. case WM_DESTROY:
  846. PostQuitMessage(0);
  847. }
  848. return 0;
  849. }