C# 調用C++DLL傳遞指向指針的指針參數的方法


 

 

C++結構體定義:

struct DeviceInfo
{    
    char szDeviceName[DEVICE_NAME_LEN];
    char szMACAddress[MAC_ADDRESS_LEN];        
    char szDeviceIP[DEVICE_IP_LEN];
};

 

C#結構體的定義:

    [StructLayout(LayoutKind.Sequential)]
   public struct DeviceInfo
    {
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
        public string szDeviceName;

         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
          public string szMACAddress;

         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
         public string szDeviceIP; 


    }

 

情況1:C++的dll負責分配內存

C++導出函數的聲明

#define DLL_API  extern "C"  __declspec(dllexport)
DLL_API int findAllDevices(DeviceInfo** ppDeviceInfoList,int * pCount);

C#導入函數的聲明

[DllImport("IPAlter_d.dll")]
        public extern static int findAllDevices(out IntPtr pDeviceInfo, ref int pCount);

C#的調用方法:

 IntPtr pBuff = new IntPtr();//直接new一個參數即可



            if (IPAlter.findAllDevices(out pBuff, ref cout) != 1)
            {
                System.Console.WriteLine("搜索失敗!");
                System.Console.ReadLine();
                return;
            }

          
            for (int i = 0; i < cout; i++)
            {

                IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(DeviceInfo)) * i);
              

              System.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szDeviceName);
                System.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szDeviceIP);
                System.Console.WriteLine(((DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo))).szMACAddress);
            }

 

情況2:C#負責分配內存

C++導出函數的聲明:

DLL_API int findAllDevice(DeviceInfo* ppDeviceInfoList,int * pCount);

 

 C#導入函數的聲明:

 [DllImport("IPAlter_d.dll")]
        public extern static int findAllDevice(IntPtr pDeviceInfo, ref int pCount);

 

 C#的調用方法:

            DeviceInfo[] DeviceInfoList = new DeviceInfo[50];
            int size = Marshal.SizeOf(typeof(DeviceInfo)) * 50;
            byte[] bytes = new byte[size];
            IntPtr pBuff = Marshal.AllocHGlobal(size);



            if (IPAlter.findAllDevice(pBuff, ref cout) != 1)
            {
                System.Console.WriteLine("搜索失敗!");
                System.Console.ReadLine();
                return;
            }


            for (int i = 0; i < cout; i++)
            {

                IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(DeviceInfo)) * i);
                DeviceInfoList[i] = (DeviceInfo)Marshal.PtrToStructure(pPonitor, typeof(DeviceInfo));

                System.Console.WriteLine(DeviceInfoList[i].szDeviceName);
                System.Console.WriteLine(DeviceInfoList[i].szDeviceIP);
                System.Console.WriteLine(DeviceInfoList[i].szMACAddress);
            }
            Marshal.FreeHGlobal(pBuff);

 

可以參考:

http://www.cnblogs.com/cxwx/archive/2010/12/29/1921140.html

http://hi.baidu.com/fanr520/item/e761f9ca0766d462f6c95d55

http://blog.csdn.net/wangweitingaabbcc/article/details/7663949


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM