gui.cpp 57 KB

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