diff options
author | Swaroop Sridhar <Swaroop.Sridhar@microsoft.com> | 2019-01-05 16:40:59 -0800 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2019-01-05 16:40:59 -0800 |
commit | 110835b1b818b333f27ab76db3f223a03027698a (patch) | |
tree | 72b080215d0ade7e583ba2e1560e21548d54fedb /src | |
parent | dcf9f36c21022b8fce374559ec5fea8d11a0852b (diff) | |
download | coreclr-110835b1b818b333f27ab76db3f223a03027698a.tar.gz coreclr-110835b1b818b333f27ab76db3f223a03027698a.tar.bz2 coreclr-110835b1b818b333f27ab76db3f223a03027698a.zip |
Move Native Library APIs to NativeLibrary class (#21821)
Move APIs that manipulate native libraries from
System.Runtime.InteropServices.Marshal class to the new
System.Runtime.InteropServices.NativeLibrary class.
In API review: #32015,
The LoadLibrary APIs were originally Approved to live in
System.Runtime.InteropServices.Marshal class.
However, recently the decision was changed such that the APIs are
in a new NativeLibrary class.
Diffstat (limited to 'src')
-rw-r--r-- | src/System.Private.CoreLib/System.Private.CoreLib.csproj | 1 | ||||
-rw-r--r-- | src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs | 159 | ||||
-rw-r--r-- | src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs | 180 | ||||
-rw-r--r-- | src/vm/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/vm/ecalllist.h | 13 | ||||
-rw-r--r-- | src/vm/marshalnative.cpp | 63 | ||||
-rw-r--r-- | src/vm/marshalnative.h | 11 | ||||
-rw-r--r-- | src/vm/mscorlib.cpp | 1 | ||||
-rw-r--r-- | src/vm/nativelibrarynative.cpp | 74 | ||||
-rw-r--r-- | src/vm/nativelibrarynative.h | 26 |
10 files changed, 292 insertions, 238 deletions
diff --git a/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/System.Private.CoreLib.csproj index 7528d1283e..bd9d6501ce 100644 --- a/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -288,6 +288,7 @@ <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\InvalidComObjectException.cs" /> <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\InvalidOleVariantTypeException.cs" /> <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\Marshal.cs" /> + <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\NativeLibrary.cs" /> <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\NonPortable.cs" Condition="'$(FeatureCominterop)' != 'true'" /> <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMap.cs" /> <Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMarshal.cs" /> diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 594b758546..1ac33e00ba 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -1788,164 +1788,5 @@ namespace System.Runtime.InteropServices RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2)); FreeHGlobal(s); } - - /// APIs for managing Native Libraries - - /// <summary> - /// NativeLibrary Loader: Simple API - /// This method is a wrapper around OS loader, using "default" flags. - /// </summary> - /// <param name="libraryPath">The name of the native library to be loaded</param> - /// <returns>The handle for the loaded native library</returns> - /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> - /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> - /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> - public static IntPtr LoadLibrary(string libraryPath) - { - if (libraryPath == null) - throw new ArgumentNullException(nameof(libraryPath)); - - return LoadLibraryFromPath(libraryPath, throwOnError: true); - } - - /// <summary> - /// NativeLibrary Loader: Simple API that doesn't throw - /// </summary> - /// <param name="libraryPath">The name of the native library to be loaded</param> - /// <param name="handle">The out-parameter for the loaded native library handle</param> - /// <returns>True on successful load, false otherwise</returns> - /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> - public static bool TryLoadLibrary(string libraryPath, out IntPtr handle) - { - if (libraryPath == null) - throw new ArgumentNullException(nameof(libraryPath)); - - handle = LoadLibraryFromPath(libraryPath, throwOnError: false); - return handle != IntPtr.Zero; - } - - /// <summary> - /// NativeLibrary Loader: High-level API - /// Given a library name, this function searches specific paths based on the - /// runtime configuration, input parameters, and attributes of the calling assembly. - /// If DllImportSearchPath parameter is non-null, the flags in this enumeration are used. - /// Otherwise, the flags specified by the DefaultDllImportSearchPaths attribute on the - /// calling assembly (if any) are used. - /// This LoadLibrary() method does not invoke the managed call-backs for native library resolution: - /// * AssemblyLoadContext.LoadUnmanagedDll() - /// </summary> - /// <param name="libraryName">The name of the native library to be loaded</param> - /// <param name="assembly">The assembly loading the native library</param> - /// <param name="searchPath">The search path</param> - /// <returns>The handle for the loaded library</returns> - /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> - /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> - /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> - /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> - public static IntPtr LoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) - { - if (libraryName == null) - throw new ArgumentNullException(nameof(libraryName)); - if (assembly == null) - throw new ArgumentNullException(nameof(assembly)); - if (!(assembly is RuntimeAssembly)) - throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); - - return LoadLibraryByName(libraryName, - ((RuntimeAssembly)assembly).GetNativeHandle(), - searchPath.HasValue, - (uint) searchPath.GetValueOrDefault(), - throwOnError: true); - } - - /// <summary> - /// NativeLibrary Loader: High-level API that doesn't throw. - /// </summary> - /// <param name="libraryName">The name of the native library to be loaded</param> - /// <param name="searchPath">The search path</param> - /// <param name="assembly">The assembly loading the native library</param> - /// <param name="handle">The out-parameter for the loaded native library handle</param> - /// <returns>True on successful load, false otherwise</returns> - /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> - /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> - public static bool TryLoadLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle) - { - if (libraryName == null) - throw new ArgumentNullException(nameof(libraryName)); - if (assembly == null) - throw new ArgumentNullException(nameof(assembly)); - if (!(assembly is RuntimeAssembly)) - throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); - - handle = LoadLibraryByName(libraryName, - ((RuntimeAssembly)assembly).GetNativeHandle(), - searchPath.HasValue, - (uint) searchPath.GetValueOrDefault(), - throwOnError: false); - return handle != IntPtr.Zero; - } - - /// <summary> - /// Free a loaded library - /// Given a library handle, free it. - /// No action if the input handle is null. - /// </summary> - /// <param name="handle">The native library handle to be freed</param> - /// <exception cref="System.InvalidOperationException">If the operation fails</exception> - public static void FreeLibrary(IntPtr handle) - { - FreeNativeLibrary(handle); - } - - /// <summary> - /// Get the address of an exported Symbol - /// This is a simple wrapper around OS calls, and does not perform any name mangling. - /// </summary> - /// <param name="handle">The native library handle</param> - /// <param name="name">The name of the exported symbol</param> - /// <returns>The address of the symbol</returns> - /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> - /// <exception cref="System.EntryPointNotFoundException">If the symbol is not found</exception> - public static IntPtr GetLibraryExport(IntPtr handle, string name) - { - if (handle == IntPtr.Zero) - throw new ArgumentNullException(nameof(handle)); - if (name == null) - throw new ArgumentNullException(nameof(name)); - - return GetNativeLibraryExport(handle, name, throwOnError: true); - } - - /// <summary> - /// Get the address of an exported Symbol, but do not throw - /// </summary> - /// <param name="handle">The native library handle</param> - /// <param name="name">The name of the exported symbol</param> - /// <param name="address"> The out-parameter for the symbol address, if it exists</param> - /// <returns>True on success, false otherwise</returns> - /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> - public static bool TryGetLibraryExport(IntPtr handle, string name, out IntPtr address) - { - if (handle == IntPtr.Zero) - throw new ArgumentNullException(nameof(handle)); - if (name == null) - throw new ArgumentNullException(nameof(name)); - - address = GetNativeLibraryExport(handle, name, throwOnError: false); - return address != IntPtr.Zero; - } - - /// External functions that implement the NativeLibrary interface - - [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] - internal static extern IntPtr LoadLibraryFromPath(string libraryName, bool throwOnError); - [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] - internal static extern IntPtr LoadLibraryByName(string libraryName, RuntimeAssembly callingAssembly, - bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag, - bool throwOnError); - [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] - internal static extern void FreeNativeLibrary(IntPtr handle); - [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] - internal static extern IntPtr GetNativeLibraryExport(IntPtr handle, string symbolName, bool throwOnError); } } diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs new file mode 100644 index 0000000000..38712ac1b7 --- /dev/null +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs @@ -0,0 +1,180 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using System.Runtime.CompilerServices; +using System.Runtime.ConstrainedExecution; +using Win32Native = Microsoft.Win32.Win32Native; +using System.Diagnostics; + +namespace System.Runtime.InteropServices +{ + /// <summary> + /// APIs for managing Native Libraries + /// </summary> + public static partial class NativeLibrary + { + /// <summary> + /// NativeLibrary Loader: Simple API + /// This method is a wrapper around OS loader, using "default" flags. + /// </summary> + /// <param name="libraryPath">The name of the native library to be loaded</param> + /// <returns>The handle for the loaded native library</returns> + /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> + /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> + /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> + public static IntPtr Load(string libraryPath) + { + if (libraryPath == null) + throw new ArgumentNullException(nameof(libraryPath)); + + return LoadFromPath(libraryPath, throwOnError: true); + } + + /// <summary> + /// NativeLibrary Loader: Simple API that doesn't throw + /// </summary> + /// <param name="libraryPath">The name of the native library to be loaded</param> + /// <param name="handle">The out-parameter for the loaded native library handle</param> + /// <returns>True on successful load, false otherwise</returns> + /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> + public static bool TryLoad(string libraryPath, out IntPtr handle) + { + if (libraryPath == null) + throw new ArgumentNullException(nameof(libraryPath)); + + handle = LoadFromPath(libraryPath, throwOnError: false); + return handle != IntPtr.Zero; + } + + /// <summary> + /// NativeLibrary Loader: High-level API + /// Given a library name, this function searches specific paths based on the + /// runtime configuration, input parameters, and attributes of the calling assembly. + /// If DllImportSearchPath parameter is non-null, the flags in this enumeration are used. + /// Otherwise, the flags specified by the DefaultDllImportSearchPaths attribute on the + /// calling assembly (if any) are used. + /// This LoadLibrary() method does not invoke the managed call-backs for native library resolution: + /// * AssemblyLoadContext.LoadUnmanagedDll() + /// </summary> + /// <param name="libraryName">The name of the native library to be loaded</param> + /// <param name="assembly">The assembly loading the native library</param> + /// <param name="searchPath">The search path</param> + /// <returns>The handle for the loaded library</returns> + /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> + /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> + /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> + /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> + public static IntPtr Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + if (libraryName == null) + throw new ArgumentNullException(nameof(libraryName)); + if (assembly == null) + throw new ArgumentNullException(nameof(assembly)); + if (!(assembly is RuntimeAssembly)) + throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); + + return LoadByName(libraryName, + ((RuntimeAssembly)assembly).GetNativeHandle(), + searchPath.HasValue, + (uint) searchPath.GetValueOrDefault(), + throwOnError: true); + } + + /// <summary> + /// NativeLibrary Loader: High-level API that doesn't throw. + /// </summary> + /// <param name="libraryName">The name of the native library to be loaded</param> + /// <param name="searchPath">The search path</param> + /// <param name="assembly">The assembly loading the native library</param> + /// <param name="handle">The out-parameter for the loaded native library handle</param> + /// <returns>True on successful load, false otherwise</returns> + /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> + /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> + public static bool TryLoad(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle) + { + if (libraryName == null) + throw new ArgumentNullException(nameof(libraryName)); + if (assembly == null) + throw new ArgumentNullException(nameof(assembly)); + if (!(assembly is RuntimeAssembly)) + throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); + + handle = LoadByName(libraryName, + ((RuntimeAssembly)assembly).GetNativeHandle(), + searchPath.HasValue, + (uint) searchPath.GetValueOrDefault(), + throwOnError: false); + return handle != IntPtr.Zero; + } + + /// <summary> + /// Free a loaded library + /// Given a library handle, free it. + /// No action if the input handle is null. + /// </summary> + /// <param name="handle">The native library handle to be freed</param> + /// <exception cref="System.InvalidOperationException">If the operation fails</exception> + public static void Free(IntPtr handle) + { + FreeLib(handle); + } + + /// <summary> + /// Get the address of an exported Symbol + /// This is a simple wrapper around OS calls, and does not perform any name mangling. + /// </summary> + /// <param name="handle">The native library handle</param> + /// <param name="name">The name of the exported symbol</param> + /// <returns>The address of the symbol</returns> + /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> + /// <exception cref="System.EntryPointNotFoundException">If the symbol is not found</exception> + public static IntPtr GetExport(IntPtr handle, string name) + { + if (handle == IntPtr.Zero) + throw new ArgumentNullException(nameof(handle)); + if (name == null) + throw new ArgumentNullException(nameof(name)); + + return GetSymbol(handle, name, throwOnError: true); + } + + /// <summary> + /// Get the address of an exported Symbol, but do not throw + /// </summary> + /// <param name="handle">The native library handle</param> + /// <param name="name">The name of the exported symbol</param> + /// <param name="address"> The out-parameter for the symbol address, if it exists</param> + /// <returns>True on success, false otherwise</returns> + /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> + public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) + { + if (handle == IntPtr.Zero) + throw new ArgumentNullException(nameof(handle)); + if (name == null) + throw new ArgumentNullException(nameof(name)); + + address = GetSymbol(handle, name, throwOnError: false); + return address != IntPtr.Zero; + } + + /// External functions that implement the NativeLibrary interface + + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] + internal static extern IntPtr LoadFromPath(string libraryName, bool throwOnError); + + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] + internal static extern IntPtr LoadByName(string libraryName, RuntimeAssembly callingAssembly, + bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag, + bool throwOnError); + + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] + internal static extern void FreeLib(IntPtr handle); + + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] + internal static extern IntPtr GetSymbol(IntPtr handle, string symbolName, bool throwOnError); + } +} diff --git a/src/vm/CMakeLists.txt b/src/vm/CMakeLists.txt index 93ccb83f5f..9bb26d402d 100644 --- a/src/vm/CMakeLists.txt +++ b/src/vm/CMakeLists.txt @@ -355,6 +355,7 @@ set(VM_SOURCES_WKS multicorejitplayer.cpp # Condition="'$(FeatureMulticoreJIT)' == 'true' nativeeventsource.cpp nativeoverlapped.cpp + nativelibrarynative.cpp objectlist.cpp olevariant.cpp pendingload.cpp @@ -473,6 +474,7 @@ set(VM_HEADERS_WKS multicorejitimpl.h nativeeventsource.h nativeoverlapped.h + nativelibrarynative.h objectlist.h olevariant.h pendingload.h diff --git a/src/vm/ecalllist.h b/src/vm/ecalllist.h index 05ced0fc68..89ed6fa95e 100644 --- a/src/vm/ecalllist.h +++ b/src/vm/ecalllist.h @@ -832,11 +832,6 @@ FCFuncStart(gInteropMarshalFuncs) FCFuncElement("GetDelegateForFunctionPointerInternal", MarshalNative::GetDelegateForFunctionPointerInternal) FCFuncElement("GetFunctionPointerForDelegateInternal", MarshalNative::GetFunctionPointerForDelegateInternal) - QCFuncElement("LoadLibraryFromPath", MarshalNative::LoadLibraryFromPath) - QCFuncElement("LoadLibraryByName", MarshalNative::LoadLibraryByName) - QCFuncElement("FreeNativeLibrary", MarshalNative::FreeNativeLibrary) - QCFuncElement("GetNativeLibraryExport", MarshalNative::GetNativeLibraryExport) - #ifdef FEATURE_COMINTEROP FCFuncElement("GetHRForException", MarshalNative::GetHRForException) FCFuncElement("GetHRForException_WinRT", MarshalNative::GetHRForException_WinRT) @@ -871,6 +866,13 @@ FCFuncStart(gInteropMarshalFuncs) #endif // FEATURE_COMINTEROP FCFuncEnd() +FCFuncStart(gInteropNativeLibraryFuncs) + QCFuncElement("LoadFromPath", NativeLibraryNative::LoadFromPath) + QCFuncElement("LoadByName", NativeLibraryNative::LoadByName) + QCFuncElement("FreeLib", NativeLibraryNative::FreeLib) + QCFuncElement("GetSymbol", NativeLibraryNative::GetSymbol) +FCFuncEnd() + FCFuncStart(gArrayWithOffsetFuncs) FCFuncElement("CalculateCount", MarshalNative::CalculateCount) FCFuncEnd() @@ -1278,6 +1280,7 @@ FCClassElement("MngdSafeArrayMarshaler", "System.StubHelpers", gMngdSafeArrayMar FCClassElement("ModuleBuilder", "System.Reflection.Emit", gCOMModuleBuilderFuncs) FCClassElement("ModuleHandle", "System", gCOMModuleHandleFuncs) FCClassElement("Monitor", "System.Threading", gMonitorFuncs) +FCClassElement("NativeLibrary", "System.Runtime.InteropServices", gInteropNativeLibraryFuncs) #ifdef FEATURE_COMINTEROP FCClassElement("OAVariantLib", "Microsoft.Win32", gOAVariantFuncs) #endif diff --git a/src/vm/marshalnative.cpp b/src/vm/marshalnative.cpp index a2cb827d34..e2b248289b 100644 --- a/src/vm/marshalnative.cpp +++ b/src/vm/marshalnative.cpp @@ -923,69 +923,6 @@ FCIMPL1(int, MarshalNative::GetHRForException_WinRT, Object* eUNSAFE) } FCIMPLEND -// static -INT_PTR QCALLTYPE MarshalNative::LoadLibraryFromPath(LPCWSTR path, BOOL throwOnError) -{ - QCALL_CONTRACT; - - NATIVE_LIBRARY_HANDLE handle = nullptr; - - BEGIN_QCALL; - - handle = NDirect::LoadLibraryFromPath(path, throwOnError); - - END_QCALL; - - return reinterpret_cast<INT_PTR>(handle); -} - -// static -INT_PTR QCALLTYPE MarshalNative::LoadLibraryByName(LPCWSTR name, QCall::AssemblyHandle callingAssembly, - BOOL hasDllImportSearchPathFlag, DWORD dllImportSearchPathFlag, - BOOL throwOnError) -{ - QCALL_CONTRACT; - - NATIVE_LIBRARY_HANDLE handle = nullptr; - Assembly *pAssembly = callingAssembly->GetAssembly(); - - BEGIN_QCALL; - - handle = NDirect::LoadLibraryByName(name, pAssembly, hasDllImportSearchPathFlag, dllImportSearchPathFlag, throwOnError); - - END_QCALL; - - return reinterpret_cast<INT_PTR>(handle); -} - -// static -void QCALLTYPE MarshalNative::FreeNativeLibrary(INT_PTR handle) -{ - QCALL_CONTRACT; - - BEGIN_QCALL; - - NDirect::FreeNativeLibrary((NATIVE_LIBRARY_HANDLE) handle); - - END_QCALL; -} - -//static -INT_PTR QCALLTYPE MarshalNative::GetNativeLibraryExport(INT_PTR handle, LPCWSTR symbolName, BOOL throwOnError) -{ - QCALL_CONTRACT; - - INT_PTR address = NULL; - - BEGIN_QCALL; - - address = NDirect::GetNativeLibraryExport((NATIVE_LIBRARY_HANDLE)handle, symbolName, throwOnError); - - END_QCALL; - - return address; -} - #ifdef FEATURE_COMINTEROP //==================================================================== diff --git a/src/vm/marshalnative.h b/src/vm/marshalnative.h index ddc8351094..85be41a86b 100644 --- a/src/vm/marshalnative.h +++ b/src/vm/marshalnative.h @@ -85,17 +85,6 @@ public: static FCDECL2(Object*, GetDelegateForFunctionPointerInternal, LPVOID FPtr, ReflectClassBaseObject* refTypeUNSAFE); static FCDECL1(LPVOID, GetFunctionPointerForDelegateInternal, Object* refDelegateUNSAFE); - - //==================================================================== - // These methods provide the native callbacks for library loading APIs - //==================================================================== - static INT_PTR QCALLTYPE LoadLibraryFromPath(LPCWSTR path, BOOL throwOnError); - static INT_PTR QCALLTYPE LoadLibraryByName(LPCWSTR name, QCall::AssemblyHandle callingAssembly, - BOOL hasDllImportSearchPathFlag, DWORD dllImportSearchPathFlag, - BOOL throwOnError); - static void QCALLTYPE FreeNativeLibrary(INT_PTR handle); - static INT_PTR QCALLTYPE GetNativeLibraryExport(INT_PTR handle, LPCWSTR symbolName, BOOL throwOnError); - #ifdef FEATURE_COMINTEROP //==================================================================== // map GUID to Type diff --git a/src/vm/mscorlib.cpp b/src/vm/mscorlib.cpp index 4fa94499db..e69cfb8774 100644 --- a/src/vm/mscorlib.cpp +++ b/src/vm/mscorlib.cpp @@ -35,6 +35,7 @@ #include "clrconfignative.h" #include "commodule.h" #include "marshalnative.h" +#include "nativelibrarynative.h" #include "system.h" #include "comutilnative.h" #include "comsynchronizable.h" diff --git a/src/vm/nativelibrarynative.cpp b/src/vm/nativelibrarynative.cpp new file mode 100644 index 0000000000..c5bf24f06e --- /dev/null +++ b/src/vm/nativelibrarynative.cpp @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// +// File: NativeLibraryNative.cpp +// + +#include "common.h" +#include "dllimport.h" +#include "nativelibrarynative.h" + +// static +INT_PTR QCALLTYPE NativeLibraryNative::LoadFromPath(LPCWSTR path, BOOL throwOnError) +{ + QCALL_CONTRACT; + + NATIVE_LIBRARY_HANDLE handle = nullptr; + + BEGIN_QCALL; + + handle = NDirect::LoadLibraryFromPath(path, throwOnError); + + END_QCALL; + + return reinterpret_cast<INT_PTR>(handle); +} + +// static +INT_PTR QCALLTYPE NativeLibraryNative::LoadByName(LPCWSTR name, QCall::AssemblyHandle callingAssembly, + BOOL hasDllImportSearchPathFlag, DWORD dllImportSearchPathFlag, + BOOL throwOnError) +{ + QCALL_CONTRACT; + + NATIVE_LIBRARY_HANDLE handle = nullptr; + Assembly *pAssembly = callingAssembly->GetAssembly(); + + BEGIN_QCALL; + + handle = NDirect::LoadLibraryByName(name, pAssembly, hasDllImportSearchPathFlag, dllImportSearchPathFlag, throwOnError); + + END_QCALL; + + return reinterpret_cast<INT_PTR>(handle); +} + +// static +void QCALLTYPE NativeLibraryNative::FreeLib(INT_PTR handle) +{ + QCALL_CONTRACT; + + BEGIN_QCALL; + + NDirect::FreeNativeLibrary((NATIVE_LIBRARY_HANDLE) handle); + + END_QCALL; +} + +//static +INT_PTR QCALLTYPE NativeLibraryNative::GetSymbol(INT_PTR handle, LPCWSTR symbolName, BOOL throwOnError) +{ + QCALL_CONTRACT; + + INT_PTR address = NULL; + + BEGIN_QCALL; + + address = NDirect::GetNativeLibraryExport((NATIVE_LIBRARY_HANDLE)handle, symbolName, throwOnError); + + END_QCALL; + + return address; +} + diff --git a/src/vm/nativelibrarynative.h b/src/vm/nativelibrarynative.h new file mode 100644 index 0000000000..e22031cad6 --- /dev/null +++ b/src/vm/nativelibrarynative.h @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// +// File: NativeLibraryNative.h +// +// +// QCall's for the NativeLibrary class +// + +#ifndef __NATIVELIBRARYNATIVE_H__ +#define __NATIVELIBRARYNATIVE_H__ + +class NativeLibraryNative +{ +public: + static INT_PTR QCALLTYPE LoadFromPath(LPCWSTR path, BOOL throwOnError); + static INT_PTR QCALLTYPE LoadByName(LPCWSTR name, QCall::AssemblyHandle callingAssembly, + BOOL hasDllImportSearchPathFlag, DWORD dllImportSearchPathFlag, + BOOL throwOnError); + static void QCALLTYPE FreeLib(INT_PTR handle); + static INT_PTR QCALLTYPE GetSymbol(INT_PTR handle, LPCWSTR symbolName, BOOL throwOnError); + +}; + +#endif // __NATIVELIBRARYNATIVE_H__ |