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

问题描述 投票:-1回答:1

我有C#表格。我正在从C ++ dll调用非托管函数。

我有一个叫FUNDownDevCBEx的回调,它返回变量int nType, IntPtr pData

指向结构_tagGPSMDVRInfo p = (_tagGPSMDVRInfo)Marshal.PtrToStructure(pData, typeof(_tagGPSMDVRInfo));,我可以获得指针变量。

但是,当我指向szIDNO时,我只得到字符串的最后一个字符,而我不知道为什么。

我希望szIDNO逐渐显示名称,但我只会得到最后一个字符。

预期:

00091
00001
01211
01222
01504

我得到了:

4
2
1
1
1

FORM IMAGE 4

[void FUNDownDevCBEx(int nType, IntPtr pData, Form1 form1)是返回szIDNO]的回调循环>

任何建议将不胜感激。

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

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

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

我的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);
    }
}

我有C#表格。我正在从C ++ dll调用非托管函数。我有一个名为FUNDownDevCBEx的回调,它返回变量int nType,IntPtr pData。因此,指向结构_tagGPSMDVRInfo p ...

c# c++ callback pinvoke intptr
1个回答
-1
投票

感谢您的评论。解决的办法是晚上睡个好觉,早上喝咖啡。.

© www.soinside.com 2019 - 2024. All rights reserved.