C# 调用C++DLL传递指向指针的指针参数的方法
2013-03-02 10:45
891 查看
C++结构体定义:
C#结构体的定义:
C#导入函数的声明
C#的调用方法:
C#导入函数的声明:
C#的调用方法:
可以参考:
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
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
相关文章推荐
- Unity使用C#调用C++dll传递指针参数、接收指针返回值
- c#调用C++写的DLL,传递二维指针参数
- C#调用c++的dll,结构体数组作为引用参数的传递方式
- 用C#调用C++DLL时的字符串指针参数传递问题
- C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)
- C#调用C++dll 结构体参数传递问题
- C#调用C++生成的Dll库,Dll使用callback向C#传递指针
- C#调用C/C++动态库的参数传递---数组指针的传递
- c# 调用 c++ dll 参数为结构体数组指针
- C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)
- C# 调用C/C++ Dll(参数含char*指针,返回char*指针)
- C#调用C++写的dll 函数传递参数问题,请有经验的人详细描述
- C# 调用Dll 传递字符串指针参数 .
- c#调用C/C++ DLL,传入指针数组(指针指向自定的结构体)
- C#调用C++dll方法,char*类型之间的传递
- C#中调用C++的dll的参数为指针类型的导出函数
- C# 调用C++DLL参数异常解决方法
- C#调用C/C++ DLL 参数传递和回调函数的总结
- C# 调用Dll 传递字符串指针参数
- C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)