I am trying to learn P/Invoke, so I created a simple dll in C++
KingFucs.h:
namespace KingFuncs
{
class KingFuncs
{
public:
stati
You need to use extern "C"
when you export your function so that you suppress C++ name mangling. And you also should not try to p/invoke to members of a class. Use free functions instead:
extern "C" {
__declspec(dllexport) int GiveMeNumber(int i)
{
return i;
}
}
On the managed side your DllImport
attribute is all wrong. Don't use SetLastError
which is for Win32 APIs only. Don't bother setting CharSet
if there are not text parameters. No need for ExactSpelling
. And the calling convention is presumably Cdecl
.
[DllImport("KingFuncDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int GiveMeNumber(int i);