3 int nssm_gui(int resource, char *name) {
\r
7 HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);
\r
9 _snprintf(blurb, sizeof(blurb), "CreateDialog() failed with error code %d", GetLastError());
\r
10 MessageBox(0, blurb, NSSM, MB_OK);
\r
14 /* Display the window */
\r
16 ShowWindow(dlg, SW_SHOW);
\r
18 /* Set service name if given */
\r
20 SetDlgItemText(dlg, IDC_NAME, name);
\r
21 /* No point making user click remove if the name is already entered */
\r
22 if (resource == IDD_REMOVE) {
\r
23 HWND button = GetDlgItem(dlg, IDC_REMOVE);
\r
25 SendMessage(button, WM_LBUTTONDOWN, 0, 0);
\r
26 SendMessage(button, WM_LBUTTONUP, 0, 0);
\r
33 while (GetMessage(&message, 0, 0, 0)) {
\r
34 TranslateMessage(&message);
\r
35 DispatchMessage(&message);
\r
38 return (int) message.wParam;
\r
41 void centre_window(HWND window) {
\r
43 RECT size, desktop_size;
\r
46 if (! window) return;
\r
48 /* Find window size */
\r
49 if (! GetWindowRect(window, &size)) return;
\r
51 /* Find desktop window */
\r
52 desktop = GetDesktopWindow();
\r
53 if (! desktop) return;
\r
55 /* Find desktop window size */
\r
56 if (! GetWindowRect(desktop, &desktop_size)) return;
\r
59 x = (desktop_size.right - size.right) / 2;
\r
60 y = (desktop_size.bottom - size.bottom) / 2;
\r
61 MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
\r
64 /* Install the service */
\r
65 int install(HWND window) {
\r
66 if (! window) return 1;
\r
68 /* Check parameters in the window */
\r
69 char name[STRING_SIZE];
\r
70 char exe[EXE_LENGTH];
\r
71 char flags[STRING_SIZE];
\r
73 /* Get service name */
\r
74 if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
\r
75 MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);
\r
79 /* Get executable name */
\r
80 if (! GetDlgItemText(window, IDC_PATH, exe, sizeof(exe))) {
\r
81 MessageBox(0, "No valid executable path was specified!", NSSM, MB_OK);
\r
86 if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
\r
87 if (! GetDlgItemText(window, IDC_FLAGS, flags, sizeof(flags))) {
\r
88 MessageBox(0, "No valid options were specified!", NSSM, MB_OK);
\r
92 else ZeroMemory(&flags, sizeof(flags));
\r
94 /* See if it works */
\r
95 switch (install_service(name, exe, flags)) {
\r
97 MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
\r
101 MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);
\r
105 MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen. You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);
\r
109 MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);
\r
113 MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);
\r
117 MessageBox(0, "Service successfully installed!", NSSM, MB_OK);
\r
121 /* Remove the service */
\r
122 int remove(HWND window) {
\r
123 if (! window) return 1;
\r
125 /* Check parameters in the window */
\r
126 char name[STRING_SIZE];
\r
128 /* Get service name */
\r
129 if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
\r
130 MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);
\r
135 char blurb[MAX_PATH];
\r
136 if (_snprintf(blurb, sizeof(blurb), "Remove the \"%s\" service?", name) < 0) {
\r
137 if (MessageBox(0, "Remove the service?", NSSM, MB_YESNO) != IDYES) return 0;
\r
139 else if (MessageBox(0, blurb, NSSM, MB_YESNO) != IDYES) return 0;
\r
141 /* See if it works */
\r
142 switch (remove_service(name)) {
\r
144 MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);
\r
148 MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);
\r
152 MessageBox(0, "Can't delete service! Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);
\r
156 MessageBox(0, "Service successfully removed!", NSSM, MB_OK);
\r
160 /* Browse for game */
\r
161 void browse(HWND window) {
\r
162 if (! window) return;
\r
165 ZeroMemory(&ofn, sizeof(ofn));
\r
166 ofn.lStructSize = sizeof(ofn);
\r
167 ofn.lpstrFilter = "Applications\0*.exe\0All files\0*.*\0\0";
\r
168 ofn.lpstrFile = new char[MAX_PATH];
\r
169 ofn.lpstrFile[0] = '\0';
\r
170 ofn.lpstrTitle = "Locate application file";
\r
171 ofn.nMaxFile = MAX_PATH;
\r
172 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
\r
174 if (GetOpenFileName(&ofn)) {
\r
175 SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
\r
178 delete[] ofn.lpstrFile;
\r
181 /* Install/remove dialogue callback */
\r
182 INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
\r
184 /* Creating the dialogue */
\r
185 case WM_INITDIALOG:
\r
188 /* Button was pressed or control was controlled */
\r
190 switch (LOWORD(w)) {
\r
193 PostQuitMessage(install(window));
\r
196 /* Cancel button */
\r
198 DestroyWindow(window);
\r
201 /* Browse button */
\r
203 browse(GetDlgItem(window, IDC_PATH));
\r
206 /* Remove button */
\r
208 PostQuitMessage(remove(window));
\r
213 /* Window closing */
\r
215 DestroyWindow(window);
\r
218 PostQuitMessage(0);
\r