gui.cpp 48 KB

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