gui.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "nssm.h"
  2. int nssm_gui(int resource, char *name) {
  3. /* Create window */
  4. HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);
  5. if (! dlg) {
  6. popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
  7. return 1;
  8. }
  9. /* Display the window */
  10. centre_window(dlg);
  11. ShowWindow(dlg, SW_SHOW);
  12. /* Set service name if given */
  13. if (name) {
  14. SetDlgItemText(dlg, IDC_NAME, name);
  15. /* No point making user click remove if the name is already entered */
  16. if (resource == IDD_REMOVE) {
  17. HWND button = GetDlgItem(dlg, IDC_REMOVE);
  18. if (button) {
  19. SendMessage(button, WM_LBUTTONDOWN, 0, 0);
  20. SendMessage(button, WM_LBUTTONUP, 0, 0);
  21. }
  22. }
  23. }
  24. /* Go! */
  25. MSG message;
  26. while (GetMessage(&message, 0, 0, 0)) {
  27. if (IsDialogMessage(dlg, &message)) continue;
  28. TranslateMessage(&message);
  29. DispatchMessage(&message);
  30. }
  31. return (int) message.wParam;
  32. }
  33. void centre_window(HWND window) {
  34. HWND desktop;
  35. RECT size, desktop_size;
  36. unsigned long x, y;
  37. if (! window) return;
  38. /* Find window size */
  39. if (! GetWindowRect(window, &size)) return;
  40. /* Find desktop window */
  41. desktop = GetDesktopWindow();
  42. if (! desktop) return;
  43. /* Find desktop window size */
  44. if (! GetWindowRect(desktop, &desktop_size)) return;
  45. /* Centre window */
  46. x = (desktop_size.right - size.right) / 2;
  47. y = (desktop_size.bottom - size.bottom) / 2;
  48. MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
  49. }
  50. /* Install the service */
  51. int install(HWND window) {
  52. if (! window) return 1;
  53. /* Check parameters in the window */
  54. char name[VALUE_LENGTH];
  55. char exe[EXE_LENGTH];
  56. char flags[VALUE_LENGTH];
  57. /* Get service name */
  58. if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
  59. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  60. return 2;
  61. }
  62. /* Get executable name */
  63. if (! GetDlgItemText(window, IDC_PATH, exe, sizeof(exe))) {
  64. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
  65. return 3;
  66. }
  67. /* Get flags */
  68. if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
  69. if (! GetDlgItemText(window, IDC_FLAGS, flags, sizeof(flags))) {
  70. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
  71. return 4;
  72. }
  73. }
  74. else ZeroMemory(&flags, sizeof(flags));
  75. /* See if it works */
  76. switch (install_service(name, exe, flags)) {
  77. case 2:
  78. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  79. return 2;
  80. case 3:
  81. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  82. return 3;
  83. case 4:
  84. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
  85. return 4;
  86. case 5:
  87. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
  88. return 5;
  89. case 6:
  90. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
  91. return 6;
  92. }
  93. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, name);
  94. return 0;
  95. }
  96. /* Remove the service */
  97. int remove(HWND window) {
  98. if (! window) return 1;
  99. /* Check parameters in the window */
  100. char name[VALUE_LENGTH];
  101. /* Get service name */
  102. if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
  103. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
  104. return 2;
  105. }
  106. /* Confirm */
  107. if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, name) != IDYES) return 0;
  108. /* See if it works */
  109. switch (remove_service(name)) {
  110. case 2:
  111. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  112. return 2;
  113. case 3:
  114. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
  115. return 3;
  116. case 4:
  117. popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
  118. return 4;
  119. }
  120. popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, name);
  121. return 0;
  122. }
  123. /* Browse for application */
  124. void browse(HWND window) {
  125. if (! window) return;
  126. size_t bufsize = 256;
  127. size_t len = bufsize;
  128. OPENFILENAME ofn;
  129. ZeroMemory(&ofn, sizeof(ofn));
  130. ofn.lStructSize = sizeof(ofn);
  131. ofn.lpstrFilter = (char *) HeapAlloc(GetProcessHeap(), 0, bufsize);
  132. /* XXX: Escaping nulls with FormatMessage is tricky */
  133. if (ofn.lpstrFilter) {
  134. ZeroMemory((void *) ofn.lpstrFilter, bufsize);
  135. char *localised = message_string(NSSM_GUI_BROWSE_FILTER_APPLICATIONS);
  136. _snprintf_s((char *) ofn.lpstrFilter, bufsize, _TRUNCATE, localised);
  137. /* "Applications" + NULL + "*.exe" + NULL */
  138. len = strlen(localised) + 1;
  139. LocalFree(localised);
  140. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.exe");
  141. /* "All files" + NULL + "*.*" + NULL */
  142. len += 6;
  143. localised = message_string(NSSM_GUI_BROWSE_FILTER_ALL_FILES);
  144. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, localised);
  145. len += strlen(localised) + 1;
  146. LocalFree(localised);
  147. _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.*");
  148. /* Remainder of the buffer is already zeroed */
  149. }
  150. ofn.lpstrFile = new char[MAX_PATH];
  151. ofn.lpstrFile[0] = '\0';
  152. ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
  153. ofn.nMaxFile = MAX_PATH;
  154. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  155. if (GetOpenFileName(&ofn)) {
  156. SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
  157. }
  158. if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
  159. delete[] ofn.lpstrFile;
  160. }
  161. /* Install/remove dialogue callback */
  162. INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
  163. switch (message) {
  164. /* Creating the dialogue */
  165. case WM_INITDIALOG:
  166. return 1;
  167. /* Button was pressed or control was controlled */
  168. case WM_COMMAND:
  169. switch (LOWORD(w)) {
  170. /* OK button */
  171. case IDC_OK:
  172. PostQuitMessage(install(window));
  173. break;
  174. /* Cancel button */
  175. case IDC_CANCEL:
  176. DestroyWindow(window);
  177. break;
  178. /* Browse button */
  179. case IDC_BROWSE:
  180. browse(GetDlgItem(window, IDC_PATH));
  181. break;
  182. /* Remove button */
  183. case IDC_REMOVE:
  184. PostQuitMessage(remove(window));
  185. break;
  186. }
  187. return 1;
  188. /* Window closing */
  189. case WM_CLOSE:
  190. DestroyWindow(window);
  191. return 0;
  192. case WM_DESTROY:
  193. PostQuitMessage(0);
  194. }
  195. return 0;
  196. }