Skip to content

Commit 15a09ae

Browse files
committed
Improve startup performance, Improve installer mutant handling, Improve single instance (2/2)
1 parent c14c8ca commit 15a09ae

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

ProcessHacker/main.c

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,26 @@ INT WINAPI wWinMain(
252252
HANDLE mutantHandle;
253253
OBJECT_ATTRIBUTES oa;
254254
UNICODE_STRING mutantName;
255+
PPH_STRING objectName;
256+
PH_FORMAT format[2];
257+
258+
PhInitFormatS(&format[0], L"PhMutant_");
259+
PhInitFormatU(&format[1], HandleToUlong(NtCurrentProcessId()));
260+
261+
objectName = PhFormat(format, 2, 16);
262+
PhStringRefToUnicodeString(&objectName->sr, &mutantName);
255263

256-
RtlInitUnicodeString(&mutantName, L"\\BaseNamedObjects\\ProcessHackerMutant");
257264
InitializeObjectAttributes(
258265
&oa,
259266
&mutantName,
260-
0,
261-
NULL,
267+
OBJ_CASE_INSENSITIVE,
268+
PhGetNamespaceHandle(),
262269
NULL
263270
);
264271

265272
NtCreateMutant(&mutantHandle, MUTANT_ALL_ACCESS, &oa, FALSE);
273+
274+
PhDereferenceObject(objectName);
266275
}
267276

268277
// Set priority.
@@ -418,26 +427,48 @@ VOID PhUnregisterMessageLoopFilter(
418427
PhFree(FilterEntry);
419428
}
420429

421-
VOID PhActivatePreviousInstance(
422-
VOID
430+
static BOOLEAN NTAPI PhpPreviousInstancesCallback(
431+
_In_ PPH_STRINGREF Name,
432+
_In_ PPH_STRINGREF TypeName,
433+
_In_opt_ PVOID Context
423434
)
424435
{
425-
HWND hwnd;
426-
427-
hwnd = FindWindow(PH_MAINWND_CLASSNAME, NULL);
436+
ULONG64 processId64;
437+
PH_STRINGREF firstPart;
438+
PH_STRINGREF secondPart;
428439

429-
if (hwnd)
440+
if (
441+
PhStartsWithStringRef2(Name, L"PhMutant_", TRUE) &&
442+
PhSplitStringRefAtChar(Name, L'_', &firstPart, &secondPart) &&
443+
PhStringToInteger64(&secondPart, 10, &processId64)
444+
)
430445
{
431-
ULONG_PTR result;
446+
HWND hwnd;
432447

433-
SendMessageTimeout(hwnd, WM_PH_ACTIVATE, PhStartupParameters.SelectPid, 0, SMTO_BLOCK, 5000, &result);
448+
hwnd = PhGetProcessMainWindow((HANDLE)processId64, NULL);
434449

435-
if (result == PH_ACTIVATE_REPLY)
450+
if (hwnd)
436451
{
437-
SetForegroundWindow(hwnd);
438-
RtlExitUserProcess(STATUS_SUCCESS);
452+
ULONG_PTR result;
453+
454+
SendMessageTimeout(hwnd, WM_PH_ACTIVATE, PhStartupParameters.SelectPid, 0, SMTO_BLOCK, 5000, &result);
455+
456+
if (result == PH_ACTIVATE_REPLY)
457+
{
458+
SetForegroundWindow(hwnd);
459+
RtlExitUserProcess(STATUS_SUCCESS);
460+
}
439461
}
440462
}
463+
464+
return TRUE;
465+
}
466+
467+
VOID PhActivatePreviousInstance(
468+
VOID
469+
)
470+
{
471+
PhEnumDirectoryObjects(PhGetNamespaceHandle(), PhpPreviousInstancesCallback, NULL);
441472
}
442473

443474
VOID PhInitializeCommonControls(

phlib/include/phutil.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,41 @@ PhParseCommandLineFuzzy(
10591059
_Out_opt_ PPH_STRING *FullFileName
10601060
);
10611061

1062+
FORCEINLINE
1063+
HANDLE
1064+
NTAPI
1065+
PhGetNamespaceHandle(
1066+
VOID
1067+
)
1068+
{
1069+
static PH_INITONCE initOnce = PH_INITONCE_INIT;
1070+
static UNICODE_STRING namespacePathUs = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\ProcessHacker");
1071+
static HANDLE directory = NULL;
1072+
1073+
if (PhBeginInitOnce(&initOnce))
1074+
{
1075+
OBJECT_ATTRIBUTES objectAttributes;
1076+
1077+
InitializeObjectAttributes(
1078+
&objectAttributes,
1079+
&namespacePathUs,
1080+
OBJ_OPENIF,
1081+
NULL,
1082+
NULL
1083+
);
1084+
1085+
NtCreateDirectoryObject(
1086+
&directory,
1087+
MAXIMUM_ALLOWED,
1088+
&objectAttributes
1089+
);
1090+
1091+
PhEndInitOnce(&initOnce);
1092+
}
1093+
1094+
return directory;
1095+
}
1096+
10621097
#ifdef __cplusplus
10631098
}
10641099
#endif

0 commit comments

Comments
 (0)