Baruch
Baruch

Reputation: 21548

p/invoke unbalanced stack error

I have the following C++ function and C# p/invoke decleration:

//C#
[DllImport("capture.dll", EntryPoint = "setup")]
public static extern void captureSetup(int rr);

//C++
extern "C" {
    __declspec(dllexport) void setup(int rr)

But I am getting an error about a p/invoke unbalanced stack likely caused by the managed signature not matching the unmanaged signature.

Can anyone see what is wrong with this?

Upvotes: 0

Views: 329

Answers (1)

David Heffernan
David Heffernan

Reputation: 613531

It's a calling convention mismatch. The C++ code uses cdecl by default but the C# assumes stdcall. You need to make them match, e.g.

[DllImport("capture.dll", EntryPoint = "setup", 
    CallingConvention = CallingConvention.Cdecl)]
public static extern void captureSetup(int rr);

Upvotes: 6

Related Questions