A call to PInvoke function has unbalanced the stack when including a C DLL into C#

ε祈祈猫儿з 提交于 2019-11-30 23:42:35

The calling convention is wrong. If removing the int arguments doesn't trip the MDA then it is Cdecl:

 [DllImport("LibNonthreaded.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern void process(int high, int low);

This isn't the standard calling convention for exported DLL functions, you might consider changing it in the C/C++ code, if you can.

Daniel Earwicker

In C++ a long is 32 bit. In C# it's 64 bit. Use int in your C# declaration.

You could also try adding __stdcall to the C++ function. See: What is __stdcall?

in C# long means 64 bit int while in C++ long means 32 bit int, you need to change your pinvoke declaration to

 [DllImport("LibNonthreaded.dll", EntryPoint = "process")]
     public unsafe static extern void process( int high, int low);

You could also try changing your C++ declaration to stdcall, that's the calling convention used by most exported functions in the Windows environment.

 __stdcall  __declspec(dllexport) void process( long high, long low );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!