gui.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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_environment(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. if (! requires_password(service->username)) {
  318. if (is_localsystem(service->username)) {
  319. HeapFree(GetProcessHeap(), 0, service->username);
  320. service->username = 0;
  321. service->usernamelen = 0;
  322. }
  323. else {
  324. TCHAR *canon = canonical_username(service->username);
  325. HeapFree(GetProcessHeap(), 0, service->username);
  326. service->username = 0;
  327. service->usernamelen = 0;
  328. if (canon) {
  329. service->usernamelen = _tcslen(canon) + 1;
  330. service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->usernamelen * sizeof(TCHAR));
  331. if (! service->username) {
  332. LocalFree(canon);
  333. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("install()"));
  334. return 6;
  335. }
  336. memmove(service->username, canon, service->usernamelen * sizeof(TCHAR));
  337. LocalFree(canon);
  338. }
  339. else return 6;
  340. }
  341. }
  342. else {
  343. /* Password. */
  344. service->passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), WM_GETTEXTLENGTH, 0, 0);
  345. size_t passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), WM_GETTEXTLENGTH, 0, 0);
  346. if (! orig_service || ! orig_service->username || ! str_equiv(service->username, orig_service->username) || service->passwordlen || passwordlen) {
  347. if (! service->passwordlen) {
  348. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  349. return 6;
  350. }
  351. if (passwordlen != service->passwordlen) {
  352. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  353. return 6;
  354. }
  355. service->passwordlen++;
  356. /* Temporary buffer for password validation. */
  357. TCHAR *password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  358. if (! password) {
  359. HeapFree(GetProcessHeap(), 0, service->username);
  360. service->username = 0;
  361. service->usernamelen = 0;
  362. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password confirmation"), _T("install()"));
  363. return 6;
  364. }
  365. /* Actual password buffer. */
  366. service->password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  367. if (! service->password) {
  368. HeapFree(GetProcessHeap(), 0, password);
  369. HeapFree(GetProcessHeap(), 0, service->username);
  370. service->username = 0;
  371. service->usernamelen = 0;
  372. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password"), _T("install()"));
  373. return 6;
  374. }
  375. /* Get first password. */
  376. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1, service->password, (int) service->passwordlen)) {
  377. HeapFree(GetProcessHeap(), 0, password);
  378. SecureZeroMemory(service->password, service->passwordlen);
  379. HeapFree(GetProcessHeap(), 0, service->password);
  380. service->password = 0;
  381. service->passwordlen = 0;
  382. HeapFree(GetProcessHeap(), 0, service->username);
  383. service->username = 0;
  384. service->usernamelen = 0;
  385. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  386. return 6;
  387. }
  388. /* Get confirmation. */
  389. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2, password, (int) service->passwordlen)) {
  390. SecureZeroMemory(password, service->passwordlen);
  391. HeapFree(GetProcessHeap(), 0, password);
  392. SecureZeroMemory(service->password, service->passwordlen);
  393. HeapFree(GetProcessHeap(), 0, service->password);
  394. service->password = 0;
  395. service->passwordlen = 0;
  396. HeapFree(GetProcessHeap(), 0, service->username);
  397. service->username = 0;
  398. service->usernamelen = 0;
  399. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  400. return 6;
  401. }
  402. /* Compare. */
  403. if (_tcsncmp(password, service->password, service->passwordlen)) {
  404. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  405. SecureZeroMemory(password, service->passwordlen);
  406. HeapFree(GetProcessHeap(), 0, password);
  407. SecureZeroMemory(service->password, service->passwordlen);
  408. HeapFree(GetProcessHeap(), 0, service->password);
  409. service->password = 0;
  410. service->passwordlen = 0;
  411. HeapFree(GetProcessHeap(), 0, service->username);
  412. service->username = 0;
  413. service->usernamelen = 0;
  414. return 6;
  415. }
  416. }
  417. }
  418. }
  419. /* Remaining tabs are only for services we manage. */
  420. if (service->native) return 0;
  421. /* Get process stuff. */
  422. combo = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_PRIORITY);
  423. service->priority = priority_index_to_constant((unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0));
  424. service->affinity = 0LL;
  425. if (! (SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY_ALL, BM_GETCHECK, 0, 0) & BST_CHECKED)) {
  426. HWND list = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY);
  427. int selected = (int) SendMessage(list, LB_GETSELCOUNT, 0, 0);
  428. int count = (int) SendMessage(list, LB_GETCOUNT, 0, 0);
  429. if (! selected) {
  430. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_WARN_AFFINITY_NONE);
  431. return 5;
  432. }
  433. else if (selected < count) {
  434. for (int i = 0; i < count; i++) {
  435. if (SendMessage(list, LB_GETSEL, i, 0)) service->affinity |= (1LL << (__int64) i);
  436. }
  437. }
  438. }
  439. if (SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_CONSOLE, BM_GETCHECK, 0, 0) & BST_CHECKED) service->no_console = 0;
  440. else service->no_console = 1;
  441. /* Get stop method stuff. */
  442. check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);
  443. check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);
  444. check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS);
  445. check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE);
  446. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay);
  447. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay);
  448. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay);
  449. /* Get exit action stuff. */
  450. check_number(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay);
  451. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  452. service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  453. if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;
  454. check_number(tablist[NSSM_TAB_EXIT], IDC_RESTART_DELAY, &service->restart_delay);
  455. /* Get I/O stuff. */
  456. check_io(window, _T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);
  457. check_io(window, _T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);
  458. check_io(window, _T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);
  459. /* Override stdout and/or stderr. */
  460. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  461. if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS;
  462. if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS;
  463. }
  464. /* Get rotation stuff. */
  465. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  466. service->rotate_files = true;
  467. 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;
  468. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);
  469. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);
  470. }
  471. /* Get environment. */
  472. unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0);
  473. if (envlen) {
  474. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR));
  475. if (! env) {
  476. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  477. cleanup_nssm_service(service);
  478. return 5;
  479. }
  480. if (! GetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, env, envlen + 1)) {
  481. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  482. HeapFree(GetProcessHeap(), 0, env);
  483. cleanup_nssm_service(service);
  484. return 5;
  485. }
  486. TCHAR *newenv;
  487. unsigned long newlen;
  488. if (unformat_environment(env, envlen, &newenv, &newlen)) {
  489. HeapFree(GetProcessHeap(), 0, env);
  490. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  491. cleanup_nssm_service(service);
  492. return 5;
  493. }
  494. HeapFree(GetProcessHeap(), 0, env);
  495. env = newenv;
  496. envlen = newlen;
  497. /* Test the environment is valid. */
  498. if (test_environment(env)) {
  499. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  500. HeapFree(GetProcessHeap(), 0, env);
  501. cleanup_nssm_service(service);
  502. return 5;
  503. }
  504. if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  505. service->env = env;
  506. service->envlen = envlen;
  507. }
  508. else {
  509. service->env_extra = env;
  510. service->env_extralen = envlen;
  511. }
  512. }
  513. return 0;
  514. }
  515. /* Install the service. */
  516. int install(HWND window) {
  517. if (! window) return 1;
  518. nssm_service_t *service = alloc_nssm_service();
  519. if (service) {
  520. int ret = configure(window, service, 0);
  521. if (ret) return ret;
  522. }
  523. /* See if it works. */
  524. switch (install_service(service)) {
  525. case 1:
  526. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()"));
  527. cleanup_nssm_service(service);
  528. return 1;
  529. case 2:
  530. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  531. cleanup_nssm_service(service);
  532. return 2;
  533. case 3:
  534. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  535. cleanup_nssm_service(service);
  536. return 3;
  537. case 4:
  538. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  539. cleanup_nssm_service(service);
  540. return 4;
  541. case 5:
  542. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  543. cleanup_nssm_service(service);
  544. return 5;
  545. case 6:
  546. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  547. cleanup_nssm_service(service);
  548. return 6;
  549. }
  550. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  551. cleanup_nssm_service(service);
  552. return 0;
  553. }
  554. /* Remove the service */
  555. int remove(HWND window) {
  556. if (! window) return 1;
  557. /* See if it works */
  558. nssm_service_t *service = alloc_nssm_service();
  559. if (service) {
  560. /* Get service name */
  561. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  562. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  563. cleanup_nssm_service(service);
  564. return 2;
  565. }
  566. /* Confirm */
  567. if (popup_message(window, MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  568. cleanup_nssm_service(service);
  569. return 0;
  570. }
  571. }
  572. switch (remove_service(service)) {
  573. case 1:
  574. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()"));
  575. cleanup_nssm_service(service);
  576. return 1;
  577. case 2:
  578. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  579. cleanup_nssm_service(service);
  580. return 2;
  581. case 3:
  582. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  583. return 3;
  584. cleanup_nssm_service(service);
  585. case 4:
  586. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  587. cleanup_nssm_service(service);
  588. return 4;
  589. }
  590. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  591. cleanup_nssm_service(service);
  592. return 0;
  593. }
  594. int edit(HWND window, nssm_service_t *orig_service) {
  595. if (! window) return 1;
  596. nssm_service_t *service = alloc_nssm_service();
  597. if (service) {
  598. int ret = configure(window, service, orig_service);
  599. if (ret) return ret;
  600. }
  601. switch (edit_service(service, true)) {
  602. case 1:
  603. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("edit()"));
  604. cleanup_nssm_service(service);
  605. return 1;
  606. case 3:
  607. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  608. cleanup_nssm_service(service);
  609. return 3;
  610. case 4:
  611. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  612. cleanup_nssm_service(service);
  613. return 4;
  614. case 5:
  615. case 6:
  616. popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_EDIT_PARAMETERS_FAILED);
  617. cleanup_nssm_service(service);
  618. return 6;
  619. }
  620. popup_message(window, MB_OK, NSSM_MESSAGE_SERVICE_EDITED, service->name);
  621. cleanup_nssm_service(service);
  622. return 0;
  623. }
  624. static TCHAR *browse_filter(int message) {
  625. switch (message) {
  626. case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd");
  627. case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T(".");
  628. case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */
  629. default: return _T("*.*");
  630. }
  631. }
  632. UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) {
  633. switch (message) {
  634. case WM_INITDIALOG:
  635. return 1;
  636. }
  637. return 0;
  638. }
  639. /* Browse for application */
  640. void browse(HWND window, TCHAR *current, unsigned long flags, ...) {
  641. if (! window) return;
  642. va_list arg;
  643. size_t bufsize = 256;
  644. size_t len = bufsize;
  645. int i;
  646. OPENFILENAME ofn;
  647. ZeroMemory(&ofn, sizeof(ofn));
  648. ofn.lStructSize = sizeof(ofn);
  649. ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR));
  650. /* XXX: Escaping nulls with FormatMessage is tricky */
  651. if (ofn.lpstrFilter) {
  652. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  653. len = 0;
  654. /* "Applications" + NULL + "*.exe" + NULL */
  655. va_start(arg, flags);
  656. while (i = va_arg(arg, int)) {
  657. TCHAR *localised = message_string(i);
  658. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised);
  659. len += _tcslen(localised) + 1;
  660. LocalFree(localised);
  661. TCHAR *filter = browse_filter(i);
  662. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter);
  663. len += _tcslen(filter) + 1;
  664. }
  665. va_end(arg);
  666. /* Remainder of the buffer is already zeroed */
  667. }
  668. ofn.lpstrFile = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, PATH_LENGTH * sizeof(TCHAR));
  669. if (ofn.lpstrFile) {
  670. if (flags & OFN_NOVALIDATE) {
  671. /* Directory hack. */
  672. _sntprintf_s(ofn.lpstrFile, PATH_LENGTH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES));
  673. ofn.nMaxFile = DIR_LENGTH;
  674. }
  675. else {
  676. _sntprintf_s(ofn.lpstrFile, PATH_LENGTH, _TRUNCATE, _T("%s"), current);
  677. ofn.nMaxFile = PATH_LENGTH;
  678. }
  679. }
  680. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  681. ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags;
  682. if (GetOpenFileName(&ofn)) {
  683. /* Directory hack. */
  684. if (flags & OFN_NOVALIDATE) strip_basename(ofn.lpstrFile);
  685. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  686. }
  687. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  688. if (ofn.lpstrFile) HeapFree(GetProcessHeap(), 0, ofn.lpstrFile);
  689. }
  690. INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
  691. switch (message) {
  692. case WM_INITDIALOG:
  693. return 1;
  694. /* Button was pressed or control was controlled. */
  695. case WM_COMMAND:
  696. HWND dlg;
  697. TCHAR buffer[PATH_LENGTH];
  698. unsigned char enabled;
  699. switch (LOWORD(w)) {
  700. /* Browse for application. */
  701. case IDC_BROWSE:
  702. dlg = GetDlgItem(tab, IDC_PATH);
  703. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  704. browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  705. /* Fill in startup directory if it wasn't already specified. */
  706. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  707. if (! buffer[0]) {
  708. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  709. strip_basename(buffer);
  710. SetDlgItemText(tab, IDC_DIR, buffer);
  711. }
  712. break;
  713. /* Browse for startup directory. */
  714. case IDC_BROWSE_DIR:
  715. dlg = GetDlgItem(tab, IDC_DIR);
  716. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  717. browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);
  718. break;
  719. /* Log on. */
  720. case IDC_LOCALSYSTEM:
  721. set_logon_enabled(0);
  722. break;
  723. case IDC_ACCOUNT:
  724. set_logon_enabled(1);
  725. break;
  726. /* Affinity. */
  727. case IDC_AFFINITY_ALL:
  728. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 0;
  729. else enabled = 1;
  730. set_affinity_enabled(enabled);
  731. break;
  732. /* Shutdown methods. */
  733. case IDC_METHOD_CONSOLE:
  734. set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE);
  735. break;
  736. case IDC_METHOD_WINDOW:
  737. set_timeout_enabled(LOWORD(w), IDC_KILL_WINDOW);
  738. break;
  739. case IDC_METHOD_THREADS:
  740. set_timeout_enabled(LOWORD(w), IDC_KILL_THREADS);
  741. break;
  742. /* Browse for stdin. */
  743. case IDC_BROWSE_STDIN:
  744. dlg = GetDlgItem(tab, IDC_STDIN);
  745. GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));
  746. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  747. break;
  748. /* Browse for stdout. */
  749. case IDC_BROWSE_STDOUT:
  750. dlg = GetDlgItem(tab, IDC_STDOUT);
  751. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  752. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  753. /* Fill in stderr if it wasn't already specified. */
  754. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  755. if (! buffer[0]) {
  756. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  757. SetDlgItemText(tab, IDC_STDERR, buffer);
  758. }
  759. break;
  760. /* Browse for stderr. */
  761. case IDC_BROWSE_STDERR:
  762. dlg = GetDlgItem(tab, IDC_STDERR);
  763. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  764. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  765. break;
  766. /* Rotation. */
  767. case IDC_ROTATE:
  768. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  769. else enabled = 0;
  770. set_rotation_enabled(enabled);
  771. break;
  772. }
  773. return 1;
  774. }
  775. return 0;
  776. }
  777. /* Install/remove dialogue callback */
  778. INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  779. nssm_service_t *service;
  780. switch (message) {
  781. /* Creating the dialogue */
  782. case WM_INITDIALOG:
  783. service = (nssm_service_t *) l;
  784. SetFocus(GetDlgItem(window, IDC_NAME));
  785. HWND tabs;
  786. HWND combo;
  787. HWND list;
  788. int i, n;
  789. tabs = GetDlgItem(window, IDC_TAB1);
  790. if (! tabs) return 0;
  791. /* Set up tabs. */
  792. TCITEM tab;
  793. ZeroMemory(&tab, sizeof(tab));
  794. tab.mask = TCIF_TEXT;
  795. selected_tab = 0;
  796. /* Application tab. */
  797. if (service->native) tab.pszText = message_string(NSSM_GUI_TAB_NATIVE);
  798. else tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION);
  799. tab.cchTextMax = (int) _tcslen(tab.pszText);
  800. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
  801. if (service->native) {
  802. tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
  803. EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);
  804. EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);
  805. }
  806. else tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
  807. ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
  808. /* Details tab. */
  809. tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
  810. tab.cchTextMax = (int) _tcslen(tab.pszText);
  811. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
  812. tablist[NSSM_TAB_DETAILS] = dialog(MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
  813. ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
  814. /* Set defaults. */
  815. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  816. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC));
  817. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED));
  818. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL));
  819. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED));
  820. SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0);
  821. /* Logon tab. */
  822. tab.pszText = message_string(NSSM_GUI_TAB_LOGON);
  823. tab.cchTextMax = (int) _tcslen(tab.pszText);
  824. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);
  825. tablist[NSSM_TAB_LOGON] = dialog(MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
  826. ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);
  827. /* Set defaults. */
  828. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  829. set_logon_enabled(0);
  830. /* Remaining tabs are only for services we manage. */
  831. if (service->native) return 1;
  832. /* Process tab. */
  833. tab.pszText = message_string(NSSM_GUI_TAB_PROCESS);
  834. tab.cchTextMax = (int) _tcslen(tab.pszText);
  835. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_PROCESS, (LPARAM) &tab);
  836. tablist[NSSM_TAB_PROCESS] = dialog(MAKEINTRESOURCE(IDD_PROCESS), window, tab_dlg);
  837. ShowWindow(tablist[NSSM_TAB_PROCESS], SW_HIDE);
  838. /* Set defaults. */
  839. combo = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_PRIORITY);
  840. SendMessage(combo, CB_INSERTSTRING, NSSM_REALTIME_PRIORITY, (LPARAM) message_string(NSSM_GUI_REALTIME_PRIORITY_CLASS));
  841. SendMessage(combo, CB_INSERTSTRING, NSSM_HIGH_PRIORITY, (LPARAM) message_string(NSSM_GUI_HIGH_PRIORITY_CLASS));
  842. SendMessage(combo, CB_INSERTSTRING, NSSM_ABOVE_NORMAL_PRIORITY, (LPARAM) message_string(NSSM_GUI_ABOVE_NORMAL_PRIORITY_CLASS));
  843. SendMessage(combo, CB_INSERTSTRING, NSSM_NORMAL_PRIORITY, (LPARAM) message_string(NSSM_GUI_NORMAL_PRIORITY_CLASS));
  844. SendMessage(combo, CB_INSERTSTRING, NSSM_BELOW_NORMAL_PRIORITY, (LPARAM) message_string(NSSM_GUI_BELOW_NORMAL_PRIORITY_CLASS));
  845. SendMessage(combo, CB_INSERTSTRING, NSSM_IDLE_PRIORITY, (LPARAM) message_string(NSSM_GUI_IDLE_PRIORITY_CLASS));
  846. SendMessage(combo, CB_SETCURSEL, NSSM_NORMAL_PRIORITY, 0);
  847. SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  848. list = GetDlgItem(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY);
  849. n = num_cpus();
  850. SendMessage(list, LB_SETCOLUMNWIDTH, 16, 0);
  851. for (i = 0; i < n; i++) {
  852. TCHAR buffer[3];
  853. _sntprintf_s(buffer, _countof(buffer), _TRUNCATE, _T("%d"), i);
  854. SendMessage(list, LB_ADDSTRING, 0, (LPARAM) buffer);
  855. }
  856. /*
  857. Size to fit.
  858. The box is high enough for four rows. It is wide enough for eight
  859. columns without scrolling. With scrollbars it shrinks to two rows.
  860. Note that the above only holds if we set the column width BEFORE
  861. adding the strings.
  862. */
  863. if (n < 32) {
  864. int columns = (n - 1) / 4;
  865. RECT rect;
  866. GetWindowRect(list, &rect);
  867. int width = rect.right - rect.left;
  868. width -= (7 - columns) * 16;
  869. int height = rect.bottom - rect.top;
  870. if (n < 4) height -= (int) SendMessage(list, LB_GETITEMHEIGHT, 0, 0) * (4 - n);
  871. SetWindowPos(list, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOOWNERZORDER);
  872. }
  873. SendMessage(list, LB_SELITEMRANGE, 1, MAKELPARAM(0, n));
  874. SendDlgItemMessage(tablist[NSSM_TAB_PROCESS], IDC_AFFINITY_ALL, BM_SETCHECK, BST_CHECKED, 0);
  875. set_affinity_enabled(0);
  876. /* Shutdown tab. */
  877. tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
  878. tab.cchTextMax = (int) _tcslen(tab.pszText);
  879. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
  880. tablist[NSSM_TAB_SHUTDOWN] = dialog(MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
  881. ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
  882. /* Set defaults. */
  883. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  884. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, NSSM_KILL_CONSOLE_GRACE_PERIOD, 0);
  885. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_CHECKED, 0);
  886. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, NSSM_KILL_WINDOW_GRACE_PERIOD, 0);
  887. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_CHECKED, 0);
  888. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, NSSM_KILL_THREADS_GRACE_PERIOD, 0);
  889. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_CHECKED, 0);
  890. /* Restart tab. */
  891. tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
  892. tab.cchTextMax = (int) _tcslen(tab.pszText);
  893. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
  894. tablist[NSSM_TAB_EXIT] = dialog(MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
  895. ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
  896. /* Set defaults. */
  897. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, NSSM_RESET_THROTTLE_RESTART, 0);
  898. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  899. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_RESTART, (LPARAM) message_string(NSSM_GUI_EXIT_RESTART));
  900. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_IGNORE, (LPARAM) message_string(NSSM_GUI_EXIT_IGNORE));
  901. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));
  902. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));
  903. SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);
  904. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_RESTART_DELAY, 0, 0);
  905. /* I/O tab. */
  906. tab.pszText = message_string(NSSM_GUI_TAB_IO);
  907. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  908. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
  909. tablist[NSSM_TAB_IO] = dialog(MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
  910. ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
  911. /* Rotation tab. */
  912. tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
  913. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  914. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
  915. tablist[NSSM_TAB_ROTATION] = dialog(MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
  916. ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
  917. /* Set defaults. */
  918. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_SETCHECK, BST_UNCHECKED, 0);
  919. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);
  920. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);
  921. set_rotation_enabled(0);
  922. /* Environment tab. */
  923. tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
  924. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  925. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
  926. tablist[NSSM_TAB_ENVIRONMENT] = dialog(MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
  927. ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
  928. return 1;
  929. /* Tab change. */
  930. case WM_NOTIFY:
  931. NMHDR *notification;
  932. notification = (NMHDR *) l;
  933. switch (notification->code) {
  934. case TCN_SELCHANGE:
  935. HWND tabs;
  936. int selection;
  937. tabs = GetDlgItem(window, IDC_TAB1);
  938. if (! tabs) return 0;
  939. selection = (int) SendMessage(tabs, TCM_GETCURSEL, 0, 0);
  940. if (selection != selected_tab) {
  941. ShowWindow(tablist[selected_tab], SW_HIDE);
  942. ShowWindow(tablist[selection], SW_SHOWDEFAULT);
  943. SetFocus(GetDlgItem(window, IDOK));
  944. selected_tab = selection;
  945. }
  946. return 1;
  947. }
  948. return 0;
  949. /* Button was pressed or control was controlled */
  950. case WM_COMMAND:
  951. switch (LOWORD(w)) {
  952. /* OK button */
  953. case IDOK:
  954. if ((int) GetWindowLongPtr(window, GWLP_USERDATA) == IDD_EDIT) {
  955. if (! edit(window, (nssm_service_t *) GetWindowLongPtr(window, DWLP_USER))) PostQuitMessage(0);
  956. }
  957. else if (! install(window)) PostQuitMessage(0);
  958. break;
  959. /* Cancel button */
  960. case IDCANCEL:
  961. DestroyWindow(window);
  962. break;
  963. /* Remove button */
  964. case IDC_REMOVE:
  965. if (! remove(window)) PostQuitMessage(0);
  966. break;
  967. }
  968. return 1;
  969. /* Window closing */
  970. case WM_CLOSE:
  971. DestroyWindow(window);
  972. return 0;
  973. case WM_DESTROY:
  974. PostQuitMessage(0);
  975. }
  976. return 0;
  977. }