gui.cpp 6.9 KB

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