gui.cpp 46 KB

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