C# 如何解决 .NET CF 异常“找不到 PInvoke DLL”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/319958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I solve the .NET CF exception "Can't find PInvoke DLL"?
提问by Ignas Limanauskas
This is to all the C# gurus. I have been banging my head on this for some time already, tried all kinds of advice on the net with no avail. The action is happening in Windows Mobile 5.0.
这是给所有 C# 大师的。我已经为此纠结了一段时间,在网上尝试了各种建议,但无济于事。该操作发生在 Windows Mobile 5.0 中。
I have a DLL named MyDll.dll. In the MyDll.h I have:
我有一个名为 MyDll.dll 的 DLL。在 MyDll.h 我有:
extern "C" __declspec(dllexport) int MyDllFunction(int one, int two);
The definition of MyDllFunction in MyDll.cpp is:
MyDll.cpp 中 MyDllFunction 的定义是:
int MyDllFunction(int one, int two)
{
return one + two;
}
The C# class contains the following declaration:
C# 类包含以下声明:
[DllImport("MyDll.dll")]
extern public static int MyDllFunction(int one, int two);
In the same class I am calling MyDllFunction the following way:
在同一个类中,我通过以下方式调用 MyDllFunction:
int res = MyDllFunction(10, 10);
And this is where the bloody thing keeps giving me "Can't find PInvoke DLL 'MyDll.dll'". I have verified that I can actually do the PInvoke on system calls, such as "GetAsyncKeyState(1)", declared as:
这就是该死的事情不断给我“找不到 PInvoke DLL 'MyDll.dll'”的地方。我已经验证我实际上可以对系统调用执行 PInvoke,例如“GetAsyncKeyState(1)”,声明为:
[DllImport("coredll.dll")]
protected static extern short GetAsyncKeyState(int vKey);
The MyDll.dll is in the same folder as the executable, and I have also tried putting it into the /Windows folder with no changes nor success. Any advice or solutions are greatly appreciated.
MyDll.dll 与可执行文件位于同一文件夹中,我也尝试将其放入 /Windows 文件夹中,但没有任何更改,也没有成功。非常感谢任何建议或解决方案。
采纳答案by Michael Trausch
Maybe this seems like an obvious thing to check, but are you compiling the native DLL for the correct CPU architecture? IIRC, Windows Mobile runs on multiple CPU architectures.
也许这似乎是一个显而易见的检查,但是您是否正在为正确的 CPU 架构编译本机 DLL?IIRC,Windows Mobile 在多个 CPU 架构上运行。
回答by JaredPar
The exception you listed and the pinvoke signature you put in the question have different names: MyDll.dll and ThreadBenchLib.dll respectively. Was that a typo or the problem?
您列出的异常和您在问题中放置的 pinvoke 签名具有不同的名称:分别是 MyDll.dll 和 ThreadBenchLib.dll。这是打字错误还是问题?
If it's not the problem try opening the DLL in depends. It's possible the DLL load is failing because of an unmet dependency.
如果这不是问题,请尝试在depends 中打开 DLL 。由于未满足的依赖关系,DLL 加载可能失败。
回答by Michael Trausch
I have the same problem, but this time there is no obvious difference in the platforms involved. Why is it something that is supposed to be 'normal' is so badly documented, so difficult and worst of all so 'flaky'. Are there ANY tools that allow me to go onto my PPC emulator, and stop by step check (a) that the dll is where CLR expects it (I have put it both in the same directory, and the windows directory on the emulator - I can see it is there usign the emulators file explorer), (b) that CLR can load it (has enough memory etc), (c) that CLR can then find what functions are present (useful if I can get beyond the stupid 'can't find the dll' error)
我有同样的问题,但这次涉及的平台没有明显差异。为什么应该是“正常”的东西被记录得如此糟糕,如此困难,最糟糕的是如此“片状”。是否有任何工具可以让我进入我的 PPC 模拟器,并逐步检查 (a) dll 是否是 CLR 期望它的位置(我已将它放在同一个目录中,并将其放在模拟器上的 windows 目录中 - 我可以看到它在那里使用模拟器文件资源管理器),(b)CLR 可以加载它(有足够的内存等),(c)CLR 然后可以找到存在哪些函数(如果我能超越愚蠢的“可以'找不到 dll' 错误)
I'd love to force Gates or indeed any of the 'windows mobile' team to use the crap they create.
我很想强迫盖茨或任何“windows mobile”团队使用他们创造的废话。
回答by user2321654
I am having the same problem. I used the Depends.exe to look for WinCE dll dependencies, and It depends of debug dll. (MSVCR90D.dll) To solve the problem I compiled a release version, and confirm the dependencies, after that it works fine.
我有同样的问题。我使用 Depends.exe 来查找 WinCE dll 依赖项,它取决于调试 dll。(MSVCR90D.dll) 为了解决这个问题,我编译了一个发布版本,并确认了依赖关系,之后就可以正常工作了。
I hope to help
我希望有所帮助