I created a DLL file which includes two empty functions below.
extern "C" __declspec(dllexport) void __stdcall myFunc1() {
// just empty function
}
extern "C" __declspec(dllexport) void __cdecl myFunc2() {
// just empty function
}
In C#, I could call the functions using DLLImport
attribute like below.
[DllImport("myDLL", CallingConvention=CallingConvention.StdCall)]
private extern static void myFunc1();
[DllImport("myDLL", CallingConvention=CallingConvention.Cdecl)]
private extern static void myFunc2();
So I tried again directly with LoadLibrary()
of kernel32.dll instead of DllImport
attribute.
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void MyFunc1();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void MyFunc2();
However a runtime error occurs when I call MyFunc1() where MyFunc2() works.
So I replaced __stdcall
with __cdecl
in C++, recompiled the DLL and then called MyFunc1() again in C#.
And.. It worked.
Why on earth doesn't __stdcall calling convention work with pinvoke in C#?