gui.cpp 38 KB

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