12

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.

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 know that the native dll is found in the same folder as the executable. What more should I do?

I am using VS 2008.

ctacke
  • 66,480
  • 18
  • 94
  • 155
Dilip
  • 213
  • 2
  • 3
  • 9
  • 2 quick comments: 1) the first time I did not have any of the native DLLs in the exe's folder. so this exception at least was making sense then. Now that made sure everything is deployed, how can I run into the _same_ exception again? 2) I tried to setup logging as described in this post: http://blogs.msdn.com/netcfteam/archive/2005/07/24/442609.aspx I used to Remote Registry Editor to do it but to no avail. No logging files are created at all! How can so many basic things go wrong? – Dilip Sep 21 '09 at 18:45

4 Answers4

9

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

  1. You are missing dependencies of 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.

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

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Also, if you are expecting the native DLL to be in the same directory as the calling exe make sure that the exe is actually deployed in same directory. – Hawkez Aug 29 '16 at 18:32
5

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)

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Hi Jared Thanks for the response. 1) I am dead certain the native dll is found in the same directory as the executing application. In fact there are 2 native dlls. I commented calls being made into one and now the other is throwing the _same_ exception 2) Check -- I have the same codebase compiled for the Win32 platform too and it works just fine there The dll name is spelled correctly and it _does_ have the .dll suffix (isn't that what you meant?) – Dilip Sep 21 '09 at 19:07
  • Do you know also happen to know why I cannot set this up properly:http://blogs.msdn.com/netcfteam/archive/2005/07/24/442609.aspx I used the remote registry editor to put in all the reqd keys and the log file still doesn't get created. Today must be all-mudane-things-must-go-wrong day! – Dilip Sep 21 '09 at 19:09
  • FYI, it will work fine with or without the "dll" suffix, though you should be consistent in the exact string you pass in (I use a const): http://www.danielmoth.com/Blog/2007/12/be-consistent-with-dllimports.html – ctacke Sep 22 '09 at 05:13
  • All the advice thus far is what I've used to solve these in the past. I have two things to try though. 1. To enable the .NETCF logging try setting a DWORD value under the last key and the key value as well. I forget which combination worked for me but I recalled it confused me, still does - e.g [HKLM\Software\Microsoft\.NETCompactFramework\Diagnostics\Logging] "Enabled" = 1. 2. Try using a release compiled version of your native DLLs. We had a few DLLs that refused to P/Invoke their debug builds. Never resolved it, probably a build switch. – Damon8or Sep 14 '12 at 20:13
0

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 allocates 32 slots of 32Mb for applications to run into.

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.

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.

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

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
  • 1
    True, but not true. True for native DLLs, but *not true* for this question. This question is tagged "compact-framework" and managed DLLs are NOT loaded under this memory model. They are loaded as memory-mapped files and do not take up the 64k virtual memory block. MSDN has a really good webcast on how the Compact Framework uses memory that you might be interested in. – ctacke Dec 30 '09 at 16:46
-1

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.

Naruto
  • 9,476
  • 37
  • 118
  • 201