从 UWP C# 应用程序使用 pinvoke 调用 LoadLibrary
Calling LoadLibrary using pinvoke from UWP C# application
我正在尝试从 C# UWP 应用程序的非托管 dll 中调用方法。我这样做了,但是在非托管 dll 上调用 "LoadLibrary()" 以便我可以使用它。
这在调试模式下一切正常,但在发布模式下,我收到一个奇怪的错误:
消息:Class 初始化方法 Tests.UnitTests.InitializeClient 抛出异常。 System.TypeLoadException:System.TypeLoadException:程序集 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中的 P/Invoke 方法 'LoadLibrary!kernel32' 未解决,因为它在 UWP 应用程序中不可用。请使用另一个 API 或使用 [DllImport(ExactSpelling=true) 来表明您了解使用非 UWP 应用程序 APIs..
的含义
这是我调用加载库的方法:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string librayName);
不幸的是,如果我添加 "ExactSpelling = true" 如下:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr LoadLibrary(string librayName);
然后调用它抛出异常:
System.EntryPointNotFoundException: '无法在 DLL 'kernel32' 中找到名为 'LoadLibrary' 的入口点。'
非常感谢任何帮助!
改为使用 LoadPackagedLibrary:
[DllImport("API-MS-WIN-CORE-LIBRARYLOADER-L2-1-0.DLL", SetLastError = true)]
public static extern IntPtr LoadPackagedLibrary([MarshalAs(UnmanagedType.LPWStr)]string libraryName, int reserved = 0);
我正在尝试从 C# UWP 应用程序的非托管 dll 中调用方法。我这样做了,但是在非托管 dll 上调用 "LoadLibrary()" 以便我可以使用它。
这在调试模式下一切正常,但在发布模式下,我收到一个奇怪的错误:
消息:Class 初始化方法 Tests.UnitTests.InitializeClient 抛出异常。 System.TypeLoadException:System.TypeLoadException:程序集 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中的 P/Invoke 方法 'LoadLibrary!kernel32' 未解决,因为它在 UWP 应用程序中不可用。请使用另一个 API 或使用 [DllImport(ExactSpelling=true) 来表明您了解使用非 UWP 应用程序 APIs..
的含义这是我调用加载库的方法:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string librayName);
不幸的是,如果我添加 "ExactSpelling = true" 如下:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr LoadLibrary(string librayName);
然后调用它抛出异常:
System.EntryPointNotFoundException: '无法在 DLL 'kernel32' 中找到名为 'LoadLibrary' 的入口点。'
非常感谢任何帮助!
改为使用 LoadPackagedLibrary:
[DllImport("API-MS-WIN-CORE-LIBRARYLOADER-L2-1-0.DLL", SetLastError = true)]
public static extern IntPtr LoadPackagedLibrary([MarshalAs(UnmanagedType.LPWStr)]string libraryName, int reserved = 0);