Skip to content

Commit 440dbab

Browse files
committed
Add version checks for legacy builds
1 parent db6c0d1 commit 440dbab

File tree

10 files changed

+83
-39
lines changed

10 files changed

+83
-39
lines changed

ProcessHacker/ProcessHacker.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<StringPooling>true</StringPooling>
212212
<SDLCheck>true</SDLCheck>
213213
<LanguageStandard>stdcpplatest</LanguageStandard>
214+
<ControlFlowGuard>Guard</ControlFlowGuard>
214215
</ClCompile>
215216
<Link>
216217
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;userenv.lib;uxtheme.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>

ProcessHacker/anawait.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ PPH_STRING PhpaGetAlpcInformation(
10131013
_In_ HANDLE ThreadId
10141014
)
10151015
{
1016+
#if (PHNT_VERSION >= PHNT_WIN7)
10161017
NTSTATUS status;
10171018
PPH_STRING string = NULL;
10181019
HANDLE threadHandle;
@@ -1053,4 +1054,7 @@ PPH_STRING PhpaGetAlpcInformation(
10531054
NtClose(threadHandle);
10541055

10551056
return string;
1057+
#else
1058+
return NULL;
1059+
#endif
10561060
}

ProcessHacker/appsup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ BOOLEAN PhInsertCopyListViewEMenuItem(
18401840
LVHITTESTINFO lvHitInfo;
18411841
HDITEM headerItem;
18421842
PH_FORMAT format[3];
1843-
WCHAR headerText[MAX_PATH];
1843+
WCHAR headerText[MAX_PATH] = L"";
18441844

18451845
if (!GetCursorPos(&location))
18461846
return FALSE;
@@ -1853,7 +1853,6 @@ BOOLEAN PhInsertCopyListViewEMenuItem(
18531853
if (ListView_SubItemHitTest(ListViewHandle, &lvHitInfo) == -1)
18541854
return FALSE;
18551855

1856-
memset(headerText, 0, sizeof(headerText));
18571856
memset(&headerItem, 0, sizeof(HDITEM));
18581857
headerItem.mask = HDI_TEXT;
18591858
headerItem.cchTextMax = RTL_NUMBER_OF(headerText);

ProcessHacker/hidnproc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ NTSTATUS PhpEnumHiddenProcessHandles(
937937
_In_opt_ PVOID Context
938938
)
939939
{
940+
#if (PHNT_VERSION >= PHNT_WIN7)
940941
NTSTATUS status;
941942
HANDLE processHandle;
942943

@@ -1016,6 +1017,9 @@ NTSTATUS PhpEnumHiddenProcessHandles(
10161017
status = STATUS_SUCCESS; // HACK
10171018

10181019
return status;
1020+
#else
1021+
return STATUS_UNSUCCESSFUL;
1022+
#endif
10191023
}
10201024

10211025
NTSTATUS PhpEnumHiddenSubKeyHandles(

ProcessHacker/hndlprp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ VOID PhpUpdateHandleGeneral(
656656
}
657657
else if (PhEqualString2(Context->HandleItem->TypeName, L"ALPC Port", TRUE))
658658
{
659+
#if (PHNT_VERSION >= PHNT_WIN7)
659660
NTSTATUS status;
660661
HANDLE processHandle;
661662
HANDLE alpcPortHandle = NULL;
@@ -699,6 +700,7 @@ VOID PhpUpdateHandleGeneral(
699700

700701
NtClose(alpcPortHandle);
701702
}
703+
#endif
702704
}
703705
else if (PhEqualString2(Context->HandleItem->TypeName, L"EtwRegistration", TRUE))
704706
{

ProcessHacker/main.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ INT WINAPI wWinMain(
130130

131131
if (PhStartupParameters.RunAsServiceMode)
132132
{
133-
RtlExitUserProcess(PhRunAsServiceStart(PhStartupParameters.RunAsServiceMode));
133+
PhExitApplication(PhRunAsServiceStart(PhStartupParameters.RunAsServiceMode));
134134
}
135135

136136
if (PhStartupParameters.CommandMode &&
137137
PhStartupParameters.CommandType &&
138138
PhStartupParameters.CommandAction)
139139
{
140-
RtlExitUserProcess(PhCommandModeStart());
140+
PhExitApplication(PhCommandModeStart());
141141
}
142142

143143
PhSettingsInitialization();
@@ -160,13 +160,12 @@ INT WINAPI wWinMain(
160160
{
161161
if (!PhGetOwnTokenAttributes().Elevated)
162162
{
163-
AllowSetForegroundWindow(ASFW_ANY); // TODO: This rarely works. (dmex)
163+
AllowSetForegroundWindow(ASFW_ANY);
164164

165165
if (SUCCEEDED(PhRunAsAdminTask(L"ProcessHackerTaskAdmin")))
166166
{
167-
PhActivatePreviousInstance(); // TODO: This rarely works. (dmex)
168-
169-
RtlExitUserProcess(STATUS_SUCCESS);
167+
PhActivatePreviousInstance();
168+
PhExitApplication(STATUS_SUCCESS);
170169
}
171170
}
172171
}
@@ -204,7 +203,7 @@ INT WINAPI wWinMain(
204203
if (PhStartupParameters.ShowOptions)
205204
{
206205
PhShowOptionsDialog(PhStartupParameters.WindowHandle);
207-
RtlExitUserProcess(STATUS_SUCCESS);
206+
PhExitApplication(STATUS_SUCCESS);
208207
}
209208

210209
if (PhPluginsEnabled && !PhStartupParameters.NoPlugins)
@@ -217,8 +216,6 @@ INT WINAPI wWinMain(
217216
{
218217
PROCESS_MITIGATION_POLICY_INFORMATION policyInfo;
219218

220-
// Note: The PhInitializeMitigationPolicy function enables the other mitigation policies.
221-
// However, we can only enable the ProcessSignaturePolicy after loading plugins.
222219
policyInfo.Policy = ProcessSignaturePolicy;
223220
policyInfo.SignaturePolicy.Flags = 0;
224221
policyInfo.SignaturePolicy.MicrosoftSignedOnly = TRUE;
@@ -235,7 +232,7 @@ INT WINAPI wWinMain(
235232
PostMessage(NULL, WM_NULL, 0, 0);
236233
GetMessage(&message, NULL, 0, 0);
237234

238-
RtlExitUserProcess(PhSvcMain(NULL, NULL));
235+
PhExitApplication(PhSvcMain(NULL, NULL));
239236
}
240237

241238
#ifndef DEBUG
@@ -247,7 +244,7 @@ INT WINAPI wWinMain(
247244
L"Most features will not work correctly.\n\n"
248245
L"Please run the 64-bit version of Process Hacker instead."
249246
);
250-
RtlExitUserProcess(STATUS_IMAGE_SUBSYSTEM_NOT_PRESENT);
247+
PhExitApplication(STATUS_IMAGE_SUBSYSTEM_NOT_PRESENT);
251248
}
252249
#endif
253250

@@ -273,7 +270,7 @@ INT WINAPI wWinMain(
273270
PhDrainAutoPool(&BaseAutoPool);
274271

275272
result = PhMainMessageLoop();
276-
RtlExitUserProcess(result);
273+
PhExitApplication(result);
277274
}
278275

279276
LONG PhMainMessageLoop(
@@ -484,7 +481,7 @@ static BOOLEAN NTAPI PhpPreviousInstancesCallback(
484481
if (result == PH_ACTIVATE_REPLY)
485482
{
486483
SetForegroundWindow(hwnd);
487-
RtlExitUserProcess(STATUS_SUCCESS);
484+
PhExitApplication(STATUS_SUCCESS);
488485
}
489486
}
490487

@@ -607,12 +604,12 @@ BOOLEAN PhInitializeRestartPolicy(
607604
return TRUE;
608605
}
609606

610-
#ifndef DEBUG
607+
#ifdef DEBUG
611608
#include <symprv.h>
612609
#include <minidumpapiset.h>
613610

614-
static ULONG CALLBACK PhpUnhandledExceptionCallback(
615-
_In_ PEXCEPTION_POINTERS ExceptionInfo
611+
LONG CALLBACK PhpUnhandledExceptionCallback(
612+
_In_ struct _EXCEPTION_POINTERS* ExceptionInfo
616613
)
617614
{
618615
PPH_STRING errorMessage;
@@ -725,7 +722,7 @@ static ULONG CALLBACK PhpUnhandledExceptionCallback(
725722
}
726723
}
727724

728-
RtlExitUserProcess(ExceptionInfo->ExceptionRecord->ExceptionCode);
725+
PhExitApplication(ExceptionInfo->ExceptionRecord->ExceptionCode);
729726

730727
PhDereferenceObject(message);
731728
PhDereferenceObject(errorMessage);
@@ -747,9 +744,7 @@ BOOLEAN PhInitializeExceptionPolicy(
747744
PhSetProcessErrorMode(NtCurrentProcess(), errorMode);
748745
}
749746

750-
// NOTE: We really shouldn't be using this function since it can be
751-
// preempted by the Win32 SetUnhandledExceptionFilter function. (dmex)
752-
RtlSetUnhandledExceptionFilter(PhpUnhandledExceptionCallback);
747+
SetUnhandledExceptionFilter(PhpUnhandledExceptionCallback);
753748
#endif
754749

755750
return TRUE;
@@ -930,16 +925,34 @@ NTSTATUS PhpReadSignature(
930925
ULONG bufferSize;
931926
IO_STATUS_BLOCK iosb;
932927

933-
if (!NT_SUCCESS(status = PhCreateFileWin32(&fileHandle, FileName, FILE_GENERIC_READ, FILE_ATTRIBUTE_NORMAL,
934-
FILE_SHARE_READ, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT)))
935-
{
928+
status = PhCreateFileWin32(
929+
&fileHandle,
930+
FileName,
931+
FILE_GENERIC_READ,
932+
FILE_ATTRIBUTE_NORMAL,
933+
FILE_SHARE_READ,
934+
FILE_OPEN,
935+
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT
936+
);
937+
938+
if (!NT_SUCCESS(status))
936939
return status;
937-
}
938940

939941
bufferSize = 1024;
940942
signature = PhAllocate(bufferSize);
941943

942-
status = NtReadFile(fileHandle, NULL, NULL, NULL, &iosb, signature, bufferSize, NULL, NULL);
944+
status = NtReadFile(
945+
fileHandle,
946+
NULL,
947+
NULL,
948+
NULL,
949+
&iosb,
950+
signature,
951+
bufferSize,
952+
NULL,
953+
NULL
954+
);
955+
943956
NtClose(fileHandle);
944957

945958
if (NT_SUCCESS(status))
@@ -1478,7 +1491,7 @@ VOID PhpProcessStartupParameters(
14781491
);
14791492

14801493
if (PhStartupParameters.Help)
1481-
RtlExitUserProcess(STATUS_SUCCESS);
1494+
PhExitApplication(STATUS_SUCCESS);
14821495
}
14831496

14841497
if (PhStartupParameters.InstallKph)
@@ -1502,7 +1515,7 @@ VOID PhpProcessStartupParameters(
15021515
if (!NT_SUCCESS(status) && !PhStartupParameters.Silent)
15031516
PhShowStatus(NULL, L"Unable to install KProcessHacker", status, 0);
15041517

1505-
RtlExitUserProcess(status);
1518+
PhExitApplication(status);
15061519
}
15071520

15081521
if (PhStartupParameters.UninstallKph)
@@ -1514,7 +1527,7 @@ VOID PhpProcessStartupParameters(
15141527
if (!NT_SUCCESS(status) && !PhStartupParameters.Silent)
15151528
PhShowStatus(NULL, L"Unable to uninstall KProcessHacker", status, 0);
15161529

1517-
RtlExitUserProcess(status);
1530+
PhExitApplication(status);
15181531
}
15191532

15201533
if (PhStartupParameters.Elevate && !PhGetOwnTokenAttributes().Elevated)
@@ -1528,7 +1541,7 @@ VOID PhpProcessStartupParameters(
15281541
0,
15291542
NULL
15301543
);
1531-
RtlExitUserProcess(STATUS_SUCCESS);
1544+
PhExitApplication(STATUS_SUCCESS);
15321545
}
15331546

15341547
if (PhStartupParameters.Debug)

ProcessHacker/options.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ INT_PTR CALLBACK PhpOptionsGeneralDlgProc(
14591459
{
14601460
HRESULT status;
14611461
PPH_STRING quotedFileName;
1462+
#if (PHNT_VERSION >= PHNT_WIN7)
14621463
RTL_ELEVATION_FLAGS flags;
14631464

14641465
if (NT_SUCCESS(RtlQueryElevationFlags(&flags)) && flags.ElevationEnabled)
@@ -1484,7 +1485,7 @@ INT_PTR CALLBACK PhpOptionsGeneralDlgProc(
14841485
}
14851486
}
14861487
}
1487-
1488+
#endif
14881489
quotedFileName = PH_AUTO(PhConcatStrings(3, L"\"", PhGetStringOrEmpty(applicationFileName), L"\""));
14891490

14901491
status = PhCreateAdminTask(

ProcessHacker/runas.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ INT_PTR CALLBACK PhpRunAsDlgProc(
12741274
if (!NT_SUCCESS(status))
12751275
goto CleanupExit;
12761276

1277+
#if (PHNT_VERSION >= PHNT_WIN7)
12771278
if (!InitializeProcThreadAttributeList(NULL, 1, 0, &attributeListLength) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
12781279
{
12791280
status = PhGetLastWin32ErrorAsNtStatus();
@@ -1293,7 +1294,7 @@ INT_PTR CALLBACK PhpRunAsDlgProc(
12931294
status = PhGetLastWin32ErrorAsNtStatus();
12941295
goto CleanupExit;
12951296
}
1296-
1297+
#endif
12971298
if (PhGetOwnTokenAttributes().Elevated)
12981299
{
12991300
PhGetObjectSecurity(
@@ -1399,13 +1400,13 @@ INT_PTR CALLBACK PhpRunAsDlgProc(
13991400
{
14001401
PhFree(processSecurityDescriptor);
14011402
}
1402-
1403+
#if (PHNT_VERSION >= PHNT_WIN7)
14031404
if (startupInfo.lpAttributeList)
14041405
{
14051406
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
14061407
PhFree(startupInfo.lpAttributeList);
14071408
}
1408-
1409+
#endif
14091410
if (processHandle)
14101411
{
14111412
NtClose(processHandle);
@@ -2276,6 +2277,7 @@ NTSTATUS PhpRunFileProgram(
22762277
if (!NT_SUCCESS(status))
22772278
goto CleanupExit;
22782279

2280+
#if (PHNT_VERSION >= PHNT_WIN7)
22792281
if (!InitializeProcThreadAttributeList(NULL, 1, 0, &attributeListLength) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
22802282
{
22812283
status = PhGetLastWin32ErrorAsNtStatus();
@@ -2295,7 +2297,7 @@ NTSTATUS PhpRunFileProgram(
22952297
status = PhGetLastWin32ErrorAsNtStatus();
22962298
goto CleanupExit;
22972299
}
2298-
2300+
#endif
22992301
if (PhGetOwnTokenAttributes().Elevated)
23002302
{
23012303
PhGetObjectSecurity(
@@ -2412,12 +2414,13 @@ NTSTATUS PhpRunFileProgram(
24122414
PhFree(processSecurityDescriptor);
24132415
}
24142416

2417+
#if (PHNT_VERSION >= PHNT_WIN7)
24152418
if (startupInfo.lpAttributeList)
24162419
{
24172420
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
24182421
PhFree(startupInfo.lpAttributeList);
24192422
}
2420-
2423+
#endif
24212424
if (processHandle)
24222425
{
24232426
NtClose(processHandle);
@@ -2520,7 +2523,7 @@ NTSTATUS RunAsCreateProcessThread(
25202523
if (!NT_SUCCESS(status = PhOpenProcess(&processHandle, PROCESS_CREATE_PROCESS, UlongToHandle(serviceStatus.dwProcessId))))
25212524
goto CleanupExit;
25222525

2523-
2526+
#if (PHNT_VERSION >= PHNT_WIN7)
25242527
if (!InitializeProcThreadAttributeList(NULL, 1, 0, &attributeListLength) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
25252528
{
25262529
status = PhGetLastWin32ErrorAsNtStatus();
@@ -2540,6 +2543,7 @@ NTSTATUS RunAsCreateProcessThread(
25402543
status = PhGetLastWin32ErrorAsNtStatus();
25412544
goto CleanupExit;
25422545
}
2546+
#endif
25432547

25442548
AllowSetForegroundWindow(ASFW_ANY);
25452549

@@ -2564,11 +2568,13 @@ NTSTATUS RunAsCreateProcessThread(
25642568
if (serviceHandle)
25652569
CloseServiceHandle(serviceHandle);
25662570

2571+
#if (PHNT_VERSION >= PHNT_WIN7)
25672572
if (startupInfo.lpAttributeList)
25682573
{
25692574
DeleteProcThreadAttributeList(startupInfo.lpAttributeList);
25702575
PhFree(startupInfo.lpAttributeList);
25712576
}
2577+
#endif
25722578

25732579
if (commandLine)
25742580
{

0 commit comments

Comments
 (0)