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.

271 lines
7.9 KiB
C++

22 years ago
#include "nssm.h"
static void strip_basename(char *buffer) {
size_t len = strlen(buffer);
size_t i;
for (i = len; i && buffer[i] != '\\' && buffer[i] != '/'; i--);
/* X:\ is OK. */
if (i && buffer[i-1] == ':') i++;
buffer[i] = '\0';
}
22 years ago
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;
nssm_service_t *service = alloc_nssm_service();
if (service) {
/* Get service name. */
if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
cleanup_nssm_service(service);
return 2;
}
22 years ago
/* Get executable name */
if (! GetDlgItemText(window, IDC_PATH, service->exe, sizeof(service->exe))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);
return 3;
}
/* Get flags */
if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {
if (! GetDlgItemText(window, IDC_FLAGS, service->flags, sizeof(service->flags))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);
return 4;
}
22 years ago
}
memmove(service->dir, service->exe, strlen(service->exe));
strip_basename(service->dir);
22 years ago
}
/* See if it works. */
switch (install_service(service)) {
case 1:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "install()");
cleanup_nssm_service(service);
return 1;
19 years ago
case 2:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
cleanup_nssm_service(service);
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);
cleanup_nssm_service(service);
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);
cleanup_nssm_service(service);
19 years ago
return 4;
22 years ago
19 years ago
case 5:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INSTALL_SERVICE_FAILED);
cleanup_nssm_service(service);
19 years ago
return 5;
case 6:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_CREATE_PARAMETERS_FAILED);
cleanup_nssm_service(service);
19 years ago
return 6;
}
22 years ago
popup_message(MB_OK, NSSM_MESSAGE_SERVICE_INSTALLED, service->name);
cleanup_nssm_service(service);
22 years ago
return 0;
}
/* Remove the service */
int remove(HWND window) {
if (! window) return 1;
/* See if it works */
nssm_service_t *service = alloc_nssm_service();
if (service) {
/* Get service name */
if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);
cleanup_nssm_service(service);
return 2;
}
22 years ago
/* Confirm */
if (popup_message(MB_YESNO, NSSM_GUI_ASK_REMOVE_SERVICE, service->name) != IDYES) {
cleanup_nssm_service(service);
return 0;
}
22 years ago
}
switch (remove_service(service)) {
case 1:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, "service", "remove()");
cleanup_nssm_service(service);
return 1;
22 years ago
19 years ago
case 2:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
cleanup_nssm_service(service);
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;
cleanup_nssm_service(service);
22 years ago
19 years ago
case 4:
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_REMOVE_SERVICE_FAILED);
cleanup_nssm_service(service);
19 years ago
return 4;
}
22 years ago
popup_message(MB_OK, NSSM_MESSAGE_SERVICE_REMOVED, service->name);
cleanup_nssm_service(service);
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:
if (! install(window)) PostQuitMessage(0);
22 years ago
break;
/* Cancel button */
case IDCANCEL:
22 years ago
DestroyWindow(window);
break;
/* Browse button */
case IDC_BROWSE:
browse(GetDlgItem(window, IDC_PATH));
break;
/* Remove button */
case IDC_REMOVE:
if (! remove(window)) PostQuitMessage(0);
22 years ago
break;
}
return 1;
/* Window closing */
case WM_CLOSE:
DestroyWindow(window);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
}
return 0;
}