gui.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #include "nssm.h"
  2. static enum { NSSM_TAB_APPLICATION, NSSM_TAB_DETAILS, NSSM_TAB_LOGON, NSSM_TAB_SHUTDOWN, NSSM_TAB_EXIT, NSSM_TAB_IO, NSSM_TAB_ROTATION, NSSM_TAB_ENVIRONMENT, NSSM_NUM_TABS };
  3. static HWND tablist[NSSM_NUM_TABS];
  4. static int selected_tab;
  5. static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function, LPARAM l) {
  6. /* The caller will deal with GetLastError()... */
  7. HRSRC resource = FindResourceEx(0, RT_DIALOG, templ, GetUserDefaultLangID());
  8. if (! resource) {
  9. if (GetLastError() != ERROR_RESOURCE_LANG_NOT_FOUND) return 0;
  10. resource = FindResourceEx(0, RT_DIALOG, templ, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  11. if (! resource) return 0;
  12. }
  13. HGLOBAL ret = LoadResource(0, resource);
  14. if (! ret) return 0;
  15. return CreateDialogIndirectParam(0, (DLGTEMPLATE *) ret, parent, function, l);
  16. }
  17. static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function) {
  18. return dialog(templ, parent, function, 0);
  19. }
  20. int nssm_gui(int resource, nssm_service_t *service) {
  21. /* Create window */
  22. HWND dlg = dialog(MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);
  23. if (! dlg) {
  24. popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
  25. return 1;
  26. }
  27. /* Remember what the window is for. */
  28. SetWindowLongPtr(dlg, GWLP_USERDATA, (LONG_PTR) resource);
  29. /* Display the window */
  30. centre_window(dlg);
  31. ShowWindow(dlg, SW_SHOW);
  32. /* Set service name if given */
  33. if (service->name[0]) {
  34. SetDlgItemText(dlg, IDC_NAME, service->name);
  35. /* No point making user click remove if the name is already entered */
  36. if (resource == IDD_REMOVE) {
  37. HWND button = GetDlgItem(dlg, IDC_REMOVE);
  38. if (button) {
  39. SendMessage(button, WM_LBUTTONDOWN, 0, 0);
  40. SendMessage(button, WM_LBUTTONUP, 0, 0);
  41. }
  42. }
  43. }
  44. if (resource == IDD_EDIT) {
  45. /* We'll need the service handle later. */
  46. SetWindowLongPtr(dlg, DWLP_USER, (LONG_PTR) service);
  47. /* Service name can't be edited. */
  48. EnableWindow(GetDlgItem(dlg, IDC_NAME), 0);
  49. SetFocus(GetDlgItem(dlg, IDOK));
  50. /* Set existing details. */
  51. HWND combo;
  52. /* Application tab. */
  53. if (service->native) SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->image);
  54. else SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe);
  55. SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir);
  56. SetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags);
  57. /* Details tab. */
  58. SetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname);
  59. SetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description);
  60. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  61. SendMessage(combo, CB_SETCURSEL, service->startup, 0);
  62. /* Log on tab. */
  63. if (service->username) {
  64. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_ACCOUNT);
  65. SetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_USERNAME, service->username);
  66. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_INTERACT), 0);
  67. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), 1);
  68. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), 1);
  69. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), 1);
  70. }
  71. else {
  72. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  73. if (service->type & SERVICE_INTERACTIVE_PROCESS) SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_INTERACT, BM_SETCHECK, BST_CHECKED, 0);
  74. }
  75. /* Shutdown tab. */
  76. if (! (service->stop_method & NSSM_STOP_METHOD_CONSOLE)) {
  77. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_UNCHECKED, 0);
  78. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE), 0);
  79. }
  80. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, service->kill_console_delay, 0);
  81. if (! (service->stop_method & NSSM_STOP_METHOD_WINDOW)) {
  82. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_UNCHECKED, 0);
  83. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW), 0);
  84. }
  85. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, service->kill_window_delay, 0);
  86. if (! (service->stop_method & NSSM_STOP_METHOD_THREADS)) {
  87. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_UNCHECKED, 0);
  88. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS), 0);
  89. }
  90. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, service->kill_threads_delay, 0);
  91. if (! (service->stop_method & NSSM_STOP_METHOD_TERMINATE)) {
  92. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_UNCHECKED, 0);
  93. }
  94. /* Restart tab. */
  95. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, service->throttle_delay, 0);
  96. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  97. SendMessage(combo, CB_SETCURSEL, service->default_exit_action, 0);
  98. /* I/O tab. */
  99. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDIN, service->stdin_path);
  100. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDOUT, service->stdout_path);
  101. SetDlgItemText(tablist[NSSM_TAB_IO], IDC_STDERR, service->stderr_path);
  102. /* Rotation tab. */
  103. if (service->stdout_disposition == CREATE_ALWAYS) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_SETCHECK, BST_CHECKED, 0);
  104. if (service->rotate_files) {
  105. SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_SETCHECK, BST_CHECKED, 0);
  106. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), 1);
  107. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), 1);
  108. }
  109. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, service->rotate_seconds, 0);
  110. if (! service->rotate_bytes_high) SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, service->rotate_bytes_low, 0);
  111. /* Check if advanced settings are in use. */
  112. if (service->stdout_disposition ^ service->stderr_disposition || service->stdout_disposition & ~CREATE_ALWAYS || service->stderr_disposition & ~CREATE_ALWAYS) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_STDIO);
  113. if (service->rotate_bytes_high) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ROTATE_BYTES);
  114. /* Environment tab. */
  115. TCHAR *env;
  116. unsigned long envlen;
  117. if (service->env_extralen) {
  118. SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_SETCHECK, BST_CHECKED, 0);
  119. env = service->env_extra;
  120. envlen = service->env_extralen;
  121. }
  122. else {
  123. env = service->env;
  124. envlen = service->envlen;
  125. }
  126. if (envlen) {
  127. /* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
  128. unsigned long i, j;
  129. unsigned long newlen = envlen;
  130. for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) newlen++;
  131. TCHAR *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));
  132. if (formatted) {
  133. for (i = 0, j = 0; i < envlen; i++) {
  134. formatted[j] = env[i];
  135. if (! env[i]) {
  136. if (env[i + 1]) {
  137. formatted[j] = _T('\r');
  138. formatted[++j] = _T('\n');
  139. }
  140. }
  141. j++;
  142. }
  143. SetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, formatted);
  144. HeapFree(GetProcessHeap(), 0, formatted);
  145. }
  146. else popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));
  147. }
  148. if (service->envlen && service->env_extralen) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ENVIRONMENT);
  149. }
  150. /* Go! */
  151. MSG message;
  152. while (GetMessage(&message, 0, 0, 0)) {
  153. if (IsDialogMessage(dlg, &message)) continue;
  154. TranslateMessage(&message);
  155. DispatchMessage(&message);
  156. }
  157. return (int) message.wParam;
  158. }
  159. void centre_window(HWND window) {
  160. HWND desktop;
  161. RECT size, desktop_size;
  162. unsigned long x, y;
  163. if (! window) return;
  164. /* Find window size */
  165. if (! GetWindowRect(window, &size)) return;
  166. /* Find desktop window */
  167. desktop = GetDesktopWindow();
  168. if (! desktop) return;
  169. /* Find desktop window size */
  170. if (! GetWindowRect(desktop, &desktop_size)) return;
  171. /* Centre window */
  172. x = (desktop_size.right - size.right) / 2;
  173. y = (desktop_size.bottom - size.bottom) / 2;
  174. MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
  175. }
  176. static inline void check_stop_method(nssm_service_t *service, unsigned long method, unsigned long control) {
  177. if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) return;
  178. service->stop_method &= ~method;
  179. }
  180. static inline void check_number(HWND tab, unsigned long control, unsigned long *timeout) {
  181. BOOL translated;
  182. unsigned long configured = GetDlgItemInt(tab, control, &translated, 0);
  183. if (translated) *timeout = configured;
  184. }
  185. static inline void set_timeout_enabled(unsigned long control, unsigned long dependent) {
  186. unsigned char enabled = 0;
  187. if (SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], control, BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  188. EnableWindow(GetDlgItem(tablist[NSSM_TAB_SHUTDOWN], dependent), enabled);
  189. }
  190. static inline void set_logon_enabled(unsigned char enabled) {
  191. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_INTERACT), ! enabled);
  192. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), enabled);
  193. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), enabled);
  194. EnableWindow(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), enabled);
  195. }
  196. static inline void set_rotation_enabled(unsigned char enabled) {
  197. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), enabled);
  198. EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), enabled);
  199. }
  200. static inline void check_io(TCHAR *name, TCHAR *buffer, unsigned long len, unsigned long control) {
  201. if (! SendMessage(GetDlgItem(tablist[NSSM_TAB_IO], control), WM_GETTEXTLENGTH, 0, 0)) return;
  202. if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) len)) return;
  203. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, name);
  204. ZeroMemory(buffer, len * sizeof(TCHAR));
  205. }
  206. /* Set service parameters. */
  207. int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service) {
  208. if (! service) return 1;
  209. set_nssm_service_defaults(service);
  210. if (orig_service) {
  211. service->native = orig_service->native;
  212. service->handle = orig_service->handle;
  213. }
  214. /* Get service name. */
  215. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  216. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  217. cleanup_nssm_service(service);
  218. return 2;
  219. }
  220. /* Get executable name */
  221. if (! service->native) {
  222. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, _countof(service->exe))) {
  223. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
  224. return 3;
  225. }
  226. /* Get startup directory. */
  227. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, _countof(service->dir))) {
  228. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  229. strip_basename(service->dir);
  230. }
  231. /* Get flags. */
  232. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
  233. if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, _countof(service->flags))) {
  234. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
  235. return 4;
  236. }
  237. }
  238. }
  239. /* Get details. */
  240. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME), WM_GETTEXTLENGTH, 0, 0)) {
  241. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DISPLAYNAME, service->displayname, _countof(service->displayname))) {
  242. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DISPLAYNAME);
  243. return 5;
  244. }
  245. }
  246. if (SendMessage(GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION), WM_GETTEXTLENGTH, 0, 0)) {
  247. if (! GetDlgItemText(tablist[NSSM_TAB_DETAILS], IDC_DESCRIPTION, service->description, _countof(service->description))) {
  248. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_DESCRIPTION);
  249. return 5;
  250. }
  251. }
  252. HWND combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  253. service->startup = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  254. if (service->startup == CB_ERR) service->startup = 0;
  255. /* Get logon stuff. */
  256. if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  257. if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_INTERACT, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  258. service->type |= SERVICE_INTERACTIVE_PROCESS;
  259. }
  260. if (service->username) HeapFree(GetProcessHeap(), 0, service->username);
  261. service->username = 0;
  262. service->usernamelen = 0;
  263. if (service->password) {
  264. SecureZeroMemory(service->password, service->passwordlen);
  265. HeapFree(GetProcessHeap(), 0, service->password);
  266. }
  267. service->password = 0;
  268. service->passwordlen = 0;
  269. }
  270. else {
  271. /* Username. */
  272. service->usernamelen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_USERNAME), WM_GETTEXTLENGTH, 0, 0);
  273. if (! service->usernamelen) {
  274. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_USERNAME);
  275. return 6;
  276. }
  277. service->usernamelen++;
  278. service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->usernamelen * sizeof(TCHAR));
  279. if (! service->username) {
  280. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("account name"), _T("install()"));
  281. return 6;
  282. }
  283. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_USERNAME, service->username, (int) service->usernamelen)) {
  284. HeapFree(GetProcessHeap(), 0, service->username);
  285. service->username = 0;
  286. service->usernamelen = 0;
  287. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_USERNAME);
  288. return 6;
  289. }
  290. /*
  291. Special case LOCALSYSTEM.
  292. Ignore the password if we're editing and the username hasn't changed.
  293. */
  294. if (str_equiv(service->username, NSSM_LOCALSYSTEM_ACCOUNT)) {
  295. HeapFree(GetProcessHeap(), 0, service->username);
  296. service->username = 0;
  297. service->usernamelen = 0;
  298. }
  299. else {
  300. /* Password. */
  301. service->passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1), WM_GETTEXTLENGTH, 0, 0);
  302. size_t passwordlen = SendMessage(GetDlgItem(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2), WM_GETTEXTLENGTH, 0, 0);
  303. if (! orig_service || ! orig_service->username || ! str_equiv(service->username, orig_service->username) || service->passwordlen || passwordlen) {
  304. if (! service->passwordlen) {
  305. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  306. return 6;
  307. }
  308. if (passwordlen != service->passwordlen) {
  309. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  310. return 6;
  311. }
  312. service->passwordlen++;
  313. /* Temporary buffer for password validation. */
  314. TCHAR *password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  315. if (! password) {
  316. HeapFree(GetProcessHeap(), 0, service->username);
  317. service->username = 0;
  318. service->usernamelen = 0;
  319. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password confirmation"), _T("install()"));
  320. return 6;
  321. }
  322. /* Actual password buffer. */
  323. service->password = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->passwordlen * sizeof(TCHAR));
  324. if (! service->password) {
  325. HeapFree(GetProcessHeap(), 0, password);
  326. HeapFree(GetProcessHeap(), 0, service->username);
  327. service->username = 0;
  328. service->usernamelen = 0;
  329. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("password"), _T("install()"));
  330. return 6;
  331. }
  332. /* Get first password. */
  333. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD1, service->password, (int) service->passwordlen)) {
  334. HeapFree(GetProcessHeap(), 0, password);
  335. SecureZeroMemory(service->password, service->passwordlen);
  336. HeapFree(GetProcessHeap(), 0, service->password);
  337. service->password = 0;
  338. service->passwordlen = 0;
  339. HeapFree(GetProcessHeap(), 0, service->username);
  340. service->username = 0;
  341. service->usernamelen = 0;
  342. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  343. return 6;
  344. }
  345. /* Get confirmation. */
  346. if (! GetDlgItemText(tablist[NSSM_TAB_LOGON], IDC_PASSWORD2, password, (int) service->passwordlen)) {
  347. SecureZeroMemory(password, service->passwordlen);
  348. HeapFree(GetProcessHeap(), 0, password);
  349. SecureZeroMemory(service->password, service->passwordlen);
  350. HeapFree(GetProcessHeap(), 0, service->password);
  351. service->password = 0;
  352. service->passwordlen = 0;
  353. HeapFree(GetProcessHeap(), 0, service->username);
  354. service->username = 0;
  355. service->usernamelen = 0;
  356. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_PASSWORD);
  357. return 6;
  358. }
  359. /* Compare. */
  360. if (_tcsncmp(password, service->password, service->passwordlen)) {
  361. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PASSWORD);
  362. SecureZeroMemory(password, service->passwordlen);
  363. HeapFree(GetProcessHeap(), 0, password);
  364. SecureZeroMemory(service->password, service->passwordlen);
  365. HeapFree(GetProcessHeap(), 0, service->password);
  366. service->password = 0;
  367. service->passwordlen = 0;
  368. HeapFree(GetProcessHeap(), 0, service->username);
  369. service->username = 0;
  370. service->usernamelen = 0;
  371. return 6;
  372. }
  373. }
  374. }
  375. }
  376. /* Remaining tabs are only for services we manage. */
  377. if (service->native) return 0;
  378. /* Get stop method stuff. */
  379. check_stop_method(service, NSSM_STOP_METHOD_CONSOLE, IDC_METHOD_CONSOLE);
  380. check_stop_method(service, NSSM_STOP_METHOD_WINDOW, IDC_METHOD_WINDOW);
  381. check_stop_method(service, NSSM_STOP_METHOD_THREADS, IDC_METHOD_THREADS);
  382. check_stop_method(service, NSSM_STOP_METHOD_TERMINATE, IDC_METHOD_TERMINATE);
  383. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, &service->kill_console_delay);
  384. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, &service->kill_window_delay);
  385. check_number(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, &service->kill_threads_delay);
  386. /* Get exit action stuff. */
  387. check_number(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, &service->throttle_delay);
  388. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  389. service->default_exit_action = (unsigned long) SendMessage(combo, CB_GETCURSEL, 0, 0);
  390. if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;
  391. /* Get I/O stuff. */
  392. check_io(_T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);
  393. check_io(_T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);
  394. check_io(_T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);
  395. /* Override stdout and/or stderr. */
  396. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  397. if (service->stdout_path[0]) service->stdout_disposition = CREATE_ALWAYS;
  398. if (service->stderr_path[0]) service->stderr_disposition = CREATE_ALWAYS;
  399. }
  400. /* Get rotation stuff. */
  401. if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  402. service->rotate_files = true;
  403. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);
  404. check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);
  405. }
  406. /* Get environment. */
  407. unsigned long envlen = (unsigned long) SendMessage(GetDlgItem(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT), WM_GETTEXTLENGTH, 0, 0);
  408. if (envlen) {
  409. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (envlen + 2) * sizeof(TCHAR));
  410. if (! env) {
  411. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  412. cleanup_nssm_service(service);
  413. return 5;
  414. }
  415. if (! GetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, env, envlen + 1)) {
  416. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  417. HeapFree(GetProcessHeap(), 0, env);
  418. cleanup_nssm_service(service);
  419. return 5;
  420. }
  421. /* Strip CR and replace LF with NULL. */
  422. unsigned long newlen = 0;
  423. unsigned long i, j;
  424. for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++;
  425. /* Must end with two NULLs. */
  426. newlen += 2;
  427. TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));
  428. if (! newenv) {
  429. HeapFree(GetProcessHeap(), 0, env);
  430. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));
  431. cleanup_nssm_service(service);
  432. return 5;
  433. }
  434. for (i = 0, j = 0; i < envlen; i++) {
  435. if (env[i] == _T('\r')) continue;
  436. if (env[i] == _T('\n')) newenv[j] = _T('\0');
  437. else newenv[j] = env[i];
  438. j++;
  439. }
  440. HeapFree(GetProcessHeap(), 0, env);
  441. env = newenv;
  442. envlen = newlen;
  443. /* Test the environment is valid. */
  444. if (test_environment(env)) {
  445. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);
  446. HeapFree(GetProcessHeap(), 0, env);
  447. cleanup_nssm_service(service);
  448. return 5;
  449. }
  450. if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
  451. service->env = env;
  452. service->envlen = envlen;
  453. }
  454. else {
  455. service->env_extra = env;
  456. service->env_extralen = envlen;
  457. }
  458. }
  459. return 0;
  460. }
  461. /* Install the service. */
  462. int install(HWND window) {
  463. if (! window) return 1;
  464. nssm_service_t *service = alloc_nssm_service();
  465. if (service) {
  466. int ret = configure(window, service, 0);
  467. if (ret) return ret;
  468. }
  469. /* See if it works. */
  470. switch (install_service(service)) {
  471. case 1:
  472. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("install()"));
  473. cleanup_nssm_service(service);
  474. return 1;
  475. case 2:
  476. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  477. cleanup_nssm_service(service);
  478. return 2;
  479. case 3:
  480. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  481. cleanup_nssm_service(service);
  482. return 3;
  483. case 4:
  484. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  485. cleanup_nssm_service(service);
  486. return 4;
  487. case 5:
  488. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  489. cleanup_nssm_service(service);
  490. return 5;
  491. case 6:
  492. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  493. cleanup_nssm_service(service);
  494. return 6;
  495. }
  496. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
  497. cleanup_nssm_service(service);
  498. return 0;
  499. }
  500. /* Remove the service */
  501. int remove(HWND window) {
  502. if (! window) return 1;
  503. /* See if it works */
  504. nssm_service_t *service = alloc_nssm_service();
  505. if (service) {
  506. /* Get service name */
  507. if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {
  508. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  509. cleanup_nssm_service(service);
  510. return 2;
  511. }
  512. /* Confirm */
  513. if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
  514. cleanup_nssm_service(service);
  515. return 0;
  516. }
  517. }
  518. switch (remove_service(service)) {
  519. case 1:
  520. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("remove()"));
  521. cleanup_nssm_service(service);
  522. return 1;
  523. case 2:
  524. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  525. cleanup_nssm_service(service);
  526. return 2;
  527. case 3:
  528. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  529. return 3;
  530. cleanup_nssm_service(service);
  531. case 4:
  532. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  533. cleanup_nssm_service(service);
  534. return 4;
  535. }
  536. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
  537. cleanup_nssm_service(service);
  538. return 0;
  539. }
  540. int edit(HWND window, nssm_service_t *orig_service) {
  541. if (! window) return 1;
  542. nssm_service_t *service = alloc_nssm_service();
  543. if (service) {
  544. int ret = configure(window, service, orig_service);
  545. if (ret) return ret;
  546. }
  547. switch (edit_service(service, true)) {
  548. case 1:
  549. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("edit()"));
  550. cleanup_nssm_service(service);
  551. return 1;
  552. case 3:
  553. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  554. cleanup_nssm_service(service);
  555. return 3;
  556. case 4:
  557. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  558. cleanup_nssm_service(service);
  559. return 4;
  560. case 5:
  561. case 6:
  562. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_EDIT_PARAMETERS_FAILED);
  563. cleanup_nssm_service(service);
  564. return 6;
  565. }
  566. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_EDITED, service->name);
  567. cleanup_nssm_service(service);
  568. return 0;
  569. }
  570. static TCHAR *browse_filter(int message) {
  571. switch (message) {
  572. case NSSM_GUI_BROWSE_FILTER_APPLICATIONS: return _T("*.exe;*.bat;*.cmd");
  573. case NSSM_GUI_BROWSE_FILTER_DIRECTORIES: return _T(".");
  574. case NSSM_GUI_BROWSE_FILTER_ALL_FILES: /* Fall through. */
  575. default: return _T("*.*");
  576. }
  577. }
  578. UINT_PTR CALLBACK browse_hook(HWND dlg, UINT message, WPARAM w, LPARAM l) {
  579. switch (message) {
  580. case WM_INITDIALOG:
  581. return 1;
  582. }
  583. return 0;
  584. }
  585. /* Browse for application */
  586. void browse(HWND window, TCHAR *current, unsigned long flags, ...) {
  587. if (! window) return;
  588. va_list arg;
  589. size_t bufsize = 256;
  590. size_t len = bufsize;
  591. int i;
  592. OPENFILENAME ofn;
  593. ZeroMemory(&ofn, sizeof(ofn));
  594. ofn.lStructSize = sizeof(ofn);
  595. ofn.lpstrFilter = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, bufsize * sizeof(TCHAR));
  596. /* XXX: Escaping nulls with FormatMessage is tricky */
  597. if (ofn.lpstrFilter) {
  598. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  599. len = 0;
  600. /* "Applications" + NULL + "*.exe" + NULL */
  601. va_start(arg, flags);
  602. while (i = va_arg(arg, int)) {
  603. TCHAR *localised = message_string(i);
  604. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize, _TRUNCATE, localised);
  605. len += _tcslen(localised) + 1;
  606. LocalFree(localised);
  607. TCHAR *filter = browse_filter(i);
  608. _sntprintf_s((TCHAR *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, _T("%s"), filter);
  609. len += _tcslen(filter) + 1;
  610. }
  611. va_end(arg);
  612. /* Remainder of the buffer is already zeroed */
  613. }
  614. ofn.lpstrFile = new TCHAR[MAX_PATH];
  615. if (flags & OFN_NOVALIDATE) {
  616. /* Directory hack. */
  617. _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T(":%s:"), message_string(NSSM_GUI_BROWSE_FILTER_DIRECTORIES));
  618. }
  619. else _sntprintf_s(ofn.lpstrFile, MAX_PATH, _TRUNCATE, _T("%s"), current);
  620. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  621. ofn.nMaxFile = MAX_PATH;
  622. ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | flags;
  623. if (GetOpenFileName(&ofn)) {
  624. /* Directory hack. */
  625. if (flags & OFN_NOVALIDATE) strip_basename(ofn.lpstrFile);
  626. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  627. }
  628. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  629. delete[] ofn.lpstrFile;
  630. }
  631. INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
  632. switch (message) {
  633. case WM_INITDIALOG:
  634. return 1;
  635. /* Button was pressed or control was controlled. */
  636. case WM_COMMAND:
  637. HWND dlg;
  638. TCHAR buffer[MAX_PATH];
  639. unsigned char enabled;
  640. switch (LOWORD(w)) {
  641. /* Browse for application. */
  642. case IDC_BROWSE:
  643. dlg = GetDlgItem(tab, IDC_PATH);
  644. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  645. browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  646. /* Fill in startup directory if it wasn't already specified. */
  647. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  648. if (! buffer[0]) {
  649. GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));
  650. strip_basename(buffer);
  651. SetDlgItemText(tab, IDC_DIR, buffer);
  652. }
  653. break;
  654. /* Browse for startup directory. */
  655. case IDC_BROWSE_DIR:
  656. dlg = GetDlgItem(tab, IDC_DIR);
  657. GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));
  658. browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);
  659. break;
  660. /* Log on. */
  661. case IDC_LOCALSYSTEM:
  662. set_logon_enabled(0);
  663. break;
  664. case IDC_ACCOUNT:
  665. set_logon_enabled(1);
  666. break;
  667. /* Shutdown methods. */
  668. case IDC_METHOD_CONSOLE:
  669. set_timeout_enabled(LOWORD(w), IDC_KILL_CONSOLE);
  670. break;
  671. case IDC_METHOD_WINDOW:
  672. set_timeout_enabled(LOWORD(w), IDC_KILL_WINDOW);
  673. break;
  674. case IDC_METHOD_THREADS:
  675. set_timeout_enabled(LOWORD(w), IDC_KILL_THREADS);
  676. break;
  677. /* Browse for stdin. */
  678. case IDC_BROWSE_STDIN:
  679. dlg = GetDlgItem(tab, IDC_STDIN);
  680. GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));
  681. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  682. break;
  683. /* Browse for stdout. */
  684. case IDC_BROWSE_STDOUT:
  685. dlg = GetDlgItem(tab, IDC_STDOUT);
  686. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  687. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  688. /* Fill in stderr if it wasn't already specified. */
  689. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  690. if (! buffer[0]) {
  691. GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));
  692. SetDlgItemText(tab, IDC_STDERR, buffer);
  693. }
  694. break;
  695. /* Browse for stderr. */
  696. case IDC_BROWSE_STDERR:
  697. dlg = GetDlgItem(tab, IDC_STDERR);
  698. GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));
  699. browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);
  700. break;
  701. /* Rotation. */
  702. case IDC_ROTATE:
  703. if (SendDlgItemMessage(tab, LOWORD(w), BM_GETCHECK, 0, 0) & BST_CHECKED) enabled = 1;
  704. else enabled = 0;
  705. set_rotation_enabled(enabled);
  706. break;
  707. }
  708. return 1;
  709. }
  710. return 0;
  711. }
  712. /* Install/remove dialogue callback */
  713. INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  714. nssm_service_t *service;
  715. switch (message) {
  716. /* Creating the dialogue */
  717. case WM_INITDIALOG:
  718. service = (nssm_service_t *) l;
  719. SetFocus(GetDlgItem(window, IDC_NAME));
  720. HWND tabs;
  721. HWND combo;
  722. tabs = GetDlgItem(window, IDC_TAB1);
  723. if (! tabs) return 0;
  724. /* Set up tabs. */
  725. TCITEM tab;
  726. ZeroMemory(&tab, sizeof(tab));
  727. tab.mask = TCIF_TEXT;
  728. selected_tab = 0;
  729. /* Application tab. */
  730. if (service->native) tab.pszText = message_string(NSSM_GUI_TAB_NATIVE);
  731. else tab.pszText = message_string(NSSM_GUI_TAB_APPLICATION);
  732. tab.cchTextMax = (int) _tcslen(tab.pszText);
  733. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
  734. if (service->native) {
  735. tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
  736. EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);
  737. EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);
  738. }
  739. else tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
  740. ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
  741. /* Details tab. */
  742. tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
  743. tab.cchTextMax = (int) _tcslen(tab.pszText);
  744. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
  745. tablist[NSSM_TAB_DETAILS] = dialog(MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
  746. ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
  747. /* Set defaults. */
  748. combo = GetDlgItem(tablist[NSSM_TAB_DETAILS], IDC_STARTUP);
  749. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_AUTOMATIC, (LPARAM) message_string(NSSM_GUI_STARTUP_AUTOMATIC));
  750. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DELAYED, (LPARAM) message_string(NSSM_GUI_STARTUP_DELAYED));
  751. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_MANUAL, (LPARAM) message_string(NSSM_GUI_STARTUP_MANUAL));
  752. SendMessage(combo, CB_INSERTSTRING, NSSM_STARTUP_DISABLED, (LPARAM) message_string(NSSM_GUI_STARTUP_DISABLED));
  753. SendMessage(combo, CB_SETCURSEL, NSSM_STARTUP_AUTOMATIC, 0);
  754. /* Logon tab. */
  755. tab.pszText = message_string(NSSM_GUI_TAB_LOGON);
  756. tab.cchTextMax = (int) _tcslen(tab.pszText);
  757. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);
  758. tablist[NSSM_TAB_LOGON] = dialog(MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
  759. ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);
  760. /* Set defaults. */
  761. CheckRadioButton(tablist[NSSM_TAB_LOGON], IDC_LOCALSYSTEM, IDC_ACCOUNT, IDC_LOCALSYSTEM);
  762. set_logon_enabled(0);
  763. /* Remaining tabs are only for services we manage. */
  764. if (service->native) return 1;
  765. /* Shutdown tab. */
  766. tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
  767. tab.cchTextMax = (int) _tcslen(tab.pszText);
  768. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
  769. tablist[NSSM_TAB_SHUTDOWN] = dialog(MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
  770. ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
  771. /* Set defaults. */
  772. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_CONSOLE, BM_SETCHECK, BST_CHECKED, 0);
  773. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_CONSOLE, NSSM_KILL_CONSOLE_GRACE_PERIOD, 0);
  774. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_WINDOW, BM_SETCHECK, BST_CHECKED, 0);
  775. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_WINDOW, NSSM_KILL_WINDOW_GRACE_PERIOD, 0);
  776. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_THREADS, BM_SETCHECK, BST_CHECKED, 0);
  777. SetDlgItemInt(tablist[NSSM_TAB_SHUTDOWN], IDC_KILL_THREADS, NSSM_KILL_THREADS_GRACE_PERIOD, 0);
  778. SendDlgItemMessage(tablist[NSSM_TAB_SHUTDOWN], IDC_METHOD_TERMINATE, BM_SETCHECK, BST_CHECKED, 0);
  779. /* Restart tab. */
  780. tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
  781. tab.cchTextMax = (int) _tcslen(tab.pszText);
  782. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
  783. tablist[NSSM_TAB_EXIT] = dialog(MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
  784. ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
  785. /* Set defaults. */
  786. SetDlgItemInt(tablist[NSSM_TAB_EXIT], IDC_THROTTLE, NSSM_RESET_THROTTLE_RESTART, 0);
  787. combo = GetDlgItem(tablist[NSSM_TAB_EXIT], IDC_APPEXIT);
  788. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_RESTART, (LPARAM) message_string(NSSM_GUI_EXIT_RESTART));
  789. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_IGNORE, (LPARAM) message_string(NSSM_GUI_EXIT_IGNORE));
  790. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_REALLY, (LPARAM) message_string(NSSM_GUI_EXIT_REALLY));
  791. SendMessage(combo, CB_INSERTSTRING, NSSM_EXIT_UNCLEAN, (LPARAM) message_string(NSSM_GUI_EXIT_UNCLEAN));
  792. SendMessage(combo, CB_SETCURSEL, NSSM_EXIT_RESTART, 0);
  793. /* I/O tab. */
  794. tab.pszText = message_string(NSSM_GUI_TAB_IO);
  795. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  796. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
  797. tablist[NSSM_TAB_IO] = dialog(MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
  798. ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
  799. /* Rotation tab. */
  800. tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
  801. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  802. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
  803. tablist[NSSM_TAB_ROTATION] = dialog(MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
  804. ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
  805. /* Set defaults. */
  806. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);
  807. SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);
  808. set_rotation_enabled(0);
  809. /* Environment tab. */
  810. tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
  811. tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
  812. SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
  813. tablist[NSSM_TAB_ENVIRONMENT] = dialog(MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
  814. ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
  815. return 1;
  816. /* Tab change. */
  817. case WM_NOTIFY:
  818. NMHDR *notification;
  819. notification = (NMHDR *) l;
  820. switch (notification->code) {
  821. case TCN_SELCHANGE:
  822. HWND tabs;
  823. int selection;
  824. tabs = GetDlgItem(window, IDC_TAB1);
  825. if (! tabs) return 0;
  826. selection = (int) SendMessage(tabs, TCM_GETCURSEL, 0, 0);
  827. if (selection != selected_tab) {
  828. ShowWindow(tablist[selected_tab], SW_HIDE);
  829. ShowWindow(tablist[selection], SW_SHOWDEFAULT);
  830. SetFocus(GetDlgItem(window, IDOK));
  831. selected_tab = selection;
  832. }
  833. return 1;
  834. }
  835. return 0;
  836. /* Button was pressed or control was controlled */
  837. case WM_COMMAND:
  838. switch (LOWORD(w)) {
  839. /* OK button */
  840. case IDOK:
  841. if ((int) GetWindowLongPtr(window, GWLP_USERDATA) == IDD_EDIT) {
  842. if (! edit(window, (nssm_service_t *) GetWindowLongPtr(window, DWLP_USER))) PostQuitMessage(0);
  843. }
  844. else if (! install(window)) PostQuitMessage(0);
  845. break;
  846. /* Cancel button */
  847. case IDCANCEL:
  848. DestroyWindow(window);
  849. break;
  850. /* Remove button */
  851. case IDC_REMOVE:
  852. if (! remove(window)) PostQuitMessage(0);
  853. break;
  854. }
  855. return 1;
  856. /* Window closing */
  857. case WM_CLOSE:
  858. DestroyWindow(window);
  859. return 0;
  860. case WM_DESTROY:
  861. PostQuitMessage(0);
  862. }
  863. return 0;
  864. }