Files
systeminformer/SystemInformer/srvprp.c
dmex 025db3a0ec Fix UI init and token diagnostics
This change moves main-window setup to WM_CREATE to avoid invalid initial layouts, improves dialog focus and window-placement restoration, and includes a wide set of Win32/UI correctness fixes across the app. It also adds thread-stack and Wow64 safety handling, improves token details (authority/RID and elevated-privilege highlighting), and broadens the low-level documentation comments. The update also adjusts a few build settings in CustomBuildTool to improve runtime behavior.
2026-10-01 23:49:16 +10:00

941 lines
33 KiB
C

/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* wj32 2010-2015
* dmex 2017-2026
*
*/
#include <phapp.h>
#include <secedit.h>
#include <svcsup.h>
#include <mainwnd.h>
#include <settings.h>
#include <actions.h>
#include <phplug.h>
#include <phsvccl.h>
#include <phsettings.h>
#include <procprv.h>
#include <srvprv.h>
#define PH_SERVICE_PROP_OLD_WNDPROC_CONTEXT 0xF
#define PH_SERVICE_PROP_CONTEXT 0xE
#define PH_SERVICE_PROP_PLACED_CONTEXT 0xD
typedef struct _SERVICE_PROPERTIES_CONTEXT
{
PPH_SERVICE_ITEM ServiceItem;
union
{
ULONG Flags;
struct
{
ULONG Ready : 1;
ULONG Dirty : 1;
ULONG OldDelayedStart : 1;
ULONG GeneralPageInitialized : 1;
ULONG LayoutInitialized : 1;
ULONG SpareFlags : 27;
};
};
ULONG Spare;
HWND WindowHandle;
HWND TypeWindowHandle;
HWND StartTypeWindowHandle;
HWND ErrorControlWindowHandle;
HWND DelayedStartWindowHandle;
HWND DescriptionWindowHandle;
HWND GroupWindowHandle;
HWND BinaryPathWindowHandle;
HWND UserNameWindowHandle;
HWND PassBoxWindowHandle;
HWND PassCheckBoxWindowHandle;
HWND ServiceDllWindowHandle;
HFONT WindowFont;
LONG WindowDpi;
PH_LAYOUT_MANAGER LayoutManager;
PPH_LAYOUT_ITEM TabPageItem;
} SERVICE_PROPERTIES_CONTEXT, *PSERVICE_PROPERTIES_CONTEXT;
INT_PTR CALLBACK PhpServiceGeneralDlgProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
/**
* Callback function to open a service object.
*
* \param Handle A variable which receives the handle.
* \param DesiredAccess The desired access to the service.
* \param Context The callback context.
* \return STATUS_SUCCESS if successful, otherwise an NTSTATUS error code.
*/
_Function_class_(PH_OPEN_OBJECT)
NTSTATUS PhpOpenServiceCallback(
_Out_ PHANDLE Handle,
_In_ ACCESS_MASK DesiredAccess,
_In_ PVOID Context
)
{
PPH_SERVICE_ITEM serviceItem = ((PPH_SERVICE_ITEM)Context);
NTSTATUS status;
SC_HANDLE serviceHandle;
status = PhOpenService(
&serviceHandle,
DesiredAccess,
PhGetString(serviceItem->Name)
);
if (NT_SUCCESS(status))
{
*Handle = serviceHandle;
return STATUS_SUCCESS;
}
*Handle = NULL;
return status;
}
/**
* Callback function to close a service object.
*
* \param Handle The handle to close.
* \param Release Whether to release the context.
* \param Context The callback context.
* \return STATUS_SUCCESS.
*/
_Function_class_(PH_CLOSE_OBJECT)
NTSTATUS PhpCloseServiceCallback(
_In_opt_ HANDLE Handle,
_In_opt_ BOOLEAN Release,
_In_opt_ PVOID Context
)
{
if (Handle)
{
PhCloseServiceHandle(Handle);
}
if (Release && Context)
{
PhDereferenceObject(((PPH_SERVICE_ITEM)Context));
}
return STATUS_SUCCESS;
}
/**
* Callback function to set service security.
*
* \param SecurityDescriptor The security descriptor.
* \param SecurityInformation The security information.
* \param Context The callback context.
* \return STATUS_SUCCESS if successful, otherwise an NTSTATUS error code.
*/
_Function_class_(PH_SET_OBJECT_SECURITY)
static _Callback_ NTSTATUS PhpSetServiceSecurityCallback(
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor,
_In_ SECURITY_INFORMATION SecurityInformation,
_In_ PVOID Context
)
{
PPH_STD_OBJECT_SECURITY stdObjectSecurity = ((PPH_STD_OBJECT_SECURITY)Context);
PPH_SERVICE_ITEM serviceItem = ((PPH_SERVICE_ITEM)stdObjectSecurity->Context);
NTSTATUS status;
status = PhStdSetObjectSecurity(
SecurityDescriptor,
SecurityInformation,
stdObjectSecurity->ObjectContext
);
if (status == STATUS_ACCESS_DENIED && !PhGetOwnTokenAttributes().Elevated)
{
// Elevate using phsvc.
if (PhUiConnectToPhSvc(NULL, FALSE))
{
status = PhSvcCallSetServiceSecurity(
PhGetString(serviceItem->Name),
SecurityInformation,
SecurityDescriptor
);
PhUiDisconnectFromPhSvc();
}
}
return status;
}
/**
* Window procedure for the service property sheet.
*
* \param hwnd The handle to the window.
* \param uMsg The message being processed.
* \param wParam Message-specific parameter.
* \param lParam Message-specific parameter.
* \return The result of the message processing.
*/
LRESULT CALLBACK PhpPropSheetSrvWndProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
WNDPROC oldWndProc;
PSERVICE_PROPERTIES_CONTEXT context;
oldWndProc = PhGetWindowContext(hwnd, PH_SERVICE_PROP_OLD_WNDPROC_CONTEXT);
context = PhGetWindowContext(hwnd, PH_SERVICE_PROP_CONTEXT);
if (!oldWndProc)
return 0;
switch (uMsg)
{
case WM_DESTROY:
{
// Save here while the sheet window is still valid (the page is destroyed later).
PhSaveWindowPlacementToSetting(SETTING_SERVICE_WINDOW_POSITION, NULL, hwnd);
}
break;
case WM_SHOWWINDOW:
{
// The common controls sheet can reposition itself after PSCB_INITIALIZED, so apply the
// saved position again right before the first show (still invisible, no flash).
if (wParam && !lParam && !PhGetWindowContext(hwnd, PH_SERVICE_PROP_PLACED_CONTEXT))
{
PhSetWindowContext(hwnd, PH_SERVICE_PROP_PLACED_CONTEXT, hwnd);
if (PhValidWindowPlacementFromSetting(SETTING_SERVICE_WINDOW_POSITION))
PhLoadWindowPlacementFromSetting(SETTING_SERVICE_WINDOW_POSITION, NULL, hwnd);
else
PhCenterWindow(hwnd, PhMainWndHandle);
}
}
break;
case WM_NCDESTROY:
{
PhSetWindowProcedure(hwnd, oldWndProc);
PhRemoveWindowContext(hwnd, PH_SERVICE_PROP_OLD_WNDPROC_CONTEXT);
PhRemoveWindowContext(hwnd, PH_SERVICE_PROP_CONTEXT);
PhRemoveWindowContext(hwnd, PH_SERVICE_PROP_PLACED_CONTEXT);
if (context)
{
if (context->LayoutInitialized)
{
PhDeleteLayoutManager(&context->LayoutManager);
context->LayoutInitialized = FALSE;
}
if (context->WindowFont)
{
DeleteFont(context->WindowFont);
context->WindowFont = NULL;
}
}
}
break;
case WM_SYSCOMMAND:
{
// Note: This fixes closing the window from the taskbar preview window. (dmex)
switch (wParam & 0xFFF0)
{
case SC_CLOSE:
{
PostQuitMessage(0);
//SetWindowLongPtr(hwnd, DWLP_MSGRESULT, TRUE);
//return TRUE;
}
break;
}
}
break;
case WM_KEYDOWN: // forward key messages
{
HWND pageWindowHandle;
if (pageWindowHandle = PropSheet_GetCurrentPageHwnd(hwnd))
{
if (SendMessage(pageWindowHandle, uMsg, wParam, lParam))
{
return TRUE;
}
}
}
break;
case WM_SIZE:
{
if (context && context->LayoutInitialized && !IsMinimized(hwnd))
{
PhLayoutManagerLayout(&context->LayoutManager);
}
}
break;
case WM_DPICHANGED:
{
PRECT CONST newRect = (PRECT)lParam;
CallWindowProc(oldWndProc, hwnd, uMsg, wParam, lParam);
// The default property sheet handler is busted on systems with +2 displays (144/96)
// and the 96 DPI is default. The tab control and buttons end up with a huge size and
// incorrect position when DPI is handled by comctl32's internal math. We override it with
// our own layout pass but there might be ~2px of rounding drift per edge. Only update the
// manager's DPI so future layout calls (if any) use the correct scale. (dmex)
if (context && context->LayoutInitialized)
{
PhLayoutManagerUpdate(&context->LayoutManager, LOWORD(wParam));
}
SetWindowPos(
hwnd,
NULL,
newRect->left,
newRect->top,
newRect->right - newRect->left,
newRect->bottom - newRect->top,
SWP_NOZORDER | SWP_NOACTIVATE
);
return 0;
}
}
return CallWindowProc(oldWndProc, hwnd, uMsg, wParam, lParam);
}
/**
* Property sheet procedure for the service properties.
*
* \param hwndDlg The handle to the property sheet.
* \param uMsg The message being processed.
* \param lParam Message-specific parameter.
* \return The result of the message processing.
*/
INT CALLBACK PhpPropSheetSrvProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ LPARAM lParam
)
{
switch (uMsg)
{
case PSCB_INITIALIZED:
{
PhSetWindowContext(hwndDlg, PH_SERVICE_PROP_OLD_WNDPROC_CONTEXT, PhGetWindowProcedure(hwndDlg));
PhSetWindowProcedure(hwndDlg, PhpPropSheetSrvWndProc);
// Position the sheet before it is first shown to avoid flashing at the default location.
if (PhValidWindowPlacementFromSetting(SETTING_SERVICE_WINDOW_POSITION))
PhLoadWindowPlacementFromSetting(SETTING_SERVICE_WINDOW_POSITION, NULL, hwndDlg);
else
PhCenterWindow(hwndDlg, PhMainWndHandle);
}
break;
}
return 0;
}
/**
* Thread function for showing service properties.
*
* \param Context The thread context.
* \return STATUS_SUCCESS.
*/
_Function_class_(USER_THREAD_START_ROUTINE)
NTSTATUS PhpShowServicePropertiesThread(
_In_ PVOID Context
)
{
PPH_SERVICE_ITEM serviceItem = ((PPH_SERVICE_ITEM)Context);
PROPSHEETHEADER propSheetHeader = { sizeof(propSheetHeader) };
PROPSHEETPAGE propSheetPage;
HPROPSHEETPAGE pages[32];
SERVICE_PROPERTIES_CONTEXT context;
propSheetHeader.dwFlags =
PSH_MODELESS |
PSH_NOAPPLYNOW |
PSH_NOCONTEXTHELP |
PSH_PROPTITLE |
PSH_USECALLBACK |
PSH_USEHICON;
propSheetHeader.hInstance = NtCurrentImageBase();
//propSheetHeader.hwndParent = ParentWindowHandle;
propSheetHeader.pszCaption = PhGetString(serviceItem->Name);
propSheetHeader.nPages = 0;
propSheetHeader.nStartPage = 0;
propSheetHeader.phpage = pages;
propSheetHeader.pfnCallback = PhpPropSheetSrvProc;
// General
memset(&context, 0, sizeof(SERVICE_PROPERTIES_CONTEXT));
context.ServiceItem = serviceItem;
context.Ready = FALSE;
context.Dirty = FALSE;
memset(&propSheetPage, 0, sizeof(PROPSHEETPAGE));
propSheetPage.dwSize = sizeof(PROPSHEETPAGE);
propSheetPage.pszTemplate = MAKEINTRESOURCE(IDD_SRVGENERAL);
propSheetPage.hInstance = NtCurrentImageBase();
propSheetPage.pfnDlgProc = PhpServiceGeneralDlgProc;
propSheetPage.lParam = (LPARAM)&context;
pages[propSheetHeader.nPages++] = CreatePropertySheetPage(&propSheetPage);
if (PhPluginsEnabled)
{
PH_PLUGIN_OBJECT_PROPERTIES objectProperties;
objectProperties.Parameter = serviceItem;
objectProperties.NumberOfPages = propSheetHeader.nPages;
objectProperties.MaximumNumberOfPages = sizeof(pages) / sizeof(HPROPSHEETPAGE);
objectProperties.Pages = pages;
PhInvokeCallback(PhGetGeneralCallback(GeneralCallbackServicePropertiesInitializing), &objectProperties);
propSheetHeader.nPages = objectProperties.NumberOfPages;
}
PhModalPropertySheet(&propSheetHeader);
PhDereferenceObject(serviceItem);
return STATUS_SUCCESS;
}
/**
* Shows the service properties.
*
* \param ParentWindowHandle The parent window handle.
* \param ServiceItem The service item.
*/
VOID PhShowServiceProperties(
_In_ HWND ParentWindowHandle,
_In_ PPH_SERVICE_ITEM ServiceItem
)
{
PhReferenceObject(ServiceItem);
PhCreateThread2(PhpShowServicePropertiesThread, ServiceItem);
}
/**
* Refreshes the icon of the service properties dialog.
*
* \param Context The service properties context.
*/
static VOID PhServicePropertiesRefreshIcon(
_In_ PSERVICE_PROPERTIES_CONTEXT Context
)
{
HICON serviceIcon;
if (Context->ServiceItem->IconEntry && Context->ServiceItem->IconEntry->SmallIconIndex)
serviceIcon = PhGetImageListIcon(Context->ServiceItem->IconEntry->SmallIconIndex, FALSE);
else
{
if (Context->ServiceItem->Type == SERVICE_KERNEL_DRIVER || Context->ServiceItem->Type == SERVICE_FILE_SYSTEM_DRIVER)
serviceIcon = PhGetImageListIcon(1, FALSE); // ServiceCogIcon;
else
serviceIcon = PhGetImageListIcon(0, FALSE); // ServiceApplicationIcon;
}
if (serviceIcon)
{
PhSetWindowIcon(GetParent(Context->WindowHandle), serviceIcon, NULL, TRUE);
}
}
/**
* Refreshes the state of the controls in the service properties dialog.
*
* \param Context The service properties context.
*/
static VOID PhServicePropertiesRefreshControls(
_In_ PSERVICE_PROPERTIES_CONTEXT Context
)
{
PPH_STRING serviceStartTypeString;
PPH_STRING serviceTypeString;
serviceStartTypeString = PH_AUTO(PhGetWindowText(Context->StartTypeWindowHandle));
serviceTypeString = PH_AUTO(PhGetWindowText(Context->TypeWindowHandle));
if (PhEqualString2(serviceStartTypeString, L"Auto start", FALSE))
EnableWindow(Context->DelayedStartWindowHandle, TRUE);
else
EnableWindow(Context->DelayedStartWindowHandle, FALSE);
if (PhEqualString2(serviceTypeString, L"Driver", FALSE) ||
PhEqualString2(serviceTypeString, L"FS driver", FALSE))
{
EnableWindow(Context->UserNameWindowHandle, FALSE);
EnableWindow(Context->PassBoxWindowHandle, FALSE);
EnableWindow(Context->PassCheckBoxWindowHandle, FALSE);
EnableWindow(Context->ServiceDllWindowHandle, FALSE);
}
else
{
EnableWindow(Context->UserNameWindowHandle, TRUE);
EnableWindow(Context->PassBoxWindowHandle, TRUE);
EnableWindow(Context->PassCheckBoxWindowHandle, TRUE);
EnableWindow(Context->ServiceDllWindowHandle, TRUE);
}
}
/**
* Dialog procedure for the general service properties tab.
*
* \param hwndDlg The handle to the dialog.
* \param uMsg The message being processed.
* \param wParam Message-specific parameter.
* \param lParam Message-specific parameter.
* \return TRUE if the message was handled, otherwise FALSE.
*/
INT_PTR CALLBACK PhpServiceGeneralDlgProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
PSERVICE_PROPERTIES_CONTEXT context;
if (uMsg == WM_INITDIALOG)
{
LPPROPSHEETPAGE propSheetPage = (LPPROPSHEETPAGE)lParam;
context = (PSERVICE_PROPERTIES_CONTEXT)propSheetPage->lParam;
PhSetWindowContext(hwndDlg, PH_WINDOW_CONTEXT_DEFAULT, context);
}
else
{
context = PhGetWindowContext(hwndDlg, PH_WINDOW_CONTEXT_DEFAULT);
}
if (!context)
return FALSE;
switch (uMsg)
{
case WM_INITDIALOG:
{
PPH_SERVICE_ITEM serviceItem = context->ServiceItem;
SC_HANDLE serviceHandle;
ULONG startType;
ULONG errorControl;
PPH_STRING serviceDll;
context->WindowHandle = hwndDlg;
context->TypeWindowHandle = GetDlgItem(hwndDlg, IDC_TYPE);
context->StartTypeWindowHandle = GetDlgItem(hwndDlg, IDC_STARTTYPE);
context->ErrorControlWindowHandle = GetDlgItem(hwndDlg, IDC_ERRORCONTROL);
context->DelayedStartWindowHandle = GetDlgItem(hwndDlg, IDC_DELAYEDSTART);
context->DescriptionWindowHandle = GetDlgItem(hwndDlg, IDC_DESCRIPTION);
context->GroupWindowHandle = GetDlgItem(hwndDlg, IDC_GROUP);
context->BinaryPathWindowHandle = GetDlgItem(hwndDlg, IDC_BINARYPATH);
context->UserNameWindowHandle = GetDlgItem(hwndDlg, IDC_USERACCOUNT);
context->PassBoxWindowHandle = GetDlgItem(hwndDlg, IDC_PASSWORD);
context->PassCheckBoxWindowHandle = GetDlgItem(hwndDlg, IDC_PASSWORDCHECK);
context->ServiceDllWindowHandle = GetDlgItem(hwndDlg, IDC_SERVICEDLL);
context->WindowDpi = PhGetWindowDpi(GetParent(hwndDlg));
PhSetWindowContext(GetParent(hwndDlg), PH_SERVICE_PROP_CONTEXT, context);
PhAddComboBoxStringRefs(context->TypeWindowHandle, PhServiceTypeStrings, RTL_NUMBER_OF(PhServiceTypeStrings));
PhAddComboBoxStringRefs(context->StartTypeWindowHandle, PhServiceStartTypeStrings, RTL_NUMBER_OF(PhServiceStartTypeStrings));
PhAddComboBoxStringRefs(context->ErrorControlWindowHandle, PhServiceErrorControlStrings, RTL_NUMBER_OF(PhServiceErrorControlStrings));
PhSetWindowText(context->DescriptionWindowHandle, PhGetStringOrEmpty(serviceItem->DisplayName));
PhSelectComboBoxString(context->TypeWindowHandle, PhGetServiceTypeString(serviceItem->Type)->Buffer, FALSE);
startType = serviceItem->StartType;
errorControl = serviceItem->ErrorControl;
if (NT_SUCCESS(PhOpenService(&serviceHandle, SERVICE_QUERY_CONFIG, PhGetString(serviceItem->Name))))
{
LPQUERY_SERVICE_CONFIG config;
PPH_STRING description;
BOOLEAN delayedStart;
if (NT_SUCCESS(PhGetServiceConfig(serviceHandle, &config)))
{
PhSetWindowText(context->GroupWindowHandle, config->lpLoadOrderGroup);
PhSetWindowText(context->BinaryPathWindowHandle, config->lpBinaryPathName);
PhSetWindowText(context->UserNameWindowHandle, config->lpServiceStartName);
if (startType != config->dwStartType || errorControl != config->dwErrorControl)
{
startType = config->dwStartType;
errorControl = config->dwErrorControl;
PhMarkNeedsConfigUpdateServiceItem(serviceItem);
}
PhFree(config);
}
if (description = PhGetServiceDescription(serviceHandle))
{
PhSetWindowText(context->DescriptionWindowHandle, PhGetString(description));
PhDereferenceObject(description);
}
if (NT_SUCCESS(PhGetServiceDelayedAutoStart(serviceHandle, &delayedStart)))
{
context->OldDelayedStart = delayedStart;
if (delayedStart)
Button_SetCheck(context->DelayedStartWindowHandle, BST_CHECKED);
}
PhCloseServiceHandle(serviceHandle);
}
PhSelectComboBoxString(context->StartTypeWindowHandle, PhGetServiceStartTypeString(startType)->Buffer, FALSE);
PhSelectComboBoxString(context->ErrorControlWindowHandle, PhGetServiceErrorControlString(errorControl)->Buffer, FALSE);
PhSetWindowText(context->PassBoxWindowHandle, L"password");
Button_SetCheck(context->PassCheckBoxWindowHandle, BST_UNCHECKED);
if (NT_SUCCESS(PhGetServiceDllParameter(serviceItem->Type, &serviceItem->Name->sr, &serviceDll)))
{
PhSetDialogItemText(hwndDlg, IDC_SERVICEDLL, PhGetString(serviceDll));
PhDereferenceObject(serviceDll);
}
else
{
PhSetDialogItemText(hwndDlg, IDC_SERVICEDLL, L"N/A");
}
PhServicePropertiesRefreshIcon(context);
PhServicePropertiesRefreshControls(context);
context->Ready = TRUE;
if (PhEnableThemeSupport) // TODO: Required for compat (dmex)
PhInitializeWindowTheme(GetParent(hwndDlg), PhEnableThemeSupport); // HACK (GetParent)
else
PhInitializeWindowTheme(hwndDlg, FALSE);
context->GeneralPageInitialized = TRUE;
}
break;
case WM_DESTROY:
{
// The position is saved by the property sheet window procedure (WM_DESTROY).
PhRemoveWindowContext(hwndDlg, PH_WINDOW_CONTEXT_DEFAULT);
}
break;
case WM_SHOWWINDOW:
{
if (!context->LayoutInitialized)
{
HWND parentHandle = GetParent(hwndDlg);
HWND tabControlHandle = PropSheet_GetTabControl(parentHandle);
PhInitializeLayoutManager(&context->LayoutManager, parentHandle);
PhAddTabControlLayoutItem(&context->LayoutManager, tabControlHandle, NULL, &context->TabPageItem);
PhAddLayoutItem(&context->LayoutManager, GetDlgItem(parentHandle, IDOK), NULL, PH_ANCHOR_RIGHT | PH_ANCHOR_BOTTOM);
PhAddLayoutItem(&context->LayoutManager, GetDlgItem(parentHandle, IDCANCEL), NULL, PH_ANCHOR_RIGHT | PH_ANCHOR_BOTTOM);
context->LayoutInitialized = TRUE;
}
}
break;
case WM_COMMAND:
{
switch (GET_WM_COMMAND_ID(wParam, lParam))
{
case IDCANCEL:
{
// Workaround for property sheet + multiline edit: http://support.microsoft.com/kb/130765
SendMessage(GetParent(hwndDlg), uMsg, wParam, lParam);
}
break;
case IDC_PASSWORD:
{
if (HIWORD(wParam) == EN_CHANGE)
{
Button_SetCheck(context->PassCheckBoxWindowHandle, BST_CHECKED);
}
}
break;
case IDC_DELAYEDSTART:
{
context->Dirty = TRUE;
}
break;
case IDC_BROWSE:
{
static PH_FILETYPE_FILTER filters[] =
{
{ L"Executable files (*.exe;*.sys)", L"*.exe;*.sys" },
{ L"All files (*.*)", L"*.*" }
};
PVOID fileDialog;
PPH_STRING commandLine;
PPH_STRING fileName;
fileDialog = PhCreateOpenFileDialog();
PhSetFileDialogFilter(fileDialog, filters, RTL_NUMBER_OF(filters));
commandLine = PH_AUTO_T(PH_STRING, PhGetWindowText(context->BinaryPathWindowHandle));
if (context->ServiceItem->Type & SERVICE_WIN32)
{
PH_STRINGREF dummyFileName;
PH_STRINGREF dummyArguments;
if (!PhParseCommandLineFuzzy(&commandLine->sr, &dummyFileName, &dummyArguments, &fileName))
fileName = NULL;
if (!fileName)
PhSwapReference(&fileName, commandLine);
}
else
{
fileName = PhGetFileName(commandLine);
}
PhSetFileDialogFileName(fileDialog, PhGetString(fileName));
PhDereferenceObject(fileName);
if (PhShowFileDialog(hwndDlg, fileDialog))
{
fileName = PhGetFileDialogFileName(fileDialog);
PhSetWindowText(context->BinaryPathWindowHandle, PhGetString(fileName));
PhDereferenceObject(fileName);
}
PhFreeFileDialog(fileDialog);
}
break;
case IDC_PERMISSIONS:
{
PhReferenceObject(context->ServiceItem);
PhEditSecurityEx(
PhCsForceNoParent ? NULL : hwndDlg,
PhGetStringOrEmpty(context->ServiceItem->DisplayName),
L"Service",
PhpOpenServiceCallback,
PhpCloseServiceCallback,
NULL,
PhpSetServiceSecurityCallback,
context->ServiceItem
);
}
break;
}
switch (GET_WM_COMMAND_CMD(wParam, lParam))
{
case EN_CHANGE:
case CBN_SELCHANGE:
{
PhServicePropertiesRefreshControls(context);
if (context->Ready)
context->Dirty = TRUE;
}
break;
}
}
break;
case WM_NOTIFY:
{
LPNMHDR header = (LPNMHDR)lParam;
switch (header->code)
{
case PSN_QUERYINITIALFOCUS:
{
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LONG_PTR)context->StartTypeWindowHandle);
}
return TRUE;
//case PSN_KILLACTIVE:
// {
// SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, FALSE);
// }
// return TRUE;
case PSN_APPLY:
{
NTSTATUS status;
PPH_SERVICE_ITEM serviceItem = context->ServiceItem;
SC_HANDLE serviceHandle;
PPH_STRING newServiceTypeString;
PPH_STRING newServiceStartTypeString;
PPH_STRING newServiceErrorControlString;
ULONG newServiceType;
ULONG newServiceStartType;
ULONG newServiceErrorControl;
PPH_STRING newServiceGroup = NULL;
PPH_STRING newServiceBinaryPath = NULL;
PPH_STRING newServiceUserAccount = NULL;
PPH_STRING newServicePassword = NULL;
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
if (!context->Dirty)
{
return TRUE;
}
newServiceTypeString = PH_AUTO(PhGetWindowText(context->TypeWindowHandle));
newServiceStartTypeString = PH_AUTO(PhGetWindowText(context->StartTypeWindowHandle));
newServiceErrorControlString = PH_AUTO(PhGetWindowText(context->ErrorControlWindowHandle));
newServiceType = PhGetServiceTypeInteger(&newServiceTypeString->sr);
newServiceStartType = PhGetServiceStartTypeInteger(&newServiceStartTypeString->sr);
newServiceErrorControl = PhGetServiceErrorControlInteger(&newServiceErrorControlString->sr);
if (PhEqualStringRef(PhGetServiceTypeString(serviceItem->Type), &newServiceTypeString->sr, TRUE))
newServiceType = SERVICE_NO_CHANGE;
if (PhEqualStringRef(PhGetServiceStartTypeString(serviceItem->StartType), &newServiceStartTypeString->sr, TRUE))
newServiceStartType = SERVICE_NO_CHANGE;
if (PhEqualStringRef(PhGetServiceErrorControlString(serviceItem->ErrorControl), &newServiceErrorControlString->sr, TRUE))
newServiceErrorControl = SERVICE_NO_CHANGE;
if (PhGetWindowTextLength(context->GroupWindowHandle))
newServiceGroup = PH_AUTO(PhGetWindowText(context->GroupWindowHandle));
if (PhGetWindowTextLength(context->BinaryPathWindowHandle))
newServiceBinaryPath = PH_AUTO(PhGetWindowText(context->BinaryPathWindowHandle));
if (PhGetWindowTextLength(context->UserNameWindowHandle))
newServiceUserAccount = PH_AUTO(PhGetWindowText(context->UserNameWindowHandle));
if (Button_GetCheck(context->PassCheckBoxWindowHandle) == BST_CHECKED)
newServicePassword = PhGetWindowText(context->PassBoxWindowHandle);
status = PhOpenService(&serviceHandle, SERVICE_CHANGE_CONFIG, PhGetString(serviceItem->Name));
if (NT_SUCCESS(status))
{
status = PhChangeServiceConfig(
serviceHandle,
newServiceType,
newServiceStartType,
newServiceErrorControl,
PhGetString(newServiceBinaryPath),
PhGetString(newServiceGroup),
NULL,
NULL,
PhGetString(newServiceUserAccount),
PhGetString(newServicePassword),
NULL
);
if (NT_SUCCESS(status))
{
BOOLEAN newDelayedStart;
newDelayedStart = Button_GetCheck(context->DelayedStartWindowHandle) == BST_CHECKED;
if (newDelayedStart != context->OldDelayedStart)
{
PhSetServiceDelayedAutoStart(serviceHandle, newDelayedStart);
}
PhMarkNeedsConfigUpdateServiceItem(serviceItem);
PhCloseServiceHandle(serviceHandle);
}
else
{
PhCloseServiceHandle(serviceHandle);
goto ErrorCase;
}
}
else
{
if (status == STATUS_ACCESS_DENIED && !PhGetOwnTokenAttributes().Elevated)
{
// Elevate using phsvc.
if (PhUiConnectToPhSvc(hwndDlg, FALSE))
{
status = PhSvcCallChangeServiceConfig(
PhGetString(serviceItem->Name),
newServiceType,
newServiceStartType,
newServiceErrorControl,
PhGetString(newServiceBinaryPath),
PhGetString(newServiceGroup),
NULL,
NULL,
PhGetString(newServiceUserAccount),
PhGetString(newServicePassword),
NULL
);
if (NT_SUCCESS(status))
{
BOOLEAN newDelayedStart;
newDelayedStart = Button_GetCheck(context->DelayedStartWindowHandle) == BST_CHECKED;
if (newDelayedStart != context->OldDelayedStart)
{
SERVICE_DELAYED_AUTO_START_INFO info;
info.fDelayedAutostart = newDelayedStart;
PhSvcCallChangeServiceConfig2(
PhGetString(serviceItem->Name),
SERVICE_CONFIG_DELAYED_AUTO_START_INFO,
&info
);
}
PhMarkNeedsConfigUpdateServiceItem(serviceItem);
}
PhUiDisconnectFromPhSvc();
if (!NT_SUCCESS(status))
{
goto ErrorCase;
}
}
else
{
// User cancelled elevation.
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID);
}
}
else
{
goto ErrorCase;
}
}
goto Cleanup;
ErrorCase:
PhShowStatus(hwndDlg, L"Unable to change service configuration.", status, 0);
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID);
Cleanup:
if (newServicePassword)
{
RtlSecureZeroMemory(newServicePassword->Buffer, newServicePassword->Length);
PhDereferenceObject(newServicePassword);
}
}
return TRUE;
}
}
break;
}
return FALSE;
}