views:

2838

answers:

4

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.

+2  A: 

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
Hi JaredThanks 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_ exception2) Check -- I have the same codebase compiled for the Win32 platform too and it works just fine thereThe dll name is spelled correctly and it _does_ have the .dll suffix (isn't that what you meant?)
Dilip
Do you know also happen to know why I cannot set this up properly:http://blogs.msdn.com/netcfteam/archive/2005/07/24/442609.aspxI 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
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
+2  A: 

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
A: 

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.

Shadow
method may not be exposed for export purpose
Shadow
A: 

Your problem lies on the fact that WM5 memory managment is crap, dll's are loaded from top of slot to bottom while apps are loaded from bottom to top, if you dont have enough space between your app and your dll, you will receive a cant 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 always load at the bottom address of the previsouly loaded app, ie, if app 1 has loaded 2 x 30kb dlls, the first one will be loaded at address 0 to 64k, the second from 64 to 128, then your app will load it's dlls from 128kb, not 0, even if your apps runs into a seperate slot.

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

good luck

Alain Holloway

Alain Holloway
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