【发布时间】:2014-05-26 10:39:25
【问题描述】:
考虑以下解决方案结构:Windows Store C# 应用程序 + 2 个本机库 PInvokeServer 和 PInvokeServer1。原生库代码:
// PInvokeServer1
PINVOKESERVER1_API int TestFunction1(void)
{
return 5;
}
// PInvokeServer
PINVOKESERVER_API int TestFunction(void)
{
return TestFunction1();
}
这两个函数都是extern C。 PInvokeServer 依赖于 PInvokeServer1(使用链接器依赖项)。 PInvokeServer.dll 和 PInvokeServer1.dll 通过构建操作内容添加到 C# 项目中,因此它们是应用程序包的一部分。 C# 声明:
const string dllName = @"Native\PInvokeServer.dll";
const string dllName1 = @"Native\PInvokeServer1.dll";
[System.Runtime.InteropServices.DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TestFunction();
[System.Runtime.InteropServices.DllImport(dllName1, CallingConvention = CallingConvention.Cdecl)]
public static extern int TestFunction1();
案例 1,不起作用(未找到模块):
TestFunction();
案例 2,有效:
TestFunction1();
案例 3,有效:
TestFunction1();
TestFunction();
案例 1:当 PInvoke 尝试加载 PInvokeServer.dll 时,它无法解析本机运行时依赖,PInvokeServer1.dll 未加载,并且我得到 Module not found 异常。例如,将 PInvokeServer1.dll 放置到 System32 目录没有帮助。
案例 2:PInvoke 能够直接加载 PInvokeServer1.dll。
案例3,一旦PInvokeServer1.dll加载完成,PInvokeServer.dll也可以加载成功。
我是我真正的程序,我有依赖于其他几个库的本地 C 库。所有这些库都添加到 C# Store 应用程序包中。但是无法加载高级库,因为 PInvoke 无法加载依赖项。我能想到的唯一方法是使用LoadLibrary PInvoke 调用加载低级库,最后使用PInvoke 调用高级库。有没有更好的办法?
【问题讨论】:
标签: c# dll windows-8 pinvoke loadlibrary