@@ -6275,7 +6275,7 @@ INT_PTR NDirect::GetNativeLibraryExport(NATIVE_LIBRARY_HANDLE handle, LPCWSTR sy
6275
6275
}
6276
6276
6277
6277
// static
6278
- NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleViaHost (NDirectMethodDesc * pMD, AppDomain* pDomain, PCWSTR wszLibName)
6278
+ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleViaHost (NDirectMethodDesc * pMD, PCWSTR wszLibName)
6279
6279
{
6280
6280
STANDARD_VM_CONTRACT;
6281
6281
// Dynamic Pinvoke Support:
@@ -6290,7 +6290,8 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleViaHost(NDirectMethodDesc * pMD,
6290
6290
}
6291
6291
#endif
6292
6292
6293
- LPVOID hmod = NULL ;
6293
+ NATIVE_LIBRARY_HANDLE hmod = NULL ;
6294
+ AppDomain* pDomain = GetAppDomain ();
6294
6295
CLRPrivBinderCoreCLR *pTPABinder = pDomain->GetTPABinderContext ();
6295
6296
Assembly* pAssembly = pMD->GetMethodTable ()->GetAssembly ();
6296
6297
@@ -6349,11 +6350,92 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleViaHost(NDirectMethodDesc * pMD,
6349
6350
args[ARGNUM_1] = PTR_TO_ARGHOLDER (ptrManagedAssemblyLoadContext);
6350
6351
6351
6352
// Make the call
6352
- CALL_MANAGED_METHOD (hmod,LPVOID, args);
6353
+ CALL_MANAGED_METHOD (hmod, NATIVE_LIBRARY_HANDLE, args);
6353
6354
6354
6355
GCPROTECT_END ();
6355
6356
6356
- return (NATIVE_LIBRARY_HANDLE)hmod;
6357
+ return hmod;
6358
+ }
6359
+
6360
+ // Return the AssemblyLoadContext for an assembly
6361
+ INT_PTR GetManagedAssemblyLoadContext (Assembly* pAssembly)
6362
+ {
6363
+ STANDARD_VM_CONTRACT;
6364
+
6365
+ PTR_ICLRPrivBinder pBindingContext = pAssembly->GetManifestFile ()->GetBindingContext ();
6366
+ if (pBindingContext == NULL )
6367
+ {
6368
+ // GetBindingContext() returns NULL for System.Private.CoreLib
6369
+ return NULL ;
6370
+ }
6371
+
6372
+ UINT_PTR assemblyBinderID = 0 ;
6373
+ IfFailThrow (pBindingContext->GetBinderID (&assemblyBinderID));
6374
+
6375
+ AppDomain *pDomain = GetAppDomain ();
6376
+ ICLRPrivBinder *pCurrentBinder = reinterpret_cast <ICLRPrivBinder *>(assemblyBinderID);
6377
+
6378
+ #ifdef FEATURE_COMINTEROP
6379
+ if (AreSameBinderInstance (pCurrentBinder, pDomain->GetWinRtBinder ()))
6380
+ {
6381
+ // No ALC associated handle with WinRT Binders.
6382
+ return NULL ;
6383
+ }
6384
+ #endif // FEATURE_COMINTEROP
6385
+
6386
+ // The code here deals with two implementations of ICLRPrivBinder interface:
6387
+ // - CLRPrivBinderCoreCLR for the TPA binder in the default ALC, and
6388
+ // - CLRPrivBinderAssemblyLoadContext for custom ALCs.
6389
+ // in order obtain the associated ALC handle.
6390
+ INT_PTR ptrManagedAssemblyLoadContext = AreSameBinderInstance (pCurrentBinder, pDomain->GetTPABinderContext ())
6391
+ ? ((CLRPrivBinderCoreCLR *)pCurrentBinder)->GetManagedAssemblyLoadContext ()
6392
+ : ((CLRPrivBinderAssemblyLoadContext *)pCurrentBinder)->GetManagedAssemblyLoadContext ();
6393
+
6394
+ return ptrManagedAssemblyLoadContext;
6395
+ }
6396
+
6397
+ // static
6398
+ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleViaEvent (NDirectMethodDesc * pMD, PCWSTR wszLibName)
6399
+ {
6400
+ STANDARD_VM_CONTRACT;
6401
+
6402
+ NATIVE_LIBRARY_HANDLE hmod = NULL ;
6403
+ Assembly* pAssembly = pMD->GetMethodTable ()->GetAssembly ();
6404
+ INT_PTR ptrManagedAssemblyLoadContext = GetManagedAssemblyLoadContext (pAssembly);
6405
+
6406
+ if (ptrManagedAssemblyLoadContext == NULL )
6407
+ {
6408
+ return NULL ;
6409
+ }
6410
+
6411
+ GCX_COOP ();
6412
+
6413
+ struct {
6414
+ STRINGREF DllName;
6415
+ OBJECTREF AssemblyRef;
6416
+ } gc = { NULL , NULL };
6417
+
6418
+ GCPROTECT_BEGIN (gc);
6419
+
6420
+ gc.DllName = StringObject::NewString (wszLibName);
6421
+ gc.AssemblyRef = pAssembly->GetExposedObject ();
6422
+
6423
+ // Prepare to invoke System.Runtime.Loader.AssemblyLoadContext.ResolveUnmanagedDllUsingEvent method
6424
+ // While ResolveUnmanagedDllUsingEvent() could compute the AssemblyLoadContext using the AssemblyRef
6425
+ // argument, it will involve another pInvoke to the runtime. So AssemblyLoadContext is passed in
6426
+ // as an additional argument.
6427
+ PREPARE_NONVIRTUAL_CALLSITE (METHOD__ASSEMBLYLOADCONTEXT__RESOLVEUNMANAGEDDLLUSINGEVENT);
6428
+ DECLARE_ARGHOLDER_ARRAY (args, 3 );
6429
+ args[ARGNUM_0] = STRINGREF_TO_ARGHOLDER (gc.DllName );
6430
+ args[ARGNUM_1] = OBJECTREF_TO_ARGHOLDER (gc.AssemblyRef );
6431
+ args[ARGNUM_2] = PTR_TO_ARGHOLDER (ptrManagedAssemblyLoadContext);
6432
+
6433
+ // Make the call
6434
+ CALL_MANAGED_METHOD (hmod, NATIVE_LIBRARY_HANDLE, args);
6435
+
6436
+ GCPROTECT_END ();
6437
+
6438
+ return hmod;
6357
6439
}
6358
6440
6359
6441
// Try to load the module alongside the assembly where the PInvoke was declared.
@@ -6633,15 +6715,13 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
6633
6715
AppDomain* pDomain = GetAppDomain ();
6634
6716
6635
6717
// AssemblyLoadContext is not supported in AppX mode and thus,
6636
- // we should not perform PInvoke resolution via it when operating in
6637
- // AppX mode.
6718
+ // we should not perform PInvoke resolution via it when operating in AppX mode.
6638
6719
if (!AppX::IsAppXProcess ())
6639
6720
{
6640
- hmod = LoadLibraryModuleViaHost (pMD, pDomain, wszLibName);
6721
+ hmod = LoadLibraryModuleViaHost (pMD, wszLibName);
6641
6722
if (hmod != NULL )
6642
6723
{
6643
6724
#ifdef FEATURE_PAL
6644
- // Register the system library handle with PAL and get a PAL library handle
6645
6725
hmod = PAL_RegisterLibraryDirect (hmod, wszLibName);
6646
6726
#endif // FEATURE_PAL
6647
6727
return hmod.Extract ();
@@ -6663,27 +6743,37 @@ HINSTANCE NDirect::LoadLibraryModule(NDirectMethodDesc * pMD, LoadLibErrorTracke
6663
6743
if (SString::_wcsicmp (wszLibName, MAIN_CLR_MODULE_NAME_W) == 0 )
6664
6744
{
6665
6745
hmod = GetCLRModule ();
6746
+ if (hmod != NULL )
6747
+ {
6748
+ return hmod.Extract ();
6749
+ }
6666
6750
}
6667
6751
#endif // FEATURE_PAL
6668
6752
6669
- if (hmod == NULL )
6753
+ hmod = LoadLibraryModuleBySearch (pMD, pErrorTracker, wszLibName);
6754
+ if (hmod != NULL )
6670
6755
{
6671
- hmod = LoadLibraryModuleBySearch (pMD, pErrorTracker, wszLibName);
6756
+ #ifdef FEATURE_PAL
6757
+ hmod = PAL_RegisterLibraryDirect (hmod, wszLibName);
6758
+ #endif // FEATURE_PAL
6759
+
6760
+ // If we have a handle add it to the cache.
6761
+ pDomain->AddUnmanagedImageToCache (wszLibName, hmod);
6762
+ return hmod.Extract ();
6763
+ }
6764
+
6765
+ if (!AppX::IsAppXProcess ())
6766
+ {
6767
+ hmod = LoadLibraryModuleViaEvent (pMD, wszLibName);
6672
6768
if (hmod != NULL )
6673
6769
{
6674
6770
#ifdef FEATURE_PAL
6675
- // Register the system library handle with PAL and get a PAL library handle
6676
6771
hmod = PAL_RegisterLibraryDirect (hmod, wszLibName);
6677
6772
#endif // FEATURE_PAL
6773
+ return hmod.Extract ();
6678
6774
}
6679
6775
}
6680
6776
6681
- if (hmod != NULL )
6682
- {
6683
- // If we have a handle add it to the cache.
6684
- pDomain->AddUnmanagedImageToCache (wszLibName, hmod);
6685
- }
6686
-
6687
6777
return hmod.Extract ();
6688
6778
}
6689
6779
0 commit comments