@@ -50,7 +50,8 @@ public HostAppEntry(
50
50
string runtimePath , // Path to the WebView2 client DLL
51
51
string userDataPath , // Path to the user data folder
52
52
string [ ] interestingLoadedDllPaths , // a list of full paths of DLLs that are related to WebView2 in some way
53
- int browserProcessPid ) // PID of the browser process
53
+ int browserProcessPid ,
54
+ IntPtr [ ] hwnds ) // PID of the browser process
54
55
{
55
56
Kind = kind ;
56
57
ExecutablePath = exePath == null ? "Unknown" : exePath ;
@@ -61,6 +62,7 @@ public HostAppEntry(
61
62
UserDataPath = userDataPath == null ? "Unknown" : userDataPath ;
62
63
InterestingLoadedDllPaths = interestingLoadedDllPaths ;
63
64
BrowserProcessPID = browserProcessPid ;
65
+ Hwnds = hwnds == null ? new IntPtr [ 0 ] : hwnds ;
64
66
}
65
67
66
68
public string DisplayLabel
@@ -98,6 +100,7 @@ public string DisplayLabel
98
100
public string UserDataPath { get ; private set ; }
99
101
public string [ ] InterestingLoadedDllPaths { get ; private set ; }
100
102
public int BrowserProcessPID { get ; private set ; } = 0 ;
103
+ public IntPtr [ ] Hwnds { get ; private set ; }
101
104
public string IntegrityLevel
102
105
{
103
106
get
@@ -401,7 +404,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
401
404
msedgewebview2Process . MainModule . FileName ,
402
405
userDataPathAndProcessType . Item1 ,
403
406
null ,
404
- 0 ) ;
407
+ 0 ,
408
+ null ) ;
405
409
}
406
410
407
411
if ( parentProcess != null )
@@ -426,7 +430,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
426
430
hostAppEntry . Runtime . ExePath ,
427
431
userDataFolder ,
428
432
hostAppEntry . InterestingLoadedDllPaths ,
429
- msedgewebview2Process . Id ) ;
433
+ msedgewebview2Process . Id ,
434
+ hostAppEntry . Hwnds ) ;
430
435
newHostAppEntry . Children . AddRange ( hostAppEntry . Children ) ;
431
436
newHostAppEntry . Children . Add ( currentProcessEntry ) ;
432
437
@@ -470,7 +475,8 @@ private static IEnumerable<HostAppEntry> GetHostAppEntriesFromMachineByProcessMo
470
475
ClientDllPathToRuntimePath ( interestingDllPaths . Item1 ) ,
471
476
null ,
472
477
interestingDllPaths . Item3 ,
473
- 0 ) ) ;
478
+ 0 ,
479
+ null ) ) ;
474
480
}
475
481
}
476
482
return results ;
@@ -531,7 +537,8 @@ public static IEnumerable<HostAppEntry> GetHostAppEntriesFromMachineByPipeEnumer
531
537
ClientDllPathToRuntimePath ( clientDllPath ) ,
532
538
null ,
533
539
interestingDllPaths ,
534
- 0 ) ) ;
540
+ 0 ,
541
+ null ) ) ;
535
542
}
536
543
}
537
544
} ;
@@ -566,6 +573,7 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
566
573
if ( hostAppEntry . BrowserProcessPID == 0 )
567
574
{
568
575
HashSet < int > runtimePids = new HashSet < int > ( ) ;
576
+ Dictionary < int , HashSet < IntPtr > > runtimePidToHwndMap = new Dictionary < int , HashSet < IntPtr > > ( ) ;
569
577
570
578
// And find corresponding top level windows for just this PID.
571
579
if ( pidToTopLevelHwndsMap . TryGetValue ( hostAppEntry . PID , out var topLevelHwnds ) )
@@ -584,9 +592,17 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
584
592
{
585
593
childHwnd = PInvoke . User32 . GetProp ( hostAppLeafHwnd , "CrossProcessChildHWND" ) ;
586
594
}
595
+
587
596
if ( childHwnd != IntPtr . Zero )
588
597
{
589
- runtimePids . Add ( HwndUtil . GetWindowProcessId ( childHwnd ) ) ;
598
+ int runtimePid = HwndUtil . GetWindowProcessId ( childHwnd ) ;
599
+ runtimePids . Add ( runtimePid ) ;
600
+
601
+ if ( ! runtimePidToHwndMap . TryGetValue ( runtimePid , out HashSet < IntPtr > runtimeHwnds ) )
602
+ {
603
+ runtimePidToHwndMap [ runtimePid ] = runtimeHwnds = new HashSet < IntPtr > ( ) ;
604
+ }
605
+ runtimeHwnds . Add ( childHwnd ) ;
590
606
}
591
607
}
592
608
}
@@ -602,6 +618,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
602
618
var userDataPathAndProcessType = GetUserDataPathAndProcessTypeFromProcessViaCommandLine ( runtimeProcess ) ;
603
619
userDataFolder = userDataPathAndProcessType . Item1 ;
604
620
621
+ runtimePidToHwndMap . TryGetValue ( runtimePid , out HashSet < IntPtr > runtimeHwnds ) ;
622
+
605
623
var runtimeEntry = new HostAppEntry (
606
624
"host" ,
607
625
hostAppEntry . ExecutablePath ,
@@ -611,7 +629,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
611
629
hostAppEntry . Runtime . ExePath ,
612
630
userDataFolder ,
613
631
hostAppEntry . InterestingLoadedDllPaths ,
614
- runtimePid ) ;
632
+ runtimePid ,
633
+ runtimeHwnds ? . ToArray ( ) ) ;
615
634
runtimeEntry . Children . AddRange ( hostAppEntry . Children ) ;
616
635
runtimeEntry . Children . Add ( new HostAppEntry (
617
636
userDataPathAndProcessType . Item2 ,
@@ -622,7 +641,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
622
641
runtimeProcess . MainModule . FileName ,
623
642
userDataFolder ,
624
643
null ,
625
- 0 ) ) ;
644
+ 0 ,
645
+ runtimeEntry . Hwnds ) ) ;
626
646
hostAppEntriesWithRuntimePID . Add ( runtimeEntry ) ;
627
647
added = true ;
628
648
}
@@ -653,6 +673,7 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
653
673
List < HostAppEntry > hostAppEntriesResults = new List < HostAppEntry > ( ) ;
654
674
Dictionary < int , HashSet < int > > parentPidToChildPidsMap = new Dictionary < int , HashSet < int > > ( ) ;
655
675
var topLevelHwnds = HwndUtil . GetTopLevelHwnds ( null , true ) ;
676
+ Dictionary < int , HashSet < IntPtr > > childPidToHwnd = new Dictionary < int , HashSet < IntPtr > > ( ) ;
656
677
657
678
// Then find all child (and child of child of...) windows that have appropriate class name
658
679
foreach ( var topLevelHwnd in topLevelHwnds )
@@ -681,6 +702,12 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
681
702
}
682
703
childPids . Add ( childPid ) ;
683
704
}
705
+
706
+ if ( ! childPidToHwnd . TryGetValue ( childPid , out HashSet < IntPtr > childHwnds ) )
707
+ {
708
+ childPidToHwnd [ childPid ] = childHwnds = new HashSet < IntPtr > ( ) ;
709
+ }
710
+ childHwnds . Add ( childHwnd ) ;
684
711
}
685
712
}
686
713
}
@@ -702,6 +729,7 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
702
729
{
703
730
var userDataPathAndProcessType = GetUserDataPathAndProcessTypeFromProcessViaCommandLine ( runtimeProcess ) ;
704
731
string userDataFolder = userDataPathAndProcessType . Item1 ;
732
+ childPidToHwnd . TryGetValue ( childPid , out HashSet < IntPtr > runtimeHwnds ) ;
705
733
706
734
var runtimeEntry = new HostAppEntry (
707
735
"host" ,
@@ -712,7 +740,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
712
740
hostAppEntry . Runtime . ExePath ,
713
741
userDataFolder ,
714
742
hostAppEntry . InterestingLoadedDllPaths ,
715
- childPid ) ;
743
+ childPid ,
744
+ runtimeHwnds ? . ToArray ( ) ) ;
716
745
runtimeEntry . Children . Add ( new HostAppEntry (
717
746
userDataPathAndProcessType . Item2 ,
718
747
runtimeProcess . MainModule . FileName ,
@@ -722,7 +751,8 @@ private static IEnumerable<HostAppEntry> AddRuntimeProcessInfoToHostAppEntriesBy
722
751
runtimeProcess . MainModule . FileName ,
723
752
userDataFolder ,
724
753
null ,
725
- 0 ) ) ;
754
+ 0 ,
755
+ runtimeEntry . Hwnds ) ) ;
726
756
runtimeEntry . Children . AddRange ( hostAppEntry . Children ) ;
727
757
hostAppEntriesResults . Add ( runtimeEntry ) ;
728
758
added = true ;
0 commit comments