c# - 从 UWP C# 应用程序使用 pinvoke 调用 LoadLibrary

标签 c# uwp pinvoke

我正在尝试从 C# UWP 应用程序的非托管 dll 调用方法。我这样做,但在非托管 dll 上调用“LoadLibrary()”以便我可以使用它。

这在 Debug模式下一切正常,但在 Release模式下,我收到一个奇怪的错误:

消息:类初始化方法 Tests.UnitTests.InitializeClient 抛出异常。 System.TypeLoadException: System.TypeLoadException: Unresolved P/Invoke method 'LoadLibrary!kernel32' in assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 因为它在 UWP 应用程序中不可用。请使用另一个 API,或使用 [DllImport(ExactSpelling=true) 表明您了解使用非 UWP 应用程序 API 的含义..

这是我调用 Load Library 的方法:

    [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 C# 应用程序使用 pinvoke 调用 LoadLibrary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620704/

相关文章:

即使使用 MobileServiceClient - UWP 成功登录后,Azure MobileApp API 也会返回 401(未经授权)

c# - 尝试从 UWP 访问 OneDrive 根目录时出错

c# - native 上下文菜单 c#

c# - 调试从 C# 调用的 VC++ 6 DLL

c# - 从字符串数组中替换所有出现的字符串

c# - 后台Worker和数据表的问题

c# - 使用 SemanticZoom 显示按字母顺序排列的标题,就像在“开始”菜单上一样

c# - HttpClient.PostAsync 第一次慢

c# - 从 Application_Error 抛出 http 异常(未经授权)

visual-studio - Visual Studio 单元测试未使用 PInvoke 找到我的 DLL。我怎样才能解决这个问题?