问题描述
我正在尝试从C#UWP应用程序中的非托管dll调用方法.我这样做了,但是在非托管dll上使用了"LoadLibrary()",这样我就可以使用它了.
I'm trying to call methods from an unmanaged dll from a C# UWP application. I do this, but pinvoking "LoadLibrary()" on the unmanaged dll so that I can use it.
这一切在Debug模式下都可以正常工作,但是在Release模式下,我得到一个奇怪的错误:
This all works fine in Debug mode, however in Release mode, I get a curious error:
消息:类初始化方法Tests.UnitTests.InitializeClient抛出异常. System.TypeLoadException:System.TypeLoadException:程序集"Client"中的P/Invoke方法"LoadLibrary!kernel32"未解决,版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null,因为在UWP应用程序中不可用.请使用其他API或使用[DllImport(ExactSpelling = true)表示您了解使用非UWP应用程序API的含义.
Message: Class Initialization method Tests.UnitTests.InitializeClient threw exception. System.TypeLoadException: System.TypeLoadException: Unresolved P/Invoke method 'LoadLibrary!kernel32' in assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it is not available in UWP applications. Please either use an another API , or use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP application APIs..
这是我定义负载库的方法:
Here is my method to pinvoke Load Library:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string librayName);
不幸的是,如果我添加"ExactSpelling = true",如下所示:
Unfortunately, if I add the "ExactSpelling = true" as below:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr LoadLibrary(string librayName);
然后调用它会引发异常:
Then calling it throws an exception:
System.EntryPointNotFoundException:'无法在DLL'kernel32'中找到名为'LoadLibrary'的入口点.'
System.EntryPointNotFoundException: 'Unable to find an entry point named 'LoadLibrary' in DLL 'kernel32'.'
非常感谢您的帮助!
推荐答案
请改用LoadPackagedLibrary:
Use LoadPackagedLibrary instead:
[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);
这篇关于使用UWP C#应用程序中的pinvoke调用LoadLibrary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!