简体   繁体   中英

C++ unmanaged function callback char to C# char

I have a C# form. I am calling unmanaged functions from a C++ dll.

I have a callback called FUNDownDevCBEx this returns variables int nType, IntPtr pData .

So pointing to the struct _tagGPSMDVRInfo p = (_tagGPSMDVRInfo)Marshal.PtrToStructure(pData, typeof(_tagGPSMDVRInfo)); I can get the pointer variables.

However when I point to szIDNO I only get the last character of the string and I don't know why..

I expected szIDNO to show names incrementally but instead i only get the last char.

expected:

00091
00001
01211
01222
01504

what I got:

4
2
1
1
1

表格图片 4

void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1) is the callback loop returning szIDNO

Any suggestions will be appreciated.

C++ typedef:

 typedef struct _tagGPSInfo
 {
     int nID;
     char szIDNO[32];               
     char szName[32];               
     char szSIMCard[16];                
     union
     {
         GPSInfo_S gDVRInfo;
         GPSMobileInfo_S gMobileInfo;
         GPSDVSInfo_S DVSInfo;
     };
 }GPSInfo_S, *LPGPSInfo_S;

C++ Callback looks like this:

void (CALLBACK * FUNDownDevCBEx)(int nType, void* pData, void * pUsr)

My C# conversion code:

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct _tagGPSInfo
    {
        [MarshalAs(UnmanagedType.I4)]
        public int nID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szIDNO;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string szSIMCard;
    }



 private void devlist_Click(object sender, EventArgs e)
        {
            try
            {
                IntPtr _lHandle = IntPtr.Zero;
                NETClass.NETCLIENT_DEVOpenDevDownEx(ref _lHandle);
                NETClass.NETCLIENT_DEVSetCharEx( _lHandle);
                FUNDownDevCBEx _1callback = new FUNDownDevCBEx(FUNDownDevCBEx);
                NETClass.NETCLIENT_DEVRegDevDownCBEx( _lHandle, this, _1callback);
                NETClass.NETCLIENT_DEVStartDevDownEx(_lHandle, 0,0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "List Exception");
                return;
            }
        }



      static void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1)
    {
        try
        {
            _tagGPSInfo p = (_tagGPSMDVRInfo)Marshal.PtrToStructure(pData, typeof(_tagGPSInfo));
            int nID = p.nID;
            string szIDNO = p.szIDNO;
            switch (nType)
            {
                case 0:
                    form1.Invoke((MethodInvoker)(() => form1.memoBox.AppendText(" DATA =" + pData + " nID=" + nID + " szIDNO=" + szIDNO + Environment.NewLine)));
                    break;
                case 1:
                    //MessageBox.Show("GPS_DEV_DOWN_GROUP" + Environment.NewLine + " DATA =" + pData + " nID=" + nID);
                    break;
                case 2:
                    //MessageBox.Show("GPS_DEV_DOWN_FAILED" + Environment.NewLine + " DATA =" + pData );
                    break;
                case 3:
                    //MessageBox.Show("GPS_DEV_DOWN_SUC" + Environment.NewLine + " DATA =" + pData);
                    break;
                case 4:
                    //MessageBox.Show("GPS_DEV_DOWN_RELATION" + Environment.NewLine + " DATA =" + pData + " nID=" + nID);
                    break;
                default:
                    //MessageBox.Show("DEFAULT");
                    break;
            }
            //MessageBox.Show("nType= " + nType + " pData= " + pData);
            NETClass.NETCLIENT_DEVStopDevDownEx(IntPtr.Zero);
            NETClass.NETCLIENT_DEVCloseDevDownEx(IntPtr.Zero);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "FUNDownDevCBEx Exception");
            return;
        }
    }

my C# NETClass:

namespace ConversionTest
{

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void FUNDownDevCBEx(int nType, IntPtr data, Form1 form1);


    class NETClass
    {       
        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall,SetLastError = true)]
        public static extern int NETCLIENT_DEVOpenDevDownEx(ref IntPtr lpHandle);

        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int NETCLIENT_DEVRegDevDownCBEx( IntPtr lHandle, Form1 form1, FUNDownDevCBEx _callback);

        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int NETCLIENT_DEVStartDevDownEx( IntPtr lHandle, int nMgrType, int nDevType);

        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int NETCLIENT_DEVStopDevDownEx( IntPtr lHandle);

        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int NETCLIENT_DEVCloseDevDownEx( IntPtr lHandle);

        [DllImport("libnetclient.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int NETCLIENT_DEVSetCharEx( IntPtr lHandle, bool bUtf8 = true);
    }
}

Thanks for the comments. The solution was a good nights rest and a cup of coffee in the morning..

Since [StructLayout(LayoutKind.Sequential)] is Sequential each variable in the struct has to be sequential. Since my original struct was extremely long I missed one int variable before the szIDNO and this caused the pointer to ""miss"" 4 characters from my string...

This is how I understand it feel free to comment...

[StructLayout(LayoutKind.Sequential)]
public struct _tagGPSMDVRInfo
{
    //struct now in proper sequence 
    [MarshalAs(UnmanagedType.I4)]
    public int nID;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szIDNO;// <--- Now in correct position.... 
    [MarshalAs(UnmanagedType.I4)]
    public int nJingDu;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
    public string strReserve;
}



static void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1)
{
    try
    {
        switch (nType)
        {
            case 0:
                _tagGPSMDVRInfo p = new _tagGPSMDVRInfo(); // Create the managed struct
                p = Marshal.PtrToStructure<_tagGPSMDVRInfo>(pData);//simplify
                int nID = p.nID;
                string szIDNO = p.szIDNO;
                form1.Invoke((MethodInvoker)(() => form1.textBox4.AppendText(" DATA =" + pData + " nID=" + nID + " szIDNO=" + szIDNO + Environment.NewLine)));
                ;
                break;
            case 1:
                //MessageBox.Show("GPS_DEV_DOWN_GROUP" + Environment.NewLine + " DATA =" + pData + " nID=" + nID);
                break;
            case 2:
                //MessageBox.Show("GPS_DEV_DOWN_FAILED" + Environment.NewLine + " DATA =" + pData );
                break;
            case 3:
                //MessageBox.Show("GPS_DEV_DOWN_SUC" + Environment.NewLine + " DATA =" + pData);
                break;
            case 4:
                //MessageBox.Show("GPS_DEV_DOWN_RELATION" + Environment.NewLine + " DATA =" + pData + " nID=" + nID);
                break;
            default:
                //MessageBox.Show("DEFAULT");
                break;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "FUNDownDevCBEx Exception");
        return;
    }
}

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