gui.cpp 56 KB

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