You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.6 KiB
C++

22 years ago
#include "nssm.h"
int nssm_gui(int resource, char *name) {
/* Create window */
HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);
if (! dlg) {
popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
22 years ago
return 1;
}
/* Display the window */
centre_window(dlg);
ShowWindow(dlg, SW_SHOW);
/* Set service name if given */
if (name) {
SetDlgItemText(dlg, IDC_NAME, name);
/* No point making user click remove if the name is already entered */
if (resource == IDD_REMOVE) {
HWND button = GetDlgItem(dlg, IDC_REMOVE);
if (button) {
SendMessage(button, WM_LBUTTONDOWN, 0, 0);
SendMessage(button, WM_LBUTTONUP, 0, 0);
}
}
}
/* Go! */
MSG message;
while (GetMessage(&message, 0, 0, 0)) {
if (IsDialogMessage(dlg, &message)) continue;
22 years ago
TranslateMessage(&message);
DispatchMessage(&message);
}
15 years ago
return (int) message.wParam;
22 years ago
}
void centre_window(HWND window) {
HWND desktop;
RECT size, desktop_size;
unsigned long x, y;
if (! window) return;
/* Find window size */
if (! GetWindowRect(window, &size)) return;
/* Find desktop window */
desktop = GetDesktopWindow();
if (! desktop) return;
/* Find desktop window size */
if (! GetWindowRect(desktop, &desktop_size)) return;
/* Centre window */
x = (desktop_size.right - size.right) / 2;
y = (desktop_size.bottom - size.bottom) / 2;
MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);
22 years ago
}
/* Install the service */
int install(HWND window) {
if (! window) return 1;
/* Check parameters in the window */
char name[VALUE_LENGTH];
char exe[EXE_LENGTH];
char flags[VALUE_LENGTH];
22 years ago
/* Get service name */
if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
22 years ago
return 2;
}
/* Get executable name */
if (! GetDlgItemText(window, IDC_PATH, exe, sizeof(exe))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
22 years ago
return 3;
}
/* Get flags */
if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
if (! GetDlgItemText(window, IDC_FLAGS, flags, sizeof(flags))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
22 years ago
return 4;
}
}
else ZeroMemory(&flags, sizeof(flags));
19 years ago
/* See if it works */
switch (install_service(name, exe, flags)) {
case 2:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
19 years ago
return 2;
22 years ago
19 years ago
case 3:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
19 years ago
return 3;
22 years ago
19 years ago
case 4:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_OUT_OF_MEMORY_FOR_IMAGEPATH);
19 years ago
return 4;
22 years ago
19 years ago
case 5:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
19 years ago
return 5;
case 6:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
19 years ago
return 6;
}
22 years ago
popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, name);
22 years ago
return 0;
}
/* Remove the service */
int remove(HWND window) {
if (! window) return 1;
/* Check parameters in the window */
char name[VALUE_LENGTH];
22 years ago
/* Get service name */
if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
22 years ago
return 2;
}
/* Confirm */
if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, name) != IDYES) return 0;
22 years ago
19 years ago
/* See if it works */
switch (remove_service(name)) {
case 2:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
19 years ago
return 2;
22 years ago
19 years ago
case 3:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_SERVICE_NOT_INSTALLED);
19 years ago
return 3;
22 years ago
19 years ago
case 4:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
19 years ago
return 4;
}
22 years ago
popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, name);
22 years ago
return 0;
}
/* Browse for application */
22 years ago
void browse(HWND window) {
if (! window) return;
size_t bufsize = 256;
size_t len = bufsize;
22 years ago
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = (char *) HeapAlloc(GetProcessHeap(), 0, bufsize);
/* XXX: Escaping nulls with FormatMessage is tricky */
if (ofn.lpstrFilter) {
ZeroMemory((void *) ofn.lpstrFilter, bufsize);
char *localised = message_string(NSSM_GUI_BROWSE_FILTER_APPLICATIONS);
_snprintf_s((char *) ofn.lpstrFilter, bufsize, _TRUNCATE, localised);
/* "Applications" + NULL + "*.exe" + NULL */
len = strlen(localised) + 1;
LocalFree(localised);
_snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.exe");
/* "All files" + NULL + "*.*" + NULL */
len += 6;
localised = message_string(NSSM_GUI_BROWSE_FILTER_ALL_FILES);
_snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, localised);
len += strlen(localised) + 1;
LocalFree(localised);
_snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.*");
/* Remainder of the buffer is already zeroed */
}
22 years ago
ofn.lpstrFile = new char[MAX_PATH];
ofn.lpstrFile[0] = '\0';
ofn.lpstrTitle = message_string(NSSM_GUI_BROWSE_TITLE);
22 years ago
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) {
SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);
}
if (ofn.lpstrFilter) HeapFree(GetProcessHeap(), 0, (void *) ofn.lpstrFilter);
22 years ago
delete[] ofn.lpstrFile;
}
/* Install/remove dialogue callback */
15 years ago
INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
22 years ago
switch (message) {
/* Creating the dialogue */
case WM_INITDIALOG:
return 1;
/* Button was pressed or control was controlled */
case WM_COMMAND:
switch (LOWORD(w)) {
/* OK button */
case IDC_OK:
PostQuitMessage(install(window));
break;
/* Cancel button */
case IDC_CANCEL:
DestroyWindow(window);
break;
/* Browse button */
case IDC_BROWSE:
browse(GetDlgItem(window, IDC_PATH));
break;
/* Remove button */
case IDC_REMOVE:
PostQuitMessage(remove(window));
break;
}
return 1;
/* Window closing */
case WM_CLOSE:
DestroyWindow(window);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
}
return 0;
}