views:

1318

answers:

7

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.

+1  A: 

Have you added the dll as a reference in your project? If you do so, then you won't need to copy the file manually. If the problem persists, then it is probably due to the dll file not built correctly (different platform perhaps).

kgiannakakis
Both projects are in the same solution and they are deployed together, no need to upload manually.
Ignas Limanauskas
In that case add the one's project output as reference to the other. Even if it doesn't solve your problem, this is a good way of handling dependencies.
kgiannakakis
A: 

Make sure you have enough free virtual memory, since the P/Invoke import can fail on that.

arul
A: 

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?

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.

JaredPar
It was typo, the true name is MyDll.dll
Ignas Limanauskas
+1  A: 

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.

Michael Trausch
Windows Mobile runs on ARM architecture only and has since Pocket PC 2003.
ctacke
Ahh, alright. Was unaware of that. Thanks for the correction.
Michael Trausch
A: 

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.

Ignas Limanauskas
This should be a comment to the original response, not a separate response.
ctacke
A: 

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)

I'd love to force Gates or indeed any of the 'windows mobile' team to use the crap they create.

A: 

can any one tell me the proper procedure (proper steps even small ones) to call a evc++ dll.and plz tell me the proper steps to create a dll in evc++ (from scratch), i m new to compact c#. plz help me. i found a "cant find pinvoke dll" error. i try to copy the dll in windows, and also in projects location, place the dependency dll's in projects folder,change the property to copy if newer, i try a number of things so plz help me, i m in big trouble so plz help me.

bikram
Open a new question. You are asking a question. Read the faq (http://stackoverflow.com/faq) on how things are done on stackoverflow.
Shaihi