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

转载于:https://www.cnblogs.com/lihuixian001/archive/2013/03/02/2939712.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果C++ DLL参数指针类型,那么在C#调用DLL时,需要使用平台调用(P/Invoke)技术,并且需要特别注意C++C#之间的内存管理问题。具体步骤如下: 1. 在C++中,使用`__declspec(dllexport)`关键字导出函数,例如: ``` __declspec(dllexport) void MyFunction(int* intPtr); ``` 2. 在C#中,使用`DllImport`特性声明导出函数及其参数,例如: ``` using System.Runtime.InteropServices; [DllImport("MyCppDll.dll")] public static extern void MyFunction(IntPtr intPtr); ``` 3. 在C#中,使用`Marshal.AllocHGlobal`方法分配一段内存,并使用`Marshal.WriteInt32`方法C#中的整数值写入该内存中,例如: ``` int myInt = 123; IntPtr intPtr = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(intPtr, myInt); ``` 4. 在C#中,调用C++ DLL中的函数,并传入指针参数,例如: ``` MyFunction(intPtr); ``` 5. 在C++中,使用指针访问该内存,并读取或修改其中的值,例如: ``` void MyFunction(int* intPtr) { int myInt = *intPtr; *intPtr = myInt * 2; } ``` 6. 在C#中,使用`Marshal.ReadInt32`方法读取C++函数中修改后的整数值,例如: ``` int resultInt = Marshal.ReadInt32(intPtr); ``` 7. 在C#中,释放用于存储整数值的内存,例如: ``` Marshal.FreeHGlobal(intPtr); ``` 需要注意的是,由于C++C#使用不同的内存管理方式,需要特别小心对象的创建和销毁,以避免内存泄漏和崩溃等问题。另外,如果C++函数中的参数指向对象的指针,需要特别注意指针指向的内存是否已经正确释放,否则也可能会导致内存泄漏和崩溃等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值