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

前端 未结 3 2152
攒了一身酷
攒了一身酷 2021-01-06 12:56

I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackI

相关标签:
3条回答
  • 2021-01-06 13:14

    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 );
    
    0 讨论(0)
  • 2021-01-06 13:20

    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?

    0 讨论(0)
  • 2021-01-06 13:31

    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.

    0 讨论(0)
提交回复
热议问题