forked from winsiderss/systeminformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappsup.c
More file actions
2127 lines (1774 loc) · 59.3 KB
/
Copy pathappsup.c
File metadata and controls
2127 lines (1774 loc) · 59.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Process Hacker -
* application support functions
*
* Copyright (C) 2010-2016 wj32
* Copyright (C) 2017 dmex
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <phapp.h>
#include <winsta.h>
#include <dbghelp.h>
#include <appmodel.h>
#include <shellapi.h>
#include <cpysave.h>
#include <emenu.h>
#include <svcsup.h>
#include <symprv.h>
#include <settings.h>
#include <actions.h>
#include <phappres.h>
#include <phsvccl.h>
#include <phsettings.h>
#include "pcre/pcre2.h"
typedef LONG (WINAPI *_GetPackageFullName)(
_In_ HANDLE hProcess,
_Inout_ UINT32 *packageFullNameLength,
_Out_opt_ PWSTR packageFullName
);
typedef LONG (WINAPI *_GetPackagePath)(
_In_ PACKAGE_ID *packageId,
_Reserved_ UINT32 reserved,
_Inout_ UINT32 *pathLength,
_Out_opt_ PWSTR path
);
typedef LONG (WINAPI *_PackageIdFromFullName)(
_In_ PCWSTR packageFullName,
_In_ UINT32 flags,
_Inout_ UINT32 *bufferLength,
_Out_opt_ BYTE *buffer
);
GUID XP_CONTEXT_GUID = { 0xbeb1b341, 0x6837, 0x4c83, { 0x83, 0x66, 0x2b, 0x45, 0x1e, 0x7c, 0xe6, 0x9b } };
GUID VISTA_CONTEXT_GUID = { 0xe2011457, 0x1546, 0x43c5, { 0xa5, 0xfe, 0x00, 0x8d, 0xee, 0xe3, 0xd3, 0xf0 } };
GUID WIN7_CONTEXT_GUID = { 0x35138b9a, 0x5d96, 0x4fbd, { 0x8e, 0x2d, 0xa2, 0x44, 0x02, 0x25, 0xf9, 0x3a } };
GUID WIN8_CONTEXT_GUID = { 0x4a2f28e3, 0x53b9, 0x4441, { 0xba, 0x9c, 0xd6, 0x9d, 0x4a, 0x4a, 0x6e, 0x38 } };
GUID WINBLUE_CONTEXT_GUID = { 0x1f676c76, 0x80e1, 0x4239, { 0x95, 0xbb, 0x83, 0xd0, 0xf6, 0xd0, 0xda, 0x78 } };
GUID WINTHRESHOLD_CONTEXT_GUID = { 0x8e0f7a12, 0xbfb3, 0x4fe8, { 0xb9, 0xa5, 0x48, 0xfd, 0x50, 0xa1, 0x5a, 0x9a } };
/**
* Determines whether a process is suspended.
*
* \param Process The SYSTEM_PROCESS_INFORMATION structure
* of the process.
*/
BOOLEAN PhGetProcessIsSuspended(
_In_ PSYSTEM_PROCESS_INFORMATION Process
)
{
ULONG i;
for (i = 0; i < Process->NumberOfThreads; i++)
{
if (
Process->Threads[i].ThreadState != Waiting ||
Process->Threads[i].WaitReason != Suspended
)
return FALSE;
}
return Process->NumberOfThreads != 0;
}
/**
* Determines the OS compatibility context of a process.
*
* \param ProcessHandle A handle to a process.
* \param Guid A variable which receives a GUID identifying an
* operating system version.
*/
NTSTATUS PhGetProcessSwitchContext(
_In_ HANDLE ProcessHandle,
_Out_ PGUID Guid
)
{
NTSTATUS status;
PROCESS_BASIC_INFORMATION basicInfo;
#ifdef _WIN64
PVOID peb32;
ULONG data32;
#endif
PVOID data;
// Reverse-engineered from WdcGetProcessSwitchContext (wdc.dll).
// On Windows 8, the function is now SdbGetAppCompatData (apphelp.dll).
// On Windows 10, the function is again WdcGetProcessSwitchContext.
#ifdef _WIN64
if (NT_SUCCESS(PhGetProcessPeb32(ProcessHandle, &peb32)) && peb32)
{
if (WindowsVersion >= WINDOWS_8)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(peb32, FIELD_OFFSET(PEB32, pShimData)),
&data32,
sizeof(ULONG),
NULL
)))
return status;
}
else
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(peb32, FIELD_OFFSET(PEB32, pContextData)),
&data32,
sizeof(ULONG),
NULL
)))
return status;
}
data = UlongToPtr(data32);
}
else
{
#endif
if (!NT_SUCCESS(status = PhGetProcessBasicInformation(ProcessHandle, &basicInfo)))
return status;
if (WindowsVersion >= WINDOWS_8)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(basicInfo.PebBaseAddress, FIELD_OFFSET(PEB, pShimData)),
&data,
sizeof(PVOID),
NULL
)))
return status;
}
else
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(basicInfo.PebBaseAddress, FIELD_OFFSET(PEB, pContextData)),
&data,
sizeof(PVOID),
NULL
)))
return status;
}
#ifdef _WIN64
}
#endif
if (!data)
return STATUS_UNSUCCESSFUL; // no compatibility context data
if (WindowsVersion >= WINDOWS_10_RS2)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(data, 1544),
Guid,
sizeof(GUID),
NULL
)))
return status;
}
else if (WindowsVersion >= WINDOWS_10)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(data, 2040 + 24), // Magic value from SbReadProcContextByHandle
Guid,
sizeof(GUID),
NULL
)))
return status;
}
else if (WindowsVersion >= WINDOWS_8_1)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(data, 2040 + 16), // Magic value from SbReadProcContextByHandle
Guid,
sizeof(GUID),
NULL
)))
return status;
}
else if (WindowsVersion >= WINDOWS_8)
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(data, 2040), // Magic value from SbReadProcContextByHandle
Guid,
sizeof(GUID),
NULL
)))
return status;
}
else
{
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(data, 32), // Magic value from WdcGetProcessSwitchContext
Guid,
sizeof(GUID),
NULL
)))
return status;
}
return STATUS_SUCCESS;
}
PPH_STRING PhGetProcessPackageFullName(
_In_ HANDLE ProcessHandle
)
{
static _GetPackageFullName getPackageFullName = NULL;
LONG result;
PPH_STRING name;
ULONG nameLength;
if (!getPackageFullName)
getPackageFullName = PhGetModuleProcAddress(L"kernel32.dll", "GetPackageFullName");
if (!getPackageFullName)
return NULL;
nameLength = 101;
name = PhCreateStringEx(NULL, (nameLength - 1) * 2);
result = getPackageFullName(ProcessHandle, &nameLength, name->Buffer);
if (result == ERROR_INSUFFICIENT_BUFFER)
{
PhDereferenceObject(name);
name = PhCreateStringEx(NULL, (nameLength - 1) * 2);
result = getPackageFullName(ProcessHandle, &nameLength, name->Buffer);
}
if (result == ERROR_SUCCESS)
{
PhTrimToNullTerminatorString(name);
return name;
}
else
{
PhDereferenceObject(name);
return NULL;
}
}
PACKAGE_ID *PhPackageIdFromFullName(
_In_ PWSTR PackageFullName
)
{
static _PackageIdFromFullName packageIdFromFullName = NULL;
LONG result;
PVOID packageIdBuffer;
ULONG packageIdBufferSize;
if (!packageIdFromFullName)
packageIdFromFullName = PhGetModuleProcAddress(L"kernel32.dll", "PackageIdFromFullName");
if (!packageIdFromFullName)
return NULL;
packageIdBufferSize = 100;
packageIdBuffer = PhAllocate(packageIdBufferSize);
result = packageIdFromFullName(PackageFullName, PACKAGE_INFORMATION_BASIC, &packageIdBufferSize, (PBYTE)packageIdBuffer);
if (result == ERROR_INSUFFICIENT_BUFFER)
{
PhFree(packageIdBuffer);
packageIdBuffer = PhAllocate(packageIdBufferSize);
result = packageIdFromFullName(PackageFullName, PACKAGE_INFORMATION_BASIC, &packageIdBufferSize, (PBYTE)packageIdBuffer);
}
if (result == ERROR_SUCCESS)
{
return packageIdBuffer;
}
else
{
PhFree(packageIdBuffer);
return NULL;
}
}
PPH_STRING PhGetPackagePath(
_In_ PACKAGE_ID *PackageId
)
{
static _GetPackagePath getPackagePath = NULL;
LONG result;
PPH_STRING path;
ULONG pathLength;
if (!getPackagePath)
getPackagePath = PhGetModuleProcAddress(L"kernel32.dll", "GetPackagePath");
if (!getPackagePath)
return NULL;
pathLength = 101;
path = PhCreateStringEx(NULL, (pathLength - 1) * 2);
result = getPackagePath(PackageId, 0, &pathLength, path->Buffer);
if (result == ERROR_INSUFFICIENT_BUFFER)
{
PhDereferenceObject(path);
path = PhCreateStringEx(NULL, (pathLength - 1) * 2);
result = getPackagePath(PackageId, 0, &pathLength, path->Buffer);
}
if (result == ERROR_SUCCESS)
{
PhTrimToNullTerminatorString(path);
return path;
}
else
{
PhDereferenceObject(path);
return NULL;
}
}
/**
* Determines the type of a process based on its image file name.
*
* \param ProcessHandle A handle to a process.
* \param KnownProcessType A variable which receives the process
* type.
*/
NTSTATUS PhGetProcessKnownType(
_In_ HANDLE ProcessHandle,
_Out_ PH_KNOWN_PROCESS_TYPE *KnownProcessType
)
{
NTSTATUS status;
PH_KNOWN_PROCESS_TYPE knownProcessType;
PROCESS_BASIC_INFORMATION basicInfo;
PH_STRINGREF systemRootPrefix;
PPH_STRING fileName;
PPH_STRING newFileName;
PH_STRINGREF name;
#ifdef _WIN64
BOOLEAN isWow64 = FALSE;
#endif
if (!NT_SUCCESS(status = PhGetProcessBasicInformation(
ProcessHandle,
&basicInfo
)))
return status;
if (basicInfo.UniqueProcessId == SYSTEM_PROCESS_ID)
{
*KnownProcessType = SystemProcessType;
return STATUS_SUCCESS;
}
PhGetSystemRoot(&systemRootPrefix);
if (!NT_SUCCESS(status = PhGetProcessImageFileName(
ProcessHandle,
&fileName
)))
{
return status;
}
newFileName = PhGetFileName(fileName);
PhDereferenceObject(fileName);
name = newFileName->sr;
knownProcessType = UnknownProcessType;
if (PhStartsWithStringRef(&name, &systemRootPrefix, TRUE))
{
// Skip the system root, and we now have three cases:
// 1. \\xyz.exe - Windows executable.
// 2. \\System32\\xyz.exe - system32 executable.
// 3. \\SysWow64\\xyz.exe - system32 executable + WOW64.
PhSkipStringRef(&name, systemRootPrefix.Length);
if (PhEqualStringRef2(&name, L"\\explorer.exe", TRUE))
{
knownProcessType = ExplorerProcessType;
}
else if (
PhStartsWithStringRef2(&name, L"\\System32", TRUE)
#ifdef _WIN64
|| (PhStartsWithStringRef2(&name, L"\\SysWow64", TRUE) && (isWow64 = TRUE, TRUE)) // ugly but necessary
#endif
)
{
// SysTem32 and SysWow64 are both 8 characters long.
PhSkipStringRef(&name, 9 * sizeof(WCHAR));
if (FALSE)
; // Dummy
else if (PhEqualStringRef2(&name, L"\\smss.exe", TRUE))
knownProcessType = SessionManagerProcessType;
else if (PhEqualStringRef2(&name, L"\\csrss.exe", TRUE))
knownProcessType = WindowsSubsystemProcessType;
else if (PhEqualStringRef2(&name, L"\\wininit.exe", TRUE))
knownProcessType = WindowsStartupProcessType;
else if (PhEqualStringRef2(&name, L"\\services.exe", TRUE))
knownProcessType = ServiceControlManagerProcessType;
else if (PhEqualStringRef2(&name, L"\\lsass.exe", TRUE))
knownProcessType = LocalSecurityAuthorityProcessType;
else if (PhEqualStringRef2(&name, L"\\lsm.exe", TRUE))
knownProcessType = LocalSessionManagerProcessType;
else if (PhEqualStringRef2(&name, L"\\winlogon.exe", TRUE))
knownProcessType = WindowsLogonProcessType;
else if (PhEqualStringRef2(&name, L"\\svchost.exe", TRUE))
knownProcessType = ServiceHostProcessType;
else if (PhEqualStringRef2(&name, L"\\rundll32.exe", TRUE))
knownProcessType = RunDllAsAppProcessType;
else if (PhEqualStringRef2(&name, L"\\dllhost.exe", TRUE))
knownProcessType = ComSurrogateProcessType;
else if (PhEqualStringRef2(&name, L"\\taskeng.exe", TRUE))
knownProcessType = TaskHostProcessType;
else if (PhEqualStringRef2(&name, L"\\taskhost.exe", TRUE))
knownProcessType = TaskHostProcessType;
else if (PhEqualStringRef2(&name, L"\\taskhostex.exe", TRUE))
knownProcessType = TaskHostProcessType;
else if (PhEqualStringRef2(&name, L"\\taskhostw.exe", TRUE))
knownProcessType = TaskHostProcessType;
else if (PhEqualStringRef2(&name, L"\\wudfhost.exe", TRUE))
knownProcessType = UmdfHostProcessType;
}
}
PhDereferenceObject(newFileName);
#ifdef _WIN64
if (isWow64)
knownProcessType |= KnownProcessWow64;
#endif
*KnownProcessType = knownProcessType;
return status;
}
static BOOLEAN NTAPI PhpSvchostCommandLineCallback(
_In_opt_ PPH_COMMAND_LINE_OPTION Option,
_In_opt_ PPH_STRING Value,
_In_opt_ PVOID Context
)
{
PPH_KNOWN_PROCESS_COMMAND_LINE knownCommandLine = Context;
if (Option && Option->Id == 1)
{
PhSwapReference(&knownCommandLine->ServiceHost.GroupName, Value);
}
return TRUE;
}
BOOLEAN PhaGetProcessKnownCommandLine(
_In_ PPH_STRING CommandLine,
_In_ PH_KNOWN_PROCESS_TYPE KnownProcessType,
_Out_ PPH_KNOWN_PROCESS_COMMAND_LINE KnownCommandLine
)
{
switch (KnownProcessType & KnownProcessTypeMask)
{
case ServiceHostProcessType:
{
// svchost.exe -k <GroupName>
static PH_COMMAND_LINE_OPTION options[] =
{
{ 1, L"k", MandatoryArgumentType }
};
KnownCommandLine->ServiceHost.GroupName = NULL;
PhParseCommandLine(
&CommandLine->sr,
options,
sizeof(options) / sizeof(PH_COMMAND_LINE_OPTION),
PH_COMMAND_LINE_IGNORE_UNKNOWN_OPTIONS,
PhpSvchostCommandLineCallback,
KnownCommandLine
);
if (KnownCommandLine->ServiceHost.GroupName)
{
PH_AUTO(KnownCommandLine->ServiceHost.GroupName);
return TRUE;
}
else
{
return FALSE;
}
}
break;
case RunDllAsAppProcessType:
{
// rundll32.exe <DllName>,<ProcedureName> ...
SIZE_T i;
PH_STRINGREF dllNamePart;
PH_STRINGREF procedureNamePart;
PPH_STRING dllName;
PPH_STRING procedureName;
i = 0;
// Get the rundll32.exe part.
dllName = PhParseCommandLinePart(&CommandLine->sr, &i);
if (!dllName)
return FALSE;
PhDereferenceObject(dllName);
// Get the DLL name part.
while (i < CommandLine->Length / 2 && CommandLine->Buffer[i] == ' ')
i++;
dllName = PhParseCommandLinePart(&CommandLine->sr, &i);
if (!dllName)
return FALSE;
PH_AUTO(dllName);
// The procedure name begins after the last comma.
if (!PhSplitStringRefAtLastChar(&dllName->sr, ',', &dllNamePart, &procedureNamePart))
return FALSE;
dllName = PH_AUTO(PhCreateString2(&dllNamePart));
procedureName = PH_AUTO(PhCreateString2(&procedureNamePart));
// If the DLL name isn't an absolute path, assume it's in system32.
// TODO: Use a proper search function.
if (RtlDetermineDosPathNameType_U(dllName->Buffer) == RtlPathTypeRelative)
{
dllName = PhaConcatStrings(
3,
PH_AUTO_T(PH_STRING, PhGetSystemDirectory())->Buffer,
L"\\",
dllName->Buffer
);
}
KnownCommandLine->RunDllAsApp.FileName = dllName;
KnownCommandLine->RunDllAsApp.ProcedureName = procedureName;
}
break;
case ComSurrogateProcessType:
{
// dllhost.exe /processid:<Guid>
static PH_STRINGREF inprocServer32Name = PH_STRINGREF_INIT(L"InprocServer32");
SIZE_T i;
ULONG_PTR indexOfProcessId;
PPH_STRING argPart;
PPH_STRING guidString;
UNICODE_STRING guidStringUs;
GUID guid;
HANDLE rootKeyHandle;
HANDLE inprocServer32KeyHandle;
PPH_STRING fileName;
i = 0;
// Get the dllhost.exe part.
argPart = PhParseCommandLinePart(&CommandLine->sr, &i);
if (!argPart)
return FALSE;
PhDereferenceObject(argPart);
// Get the argument part.
while (i < (ULONG)CommandLine->Length / 2 && CommandLine->Buffer[i] == ' ')
i++;
argPart = PhParseCommandLinePart(&CommandLine->sr, &i);
if (!argPart)
return FALSE;
PH_AUTO(argPart);
// Find "/processid:"; the GUID is just after that.
_wcsupr(argPart->Buffer);
indexOfProcessId = PhFindStringInString(argPart, 0, L"/PROCESSID:");
if (indexOfProcessId == -1)
return FALSE;
guidString = PhaSubstring(
argPart,
indexOfProcessId + 11,
(ULONG)argPart->Length / 2 - indexOfProcessId - 11
);
PhStringRefToUnicodeString(&guidString->sr, &guidStringUs);
if (!NT_SUCCESS(RtlGUIDFromString(
&guidStringUs,
&guid
)))
return FALSE;
KnownCommandLine->ComSurrogate.Guid = guid;
KnownCommandLine->ComSurrogate.Name = NULL;
KnownCommandLine->ComSurrogate.FileName = NULL;
// Lookup the GUID in the registry to determine the name and file name.
if (NT_SUCCESS(PhOpenKey(
&rootKeyHandle,
KEY_READ,
PH_KEY_CLASSES_ROOT,
&PhaConcatStrings2(L"CLSID\\", guidString->Buffer)->sr,
0
)))
{
KnownCommandLine->ComSurrogate.Name =
PH_AUTO(PhQueryRegistryString(rootKeyHandle, NULL));
if (NT_SUCCESS(PhOpenKey(
&inprocServer32KeyHandle,
KEY_READ,
rootKeyHandle,
&inprocServer32Name,
0
)))
{
KnownCommandLine->ComSurrogate.FileName =
PH_AUTO(PhQueryRegistryString(inprocServer32KeyHandle, NULL));
if (fileName = PH_AUTO(PhExpandEnvironmentStrings(
&KnownCommandLine->ComSurrogate.FileName->sr
)))
{
KnownCommandLine->ComSurrogate.FileName = fileName;
}
NtClose(inprocServer32KeyHandle);
}
NtClose(rootKeyHandle);
}
else if (NT_SUCCESS(PhOpenKey(
&rootKeyHandle,
KEY_READ,
PH_KEY_CLASSES_ROOT,
&PhaConcatStrings2(L"AppID\\", guidString->Buffer)->sr,
0
)))
{
KnownCommandLine->ComSurrogate.Name =
PH_AUTO(PhQueryRegistryString(rootKeyHandle, NULL));
NtClose(rootKeyHandle);
}
}
break;
default:
return FALSE;
}
return TRUE;
}
VOID PhEnumChildWindows(
_In_opt_ HWND WindowHandle,
_In_ ULONG Limit,
_In_ PH_CHILD_ENUM_CALLBACK Callback,
_In_ PVOID Context
)
{
HWND childWindow = NULL;
ULONG i = 0;
while (i < Limit && (childWindow = FindWindowEx(WindowHandle, childWindow, NULL, NULL)))
{
if (!Callback(childWindow, Context))
return;
i++;
}
}
typedef struct _GET_PROCESS_MAIN_WINDOW_CONTEXT
{
HWND Window;
HWND ImmersiveWindow;
HANDLE ProcessId;
BOOLEAN IsImmersive;
BOOLEAN SkipInvisible;
} GET_PROCESS_MAIN_WINDOW_CONTEXT, *PGET_PROCESS_MAIN_WINDOW_CONTEXT;
BOOLEAN CALLBACK PhpGetProcessMainWindowEnumWindowsProc(
_In_ HWND WindowHandle,
_In_opt_ PVOID Context
)
{
PGET_PROCESS_MAIN_WINDOW_CONTEXT context = (PGET_PROCESS_MAIN_WINDOW_CONTEXT)Context;
ULONG processId;
HWND parentWindow;
WINDOWINFO windowInfo;
if (context->SkipInvisible && !IsWindowVisible(WindowHandle))
return TRUE;
GetWindowThreadProcessId(WindowHandle, &processId);
if (UlongToHandle(processId) == context->ProcessId && (context->SkipInvisible ?
!((parentWindow = GetParent(WindowHandle)) && IsWindowVisible(parentWindow)) && // skip windows with a visible parent
PhGetWindowTextEx(WindowHandle, PH_GET_WINDOW_TEXT_INTERNAL | PH_GET_WINDOW_TEXT_LENGTH_ONLY, NULL) != 0 : TRUE)) // skip windows with no title
{
if (!context->ImmersiveWindow && context->IsImmersive &&
GetProp(WindowHandle, L"Windows.ImmersiveShell.IdentifyAsMainCoreWindow"))
{
context->ImmersiveWindow = WindowHandle;
}
windowInfo.cbSize = sizeof(WINDOWINFO);
if (!context->Window && GetWindowInfo(WindowHandle, &windowInfo) && (windowInfo.dwStyle & WS_DLGFRAME))
{
context->Window = WindowHandle;
// If we're not looking at an immersive process, there's no need to search any more windows.
if (!context->IsImmersive)
return FALSE;
}
}
return TRUE;
}
HWND PhGetProcessMainWindow(
_In_ HANDLE ProcessId,
_In_opt_ HANDLE ProcessHandle
)
{
return PhGetProcessMainWindowEx(ProcessId, ProcessHandle, TRUE);
}
HWND PhGetProcessMainWindowEx(
_In_ HANDLE ProcessId,
_In_opt_ HANDLE ProcessHandle,
_In_ BOOLEAN SkipInvisible
)
{
GET_PROCESS_MAIN_WINDOW_CONTEXT context;
HANDLE processHandle = NULL;
memset(&context, 0, sizeof(GET_PROCESS_MAIN_WINDOW_CONTEXT));
context.ProcessId = ProcessId;
context.SkipInvisible = SkipInvisible;
if (ProcessHandle)
processHandle = ProcessHandle;
else
PhOpenProcess(&processHandle, ProcessQueryAccess, ProcessId);
if (processHandle && IsImmersiveProcess_I)
context.IsImmersive = IsImmersiveProcess_I(processHandle);
PhEnumChildWindows(NULL, 0x800, PhpGetProcessMainWindowEnumWindowsProc, &context);
if (!ProcessHandle && processHandle)
NtClose(processHandle);
return context.ImmersiveWindow ? context.ImmersiveWindow : context.Window;
}
PPH_STRING PhGetServiceRelevantFileName(
_In_ PPH_STRINGREF ServiceName,
_In_ SC_HANDLE ServiceHandle
)
{
PPH_STRING fileName = NULL;
LPQUERY_SERVICE_CONFIG config;
if (config = PhGetServiceConfig(ServiceHandle))
{
PhGetServiceDllParameter(ServiceName, &fileName);
if (!fileName)
{
PPH_STRING commandLine;
commandLine = PhCreateString(config->lpBinaryPathName);
if (config->dwServiceType & SERVICE_WIN32)
{
PH_STRINGREF dummyFileName;
PH_STRINGREF dummyArguments;
PhParseCommandLineFuzzy(&commandLine->sr, &dummyFileName, &dummyArguments, &fileName);
if (!fileName)
PhSwapReference(&fileName, commandLine);
}
else
{
fileName = PhGetFileName(commandLine);
}
PhDereferenceObject(commandLine);
}
PhFree(config);
}
return fileName;
}
PPH_STRING PhEscapeStringForDelimiter(
_In_ PPH_STRING String,
_In_ WCHAR Delimiter
)
{
PH_STRING_BUILDER stringBuilder;
SIZE_T length;
SIZE_T i;
WCHAR temp[2];
length = String->Length / 2;
PhInitializeStringBuilder(&stringBuilder, String->Length / 2 * 3);
temp[0] = '\\';
for (i = 0; i < length; i++)
{
if (String->Buffer[i] == '\\' || String->Buffer[i] == Delimiter)
{
temp[1] = String->Buffer[i];
PhAppendStringBuilderEx(&stringBuilder, temp, 4);
}
else
{
PhAppendCharStringBuilder(&stringBuilder, String->Buffer[i]);
}
}
return PhFinalStringBuilderString(&stringBuilder);
}
PPH_STRING PhUnescapeStringForDelimiter(
_In_ PPH_STRING String,
_In_ WCHAR Delimiter
)
{
PH_STRING_BUILDER stringBuilder;
SIZE_T length;
SIZE_T i;
length = String->Length / 2;
PhInitializeStringBuilder(&stringBuilder, String->Length / 2 * 3);
for (i = 0; i < length; i++)
{
if (String->Buffer[i] == '\\')
{
if (i != length - 1)
{
PhAppendCharStringBuilder(&stringBuilder, String->Buffer[i + 1]);
i++;
}
else
{
// Trailing backslash. Just ignore it.
break;
}
}
else
{
PhAppendCharStringBuilder(&stringBuilder, String->Buffer[i]);
}
}
return PhFinalStringBuilderString(&stringBuilder);
}
VOID PhSearchOnlineString(
_In_ HWND hWnd,
_In_ PWSTR String
)
{
PhShellExecuteUserString(hWnd, L"SearchEngine", String, TRUE, NULL);
}
VOID PhShellExecuteUserString(
_In_ HWND hWnd,
_In_ PWSTR Setting,
_In_ PWSTR String,
_In_ BOOLEAN UseShellExecute,
_In_opt_ PWSTR ErrorMessage
)
{
static PH_STRINGREF replacementToken = PH_STRINGREF_INIT(L"%s");
PPH_STRING executeString;
PH_STRINGREF stringBefore;
PH_STRINGREF stringMiddle;
PH_STRINGREF stringAfter;
PPH_STRING ntMessage;
executeString = PhGetStringSetting(Setting);
// Expand environment strings.
PhMoveReference(&executeString, PhExpandEnvironmentStrings(&executeString->sr));
// Make sure the user executable string is absolute. We can't use RtlDetermineDosPathNameType_U
// here because the string may be a URL.
if (PhFindCharInString(executeString, 0, ':') == -1)
{
INT stringArgCount;
PWSTR* stringArgList;
// (dmex) HACK: Escape the individual executeString components.
if ((stringArgList = CommandLineToArgvW(executeString->Buffer, &stringArgCount)) && stringArgCount == 2)
{
PPH_STRING fileName = PhCreateString(stringArgList[0]);
PPH_STRING fileArgs = PhCreateString(stringArgList[1]);
// Make sure the string is absolute and escape the filename.
if (RtlDetermineDosPathNameType_U(fileName->Buffer) == RtlPathTypeRelative)
PhMoveReference(&fileName, PhConcatStrings(4, L"\"", PhApplicationDirectory->Buffer, fileName->Buffer, L"\""));
else
PhMoveReference(&fileName, PhConcatStrings(3, L"\"", fileName->Buffer, L"\""));
// Escape the parameters.
PhMoveReference(&fileArgs, PhConcatStrings(3, L"\"", fileArgs->Buffer, L"\""));
// Create the escaped execute string.
PhMoveReference(&executeString, PhConcatStrings(3, fileName->Buffer, L" ", fileArgs->Buffer));
PhDereferenceObject(fileArgs);
PhDereferenceObject(fileName);
LocalFree(stringArgList);
}
else
{
if (RtlDetermineDosPathNameType_U(executeString->Buffer) == RtlPathTypeRelative)
PhMoveReference(&executeString, PhConcatStrings(4, L"\"", PhApplicationDirectory->Buffer, executeString->Buffer, L"\""));
else
PhMoveReference(&executeString, PhConcatStrings(3, L"\"", executeString->Buffer, L"\""));
}
}
// Replace the token with the string, or use the original string if the token is not present.
if (PhSplitStringRefAtString(&executeString->sr, &replacementToken, FALSE, &stringBefore, &stringAfter))
{
PhInitializeStringRef(&stringMiddle, String);
PhMoveReference(&executeString, PhConcatStringRef3(&stringBefore, &stringMiddle, &stringAfter));
}