.net 在 Windows Mobile 中找不到 PInvoke DLL 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1456093/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:21:06  来源:igfitidea点击:

Can't find PInvoke DLL error in Windows Mobile

.netwindows-mobilecompact-frameworkpinvokeemulation

提问by Dilip

I am having a lot of trouble getting a basic scenario to work on windows mobile 5.0 emulator. I have a winforms app that eventually calls into native code. Deployment works fine and all the native DLLs are copied in the same folder as the winforms .exe. I also verified this is the case with Remote File Viewer tool.

我在使用 Windows Mobile 5.0 模拟器的基本场景时遇到了很多麻烦。我有一个最终调用本机代码的 winforms 应用程序。部署工作正常,所有本机 DLL 都复制到与 winforms .exe 相同的文件夹中。我还验证了远程文件查看器工具的情况。

However when I launch my app, it always fails with "Can't find PInvoke dll -- System.MissingMethodException" error (when the time comes to call into native code, the DllImport attribute is rendered useless). I knowthat the native dll is found in the same folder as the executable. What more should I do?

但是,当我启动我的应用程序时,它总是以“无法找到 PInvoke dll -- System.MissingMethodException”错误(当调用本机代码时,DllImport 属性变得无用)而失败。我知道本机 dll 与可执行文件位于同一文件夹中。我还应该做什么?

I am using VS 2008.

我正在使用 VS 2008。

回答by ctacke

To extend Jared's answer, four more common reasons to get a MissingMethodException while P/Invoking in the CF:

为了扩展 Jared 的回答,在 CF 中 P/Invoking 时获得 MissingMethodException 的四个常见原因:

  1. You are missing dependenciesof the native library you are calling into.
  2. The native assmebly was compiled for the wrong subsystem (i.e. desktop, not CE)
  3. The native assembly was compiled for the wrong processor (i.e. x86 and not ARM)
  4. You don't have enough virtual memory for the DLL to load.
  1. 您缺少正在调用的本机库的依赖项。
  2. 本机汇编编译为错误的子系统(即桌面,而不是 CE)
  3. 本机程序集是为错误的处理器编译的(即 x86 而不是 ARM)
  4. 您没有足够的虚拟内存来加载 DLL。

Have you verified the DLL entry points are undecorated with something like dumpbin?

您是否已验证 DLL 入口点未使用dumpbin 之类的东西进行修饰?

回答by JaredPar

Given the error message there are usually one of 2 problems

鉴于错误消息,通常有两个问题之一

  1. It can't find the DLL. The DLL is found by looking at the executing directory and the PATH environment variable
  2. It can't find the function within the DLL. Have you checked to make sure both the declaration and definition of the DLL are both extern "C" and marked as __declspec(dllexport)
  1. 它找不到DLL。通过查看执行目录和PATH环境变量找到DLL
  2. 它在 DLL 中找不到该函数。您是否检查过以确保 DLL 的声明和定义都是 extern "C" 并标记为__declspec(dllexport)

Also, sanity check is to make sure the DLL name is spelled correctly and lacking the .dll suffix.

此外,健全性检查是为了确保 DLL 名称拼写正确且缺少 .dll 后缀。

回答by Alain Holloway

Your problem is due to the fact that WM5 memory managment is crap. DLLs are loaded from top of slot to bottom while apps are loaded from bottom to top. If you don't have enough space between your app and your DLL, you will receive a "can't pinvoke" error.

您的问题是由于 WM5 内存管理是垃圾。DLL 从插槽顶部到底部加载,而应用程序从底部到顶部加载。如果您的应用程序和 DLL 之间没有足够的空间,您将收到“无法 pinvoke”错误。

WM5 allocates 32 slots of 32Mb for applications to run into.

WM5 为应用程序分配了 32 个 32Mb 的插槽。

Each time WM5 allocates memory for dll, it uses a minimum of 64Kb block, so if your DLL is 32K, it will take 64k, if your DLL takes 68k then WM5 will allocate 2x64Kb — 128Kb.

WM5 每次为 dll 分配内存时,它至少使用 64Kb 块,所以如果你的 DLL 是 32K,它将需要 64k,如果你的 DLL 需要 68k,那么 WM5 将分配 2x64Kb — 128Kb。

When WM5 loads the DLL needed, it will always load at the bottom address of the previsouly loaded app, i.e. if app 1 has loaded 2×30kb DLLs, the first one will be loaded at address 0 to 64k, the second from 64 to 128, then your app will load its DLLs from 128kb, not 0, even if your apps runs into a separate slot.

当WM5加载需要的DLL时,它总是在预先加载的应用程序的底部地址加载,即如果应用程序1加载了2×30kb的DLL,第一个将在地址0到64k处加载,第二个从64到128 ,那么您的应用程序将从 128kb 加载其 DLL,而不是 0,即使您的应用程序运行到单独的插槽中。

In order to make things work, you will have to load your app earlier or remove non-needed apps from the windows starup folder.

为了使事情正常工作,您必须提前加载您的应用程序或从 Windows 启动文件夹中删除不需要的应用程序。

回答by Naruto

The DLL what you are using doesn't have definition for the method what you are calling. so the exception occurs.. it compiles fine.. only in run time problem occur.. solution is you need to make sure the definition is present in the DLL or not,else you need to go for some other dll.

您正在使用的 DLL 没有定义您正在调用的方法。所以发生异常..它编译得很好..只有在运行时出现问题..解决方案是您需要确保定义是否存在于DLL中,否则您需要寻找其他一些DLL。