[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) #endifextern "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 !!
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();
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 ?
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?