gui.cpp 6.6 KB

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