当前位置: 移动技术网 > IT编程>开发语言>c# > 基于C#调用c++Dll结构体数组指针的问题详解

基于C#调用c++Dll结构体数组指针的问题详解

2019年07月18日  | 移动技术网IT编程  | 我要评论

c#调用c++dll文件是一件很麻烦的事情,首先面临的是数据类型转换的问题,相信经常做c#开发的都和我一样把学校的那点c++底子都忘光了吧(语言特性类)。

网上有一大堆得转换对应表,也有一大堆的转换实例,但是都没有强调一个更重要的问题,就是c#数据类型和c++数据类型占内存长度的对应关系。

如果dll文件中只包含一些基础类型,那这个问题可能可以被忽略,但是如果是组合类型(这个叫法也许不妥),如结构体、类类型等,在其中的成员变量的长度的申明正确与否将决定你对dll文件调用的成败。

如有以下代码,其实不是dll文件的源码,而是厂商给的c++例子代码

c++中的结构体申明

typedef struct 
{ 
 unsigned char port; 
 unsigned long id; 
 unsigned char ctrl; 
 unsigned char pdata[8]; 
}hscan_msg; 

c++中的函数申明(一个c++程序引用另一个c++的dll文件)

extern "c" int _stdcall hscan_sendcanmessage(unsigned char ndevice,unsigned char nport,hscan_msg *msg,int nlength);

c++中的调用:

.... 
hscan_msg msg[100]; 
..... 
hscan_sendcanmessage(m_ndevice,m_nport,msg,nframes); 

由上述代码可见,msg是个结构体的数组。

下面是我的c#的代码

c#结构体申明:(申明成)

[structlayout(layoutkind.sequential)] 
 public struct hscan_msg 
 { 
    // unmanagedtype.byvalarray, [marshalas(unmanagedtype.u1)]这个非常重要,就是申明对应类型和长度的 
 [marshalas(unmanagedtype.u1)] 
 public byte port; 
 [marshalas(unmanagedtype.u4)] 
 public uint nid; 
 [marshalas(unmanagedtype.u1)] 
 public byte nctrl; 
 [marshalas(unmanagedtype.byvalarray, sizeconst = 8)] 
 public byte[] pdata; 
 }; 

c#函数申明

[dllimport("hs2106api.dll")] 
 public static extern int hscan_sendcanmessage( 
 byte ndevice, byte nport, hscan_msg[] pmsg, int nlength); 

c#函数调用

hscan_msg[] msg = new hscan_msg[1]; //发送缓冲区大小可根据需要设置; 
 for (int yy = 0; yy < msg.length; yy++) 
 { 
 msg[yy] = new hscan_msg(); 
 } 
    //...结构体中的成员的实例化略 
    hscan_sendcanmessage(0x0, 0x0, msg, 1) 

那些只能用指针不能用结构体和类的地方

c++中的结构体申明

typedef struct 
{ 
 unsigned char port; 
 unsigned long id; 
 unsigned char ctrl; 
 unsigned char pdata[8]; 
}hscan_msg; 

c++中的函数申明(一个c++程序引用另一个c++的dll文件)

extern "c" int _stdcall hscan_sendcanmessage(unsigned char ndevice,unsigned char nport,hscan_msg *msg,int nlength); 

c#中的结构体申明:

[structlayout(layoutkind.sequential)] 
 public struct hscan_msg 
 { 
 [marshalas(unmanagedtype.u1)] 
 public byte port; 
 /// <summary> 
 /// 节点标识,neff=1 时(扩展帧),为29 位neff=0(标准帧)时,为11 位; 
 /// </summary> 
 [marshalas(unmanagedtype.u4)] 
 public uint nid; 
 [marshalas(unmanagedtype.u1)] 
 public byte nctrl; 
 [marshalas(unmanagedtype.byvalarray, sizeconst = 8)] 
 public byte[] pdata; 
 }; 

c#函数的调用:包含使用指针intptr替代结构体数组和读取intptr的方法

hscan_msg[] msg1 = new hscan_msg[10]; 
 for (int i = 0; i < msg1.length; i++) 
 { 
 msg1[i] = new hscan_msg(); 
 msg1[i].pdata = new byte[8]; 
 } 
 intptr[] ptarray = new intptr[1]; 
 ptarray[0] = marshal.allochglobal(marshal.sizeof(typeof(hscan_msg)) * 10); 
 intptr pt = marshal.allochglobal(marshal.sizeof(typeof(hscan_msg))); 
 marshal.copy(ptarray, 0, pt, 1); 
 
 int count = hscan_readcanmessage(0x0, 0,pt, 10); 
 
 textboxstatus.text += "/r/n" + "读取0口:" + count.tostring() + "帧数据"; 
 for (int j = 0; j < 10; j++) 
 { 
 msg1[j] = 
 (hscan_msg)marshal.ptrtostructure((intptr)((uint32)pt+ j * marshal.sizeof(typeof(hscan_msg))) 
 , typeof(hscan_msg)); 
 textboxstatus.text += "/r/n收到0口" + convert.tobyte(msg1[j].pdata[0]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[1]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[2]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[3]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[4]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[5]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[6]).tostring() 
 + "|" + convert.tobyte(msg1[j].pdata[7]).tostring(); 
 } 

以上这篇基于c#调用c++dll结构体数组指针的问题详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网