gui.cpp 41 KB

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