@@ -55,7 +55,7 @@ public static void ChangeState_DWM(bool suspend)
5555 DWMThreadAdress ??= GetTargetFunctionAddress ( "dwmcorei.dll" , 0x54F70 ) ;
5656 if ( DWMThreadAdress is null )
5757 {
58- Logger . Error ( "Failed to resolve thread start adress." ) ; return ;
58+ Logger . Error ( "Failed to resolve DWM thread start adress." ) ; return ;
5959 }
6060
6161 ChangeState ( suspend , ( IntPtr ) DWMThreadAdress , ref DWM_IsSuspended , ref DWMThreadId , "DWM" ) ;
@@ -74,12 +74,12 @@ public static void ChangeState_XAML(bool suspend)
7474 Logger . Debug ( "XAML Thread was already running" ) ; return ;
7575 }
7676
77- // The reported offset on ProcessExplorer seems to be missing 0x6280 somehow
78- // 0x54F70 + 0x6280 = 0x5B1F0
79- XAMLThreadAdress ??= GetTargetFunctionAddress ( "Microsoft.UI.Xaml.dll" , 0x5B1F0 ) ;
77+ // The reported offset on ProcessExplorer seems to be missing a part somehow.
78+ // To find the real adress, set offset to 0, and then get the offset from the Debugger.Break at ChangeState()
79+ XAMLThreadAdress ??= GetTargetFunctionAddress ( "Microsoft.UI.Xaml.dll" , 0x5b320 ) ;
8080 if ( XAMLThreadAdress is null )
8181 {
82- Logger . Error ( "Failed to resolve thread start adress." ) ; return ;
82+ Logger . Error ( "Failed to resolve XAML thread start adress." ) ; return ;
8383 }
8484
8585 ChangeState ( suspend , ( IntPtr ) XAMLThreadAdress , ref XAML_IsSuspended , ref XAMLThreadId , "XAML" ) ;
@@ -97,24 +97,36 @@ private static IntPtr GetThreadStartAdress(int threadId)
9797 return adress ;
9898 }
9999
100- private static void ChangeState ( bool suspend , IntPtr threadAdress , ref bool IsSuspended , ref int ? threadId ,
100+ private static void ChangeState ( bool suspend , IntPtr expectedAdress , ref bool IsSuspended , ref int ? threadId ,
101101 string loggerName , bool canRetry = true )
102102 {
103+ IntPtr minId = - 1 ;
104+ uint LastDiff = uint . MaxValue ;
105+
103106 if ( threadId is null )
104107 {
105108 foreach ( ProcessThread thread in Process . GetCurrentProcess ( ) . Threads )
106109 {
107- if ( GetThreadStartAdress ( thread . Id ) == threadAdress )
110+ var adress = GetThreadStartAdress ( thread . Id ) ;
111+ if ( adress == expectedAdress )
108112 {
109113 threadId = thread . Id ;
110114 Logger . Info ( $ "Thread with Id { threadId } was assigned as { loggerName } thread") ;
115+ break ;
116+ }
117+ else if ( ( ( uint ) adress - ( uint ) expectedAdress ) < LastDiff )
118+ {
119+ LastDiff = ( uint ) adress - ( uint ) expectedAdress ;
120+ minId = thread . Id ;
111121 }
112122 }
113123 }
114124
115125 if ( threadId is null )
116126 {
117- Logger . Error ( $ "No thread matching { loggerName } with start adress { threadAdress : X} was found") ;
127+ Logger . Error ( $ "No thread matching { loggerName } with start adress { expectedAdress : X} was found. " +
128+ $ "Best guess was { minId } with adress offset { LastDiff } ") ;
129+ if ( Debugger . IsAttached ) Debugger . Break ( ) ;
118130 return ;
119131 }
120132
@@ -124,7 +136,7 @@ private static void ChangeState(bool suspend, IntPtr threadAdress, ref bool IsSu
124136 if ( canRetry )
125137 {
126138 threadId = null ; // On first try, reset argument threadId so it does get loaded again.
127- ChangeState ( suspend , threadAdress , ref IsSuspended , ref threadId , loggerName , false ) ;
139+ ChangeState ( suspend , expectedAdress , ref IsSuspended , ref threadId , loggerName , false ) ;
128140 return ;
129141 }
130142 // The threadId was already reloaded
0 commit comments