简体   繁体   English

C++ 非托管函数回调 char 到 C# char

[英]C++ unmanaged function callback char to C# char

I have a C# form.我有一个 C# 表单。 I am calling unmanaged functions from a C++ dll.我正在从 C++ dll 调用非托管函数。

I have a callback called FUNDownDevCBEx this returns variables int nType, IntPtr pData .我有一个名为FUNDownDevCBEx的回调,它返回变量int nType, IntPtr pData

So pointing to the struct _tagGPSMDVRInfo p = (_tagGPSMDVRInfo)Marshal.PtrToStructure(pData, typeof(_tagGPSMDVRInfo));所以指向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..但是,当我指向szIDNO我只得到字符串的最后一个字符,我不知道为什么..

I expected szIDNO to show names incrementally but instead i only get the last char.我希望szIDNO以增量方式显示名称,但我只得到最后一个字符。

expected:预期的:

00091
00001
01211
01222
01504

what I got:我得到了什么:

4
2
1
1
1

表格图片 4 4

void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1) is the callback loop returning szIDNO void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1)是返回szIDNO的回调循环

Any suggestions will be appreciated.任何建议将不胜感激。

C++ typedef: C++ 类型定义:

 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: C++ 回调看起来像这样:

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

My C# conversion code:我的 C# 转换代码:

 [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:我的 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.由于[StructLayout(LayoutKind.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...由于我的原始结构非常长,我在szIDNO之前错过了一个int变量,这导致指针指向我的字符串中的""miss"" 4 个字符...

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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