简体   繁体   中英

unbalanced stack because of pointer

I'm not sure how to resolve this problem and I have been hitting my head against a brick wall for the best part of two hours. I have a function in an unmanaged DLL with the following documentation:

BOOL ZLNET_QueryDeviceTime(LONG lLoginID, LPZLNET_TIME pDeviceTime, int waittime=2000);

I assume that the pDeviceTime is a pointer and therefore I pass out in my C# code in order to use as a reference. In my C# project I have the following declaration:

[StructLayout(LayoutKind.Sequential)]
    public struct ZLNET_TIME
    {
        public Int32 dwYear;
        public Int32 dwMonth;
        public Int32 dwDay;
        public Int32 dwHour;
        public Int32 dwMinute;
        public Int32 dwSecond;
    } ;

[DllImport("zlnetsdk.dll")]
unsafe public static extern bool ZLNET_QueryDeviceTime(long lLoginID, out ZLNET_TIME pDeviceTime, int waittime);

The I call my function:

ZLNET_TIME t = new ZLNET_TIME();
ZLNET_QueryDeviceTime(loginResult, out t, 2000);

However, when I run my project I get the error:

Managed Debugging Assistant PInvokeStackImbalance has detected a problem in (MY APP EXE).

Additional Information: A call to PInvoke function VP::ZLNET_QueryDeviceTime has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

I then get nothing back in my ZLNET_TIME variable. Can anyone help me with this?

The first parameter should be int rather than long in the C# code. On the C++ side long is a 32 bit type. But in C# it is 64 bits.

The other potential issue is calling convention. Are you sure the C++ code is stdcall? It looks like it is cdecl unless you omitted the code that specifies the calling convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM