Search code examples
c#wpfdll.net-4.0calling-convention

WPF: A call to PInvoke function has unbalanced the stack


I'm encountering this error while using one of method in my .dll reference. When I call MyRef.SetDbaseId method I'm returned to VS with this error. I've tried to add CallingConvention enum parameters, but all of them does not work for me. I've also opened dll in DependencyWalker to check entry point and param (ulong), which fits in my app. It's confusing because other methods works fine. Any ideas how to solve this problem?

[DllImport("my.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?setdbaseid@@YGHK@Z")]
public static extern int SetDbaseID(ulong dbase_id);
ulong tmid = ulong.Parse(p_6);
i = MyRef.SetDbaseID(tmid);

Solution

  • The mangled name, ?setdbaseid@@YGHK@Z, demangles to:

     int __stdcall setdbaseid(unsigned long);
    

    Which makes your declaration wrong, an unsigned long in native code is 32-bits. And the calling convention is wrong. Fix:

    [DllImport("my.dll", EntryPoint = "?setdbaseid@@YGHK@Z"))]
    public static extern int SetDbaseID(uint dbase_id);