How do I solve the .NET CF exception "Can't find PInvoke DLL"?

11,838

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.

Share:
11,838
Ignas Limanauskas
Author by

Ignas Limanauskas

Updated on June 04, 2022

Comments

  • Ignas Limanauskas
    Ignas Limanauskas almost 2 years

    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.

    I have a DLL named MyDll.dll. In the MyDll.h I have:

    extern "C" __declspec(dllexport) int MyDllFunction(int one, int two);
    

    The definition of MyDllFunction in MyDll.cpp is:

    int MyDllFunction(int one, int two)
    {
        return one + two;
    }
    

    The C# class contains the following declaration:

    [DllImport("MyDll.dll")]
    extern public static int MyDllFunction(int one, int two);
    

    In the same class I am calling MyDllFunction the following way:

    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:

        [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.

  • Ignas Limanauskas
    Ignas Limanauskas over 15 years
    It was typo, the true name is MyDll.dll
  • Ignas Limanauskas
    Ignas Limanauskas over 15 years
    Spot on, www.trausch.us! Kudos! The platform for the Solution was "Any CPU", and I was deploying to WM5. However, for some reason VS2005 decided to set the platform for DLL as WM6. I noticed this, actually, just after I was done with the question. Did not want to lose all the typing, thus posted the question anyway.
  • ctacke
    ctacke over 15 years
    Windows Mobile runs on ARM architecture only and has since Pocket PC 2003.
  • Rambo223
    Rambo223 over 15 years
    Ahh, alright. Was unaware of that. Thanks for the correction.
  • Charles Keepax
    Charles Keepax about 12 years
    For those finding there way here, see the answer given by Ignas himself as to why he accepted this answer.