gui.cpp 45 KB

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