[C# & C++] P/invoke

I'm totally lost, this C++ code should work in a C# assembly but i get a
"Missing Methode" exception :(



#include "MyDll.h"

MYDLL_APIint fnMyDll()
{
return 99;
}

and the header file


#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

extern "C"
{
MYDLL_APIint fnMyDll();
}


then i wrapp all this stuff


[DllImport("MyDll.dll", EntryPoint = "fnMyDll", CharSet=CharSet.Unicode)]
privatestaticexternint fnMyDll();

privateint GetInt()
{
return fnMyDll();
}


But i get thi sexception i really don't undersatdn what's wrong !!

[1736 byte] By [lmussier] at [2008-3-5]
# 1
You need to specify ExactSpelling=true or remove CharSet=CharSet.Unicode.

When specifying a charset, P/Invoke appends an "A" or "W" to method names. Since you specified Unicode, P/Invoke is looking for a method named fnMyDllW.

Since your function doesn't have any string parameters, I would recommend removing the charset parameter. You can also omit EntryPoint as it matches your declared method name.

[DllImport("MyDll.dll")]
private static extern int fnMyDll();

JamesKovacs at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 2
Thanks it's work fine now.
But in have one more question, when a downlaod my dll in the \Windows folder all is ok i can get it and work with it.
But if i shut dowm my phone my dll is erased oO, if i want to run my app again i had to re-download the dll. So firts, why my dll is erased on shutdown and how t avoid this problem ?
lmussier at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 3
I'm wondering if it's some self-repair feature of your PPC device. I assume your data in other directories in intact. Can you place the DLL in another location?
JamesKovacs at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 4
Yes i can put it in an other folder, it's not a problem it's just a surprising thing as all dll should stay in the Windows folder.
Thanks for replies
lmussier at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 5
I inserted the following code:

[DllImport("ws2_32.dll"]
private extern static
Int32 WSAStartup(Int16 wVersionRequested, WSAData wsaData);

But I still get the following error message:

An unhandled exception of type 'System.MissingMethodException' occurred in NetworkLocationAwareness.exe

Additional information: Can't find PInvoke DLL 'ws2_32.dll'.

What seems to be the problem?

Jackoholic at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...