internal static unsafe void FixupModuleCell(ModuleFixupCell *pCell) { string moduleName = GetModuleName(pCell); uint dllImportSearchPath = 0; bool hasDllImportSearchPath = (pCell->DllImportSearchPathAndCookie & InteropDataConstants.HasDllImportSearchPath) != 0; if (hasDllImportSearchPath) { dllImportSearchPath = pCell->DllImportSearchPathAndCookie & ~InteropDataConstants.HasDllImportSearchPath; } Assembly callingAssembly = RuntimeAugments.Callbacks.GetAssemblyForHandle(new RuntimeTypeHandle(pCell->CallingAssemblyType)); // First check if there's a NativeLibrary callback and call it to attempt the resolution IntPtr hModule = NativeLibrary.LoadLibraryCallbackStub(moduleName, callingAssembly, hasDllImportSearchPath, dllImportSearchPath); if (hModule == IntPtr.Zero) { // NativeLibrary callback didn't resolve the library. Use built-in rules. NativeLibrary.LoadLibErrorTracker loadLibErrorTracker = default; hModule = NativeLibrary.LoadBySearch( callingAssembly, searchAssemblyDirectory: false, dllImportSearchPathFlags: 0, ref loadLibErrorTracker, moduleName); if (hModule == IntPtr.Zero) { // Built-in rules didn't resolve the library. Use AssemblyLoadContext as a last chance attempt. AssemblyLoadContext loadContext = AssemblyLoadContext.GetLoadContext(callingAssembly) !; hModule = loadContext.GetResolvedUnmanagedDll(callingAssembly, moduleName); } if (hModule == IntPtr.Zero) { // If the module is still unresolved, this is an error. loadLibErrorTracker.Throw(moduleName); } } Debug.Assert(hModule != IntPtr.Zero); var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero); if (oldValue != IntPtr.Zero) { // Some other thread won the race to fix it up. FreeLibrary(hModule); } }
internal static unsafe IntPtr ResolvePInvokeSlow(MethodFixupCell *pCell) { ModuleFixupCell *pModuleCell = pCell->Module; IntPtr hModule = pModuleCell->Handle; if (hModule == IntPtr.Zero) { FixupModuleCell(pModuleCell); hModule = pModuleCell->Handle; } FixupMethodCell(hModule, pCell); return(pCell->Target); }
internal static unsafe void FixupModuleCell(ModuleFixupCell *pCell) { string moduleName = GetModuleName(pCell); IntPtr hModule = TryResolveModule(moduleName); if (hModule != IntPtr.Zero) { var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero); if (oldValue != IntPtr.Zero) { // Some other thread won the race to fix it up. FreeLibrary(hModule); } } else { throw new DllNotFoundException(SR.Format(SR.Arg_DllNotFoundExceptionParameterized, moduleName)); } }
internal static unsafe IntPtr ResolvePInvokeSlow(MethodFixupCell *pCell) { int lastSystemError = Marshal.GetLastSystemError(); ModuleFixupCell *pModuleCell = pCell->Module; IntPtr hModule = pModuleCell->Handle; if (hModule == IntPtr.Zero) { FixupModuleCell(pModuleCell); hModule = pModuleCell->Handle; } FixupMethodCell(hModule, pCell); Marshal.SetLastSystemError(lastSystemError); return(pCell->Target); }
internal static unsafe void FixupModuleCell(ModuleFixupCell *pCell) { #if !PLATFORM_UNIX char *moduleName = (char *)pCell->ModuleName; IntPtr hModule = Interop.mincore.LoadLibraryEx(moduleName, IntPtr.Zero, 0); if (hModule != IntPtr.Zero) { var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero); if (oldValue != IntPtr.Zero) { // Some other thread won the race to fix it up. Interop.mincore.FreeLibrary(hModule); } } else { // TODO: should be DllNotFoundException, but layering... throw new TypeLoadException(new string(moduleName)); } #else byte *moduleName = (byte *)pCell->ModuleName; IntPtr hModule = Interop.Sys.LoadLibrary(moduleName); if (hModule != IntPtr.Zero) { var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero); if (oldValue != IntPtr.Zero) { // Some other thread won the race to fix it up. Interop.Sys.FreeLibrary(hModule); } } else { // TODO: should be DllNotFoundException, but layering... throw new TypeLoadException(Encoding.UTF8.GetString(moduleName, strlen(moduleName))); } #endif }
internal static unsafe void FixupModuleCell(ModuleFixupCell *pCell) { byte * pModuleName = (byte *)pCell->ModuleName; string moduleName = Encoding.UTF8.GetString(pModuleName, strlen(pModuleName)); IntPtr hModule = TryResolveModule(moduleName); if (hModule != IntPtr.Zero) { var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero); if (oldValue != IntPtr.Zero) { // Some other thread won the race to fix it up. FreeLibrary(hModule); } } else { // TODO: should be DllNotFoundException, but layering... throw new TypeLoadException(moduleName); } }
private static unsafe string GetModuleName(ModuleFixupCell *pCell) { byte *pModuleName = (byte *)pCell->ModuleName; return(Encoding.UTF8.GetString(pModuleName, strlen(pModuleName))); }