CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Apr 2004
    Posts
    102

    How to resolve a PInvokeStackImbalance was detected issue

    I have started converting a C SDK to C# and have encountered a PInvokeStackImbalance was detected error that I just can't figure out how to resolve.

    The details of the message is as follows:

    PInvokeStackImbalance was detected
    Message: A call to PInvoke function 'PInvoke!PlatformInvokeTest::CLIENT_Logout' 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.
    Some info from the C SDK header file follows:

    Code:
    #ifndef LLONG
    #ifdef WIN32
    #define LLONG LONG
    #else //WIN64 
    #define LLONG INT64
    #endif
    #endif
    
    #ifndef CLIENT_API
    #define CLIENT_API  __declspec(dllexport) 
    #endif
    
    #else
    
    #ifndef CLIENT_API
    #define CLIENT_API  __declspec(dllimport)
    #endif
    
    #endif
    
    #define CALLBACK __stdcall
    #define CALL_METHOD  __stdcall  //__cdecl
    
    
    // Register to the device 
    CLIENT_API LLONG CALL_METHOD CLIENT_Login(char *pchDVRIP, WORD wDVRPort, char *pchUserName, char *pchPassword, LPNET_DEVICEINFO lpDeviceInfo, int *error = 0);
    
    // Log out the device 
    CLIENT_API BOOL CALL_METHOD CLIENT_Logout(LLONG lLoginID);
    and finally my source code...

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    class PlatformInvokeTest
    {
        static public long lLogin;
    
        public delegate void fDisConnect(long lLoginID, IntPtr pchDVRIP, long nDVRPort, uint dwUser);
        public delegate void fHaveReConnect(long lLoginID, IntPtr pchDVRIP, long nDVRPort, uint dwUser);
    
        //  [DllImport("dhnetsdk.dll", CallingConvention = CallingConvention.StdCall)]
        [DllImport("dhnetsdk.dll")]
        public static extern bool CLIENT_Init(fDisConnect cbDisConnect, uint dwUser);
    
      //  [DllImport("dhnetsdk.dll", CallingConvention = CallingConvention.StdCall)]
          [DllImport("dhnetsdk.dll")]
        public static extern void CLIENT_SetAutoReconnect(fHaveReConnect cbHaveReconnt, uint dwUser);
    
     //   [DllImport("dhnetsdk.dll", CallingConvention = CallingConvention.StdCall)]
          [DllImport("dhnetsdk.dll")]
        public static extern long CLIENT_Login(string pchDVRIP, ushort wDVRPort, string pchUserName, string pchPassword, NET_DEVICEINFO lpDeviceInfo, IntPtr error);
    
       // [DllImport("dhnetsdk.dll", CallingConvention = CallingConvention.StdCall)]
          [DllImport("dhnetsdk.dll")]
        public static extern bool CLIENT_Logout(long lID);
    
        // [DllImport("dhnetsdk.dll", CallingConvention = CallingConvention.StdCall)]
          [DllImport("dhnetsdk.dll")]
        public static extern void CLIENT_Cleanup();
    
        static int[] long2doubleInt(long a)
        {
            int a1 = (int)(a & uint.MaxValue);
            int a2 = (int)(a >> 32);
            return new int[] { a1, a2 };
        }
    
        public static void fDisConnectMethod(long lLoginID, IntPtr pchDVRIP, long nDVRPort, uint dwUser)
        {
            System.Console.WriteLine("Disconnect");
            return;
        }
    
        public static void fHaveReConnectMethod(long lLoginID, IntPtr pchDVRIP, long nDVRPort, uint dwUser)
        {
            System.Console.WriteLine("Reconnect success");
            return;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public class NET_DEVICEINFO
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
            public byte[] sSerialNumber;
            public byte byAlarmInPortNum;
            public byte byAlarmOutPortNum;
            public byte byDiskNum;
            public byte byDVRType;
            public byte byChanNum;
        }
    
        public static void Main()
        {
            fDisConnect fDisConnecthandler = fDisConnectMethod;
            fHaveReConnect fHaveReConnecthandler = fHaveReConnectMethod;
            NET_DEVICEINFO deviceinfo = new NET_DEVICEINFO();
            IntPtr iRet = new IntPtr(0);
            CLIENT_Init(fDisConnectMethod, 0);
            CLIENT_SetAutoReconnect(fHaveReConnecthandler, 0);
            lLogin = CLIENT_Login("192.168.1.198", 3112, "userid", "password", deviceinfo, iRet);
    
            int[] al = long2doubleInt(lLogin);
            if (al[0] <= 0)
                Console.WriteLine("Login device failed");
            else
            {
                Console.WriteLine("Login device successful");
                CLIENT_Logout(lLogin);
                CLIENT_Cleanup();
            }
        }
    }
    The error is thrown at the CLIENT_Logout method. Any assistance would be greatly appreciated.
    Last edited by BobS0327; September 19th, 2013 at 01:10 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured