C++结构体转c#下数据类型

aifudi 2010-10-27 04:54:00
c++下有以下结构体类型

struct test
{
int port;
char * buffer[2];

int fszie[2];
char * pathname;


}

现在转为如下的C#类

public struct test
{
public int port ;

[MarshalAs(unmanagedType.ByValArray,sizeconst =2)]
public stringbuilder[]buffer;

[MarshalAs(unmanagedType.ByValArray,sizeconst =2)]
public int []fsize;
[MarshalAs(unmanagedType.Lpstr)]
public string pathname;

}

现在在c#下调用c++ dll提供的一个函数,参数就是struct test 类型,但是参数传递过程总是不对 。调试跟踪了一下
c++下的char * buffer[2] 不对,请教各位,在C#下如何封装这个数据类型
...全文
187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-10-27
  • 打赏
  • 举报
回复
C++ C#
传入的char* string
传出的char* StringBuilder(预分配空间)
short short
char byte
char[n] fixed byte[n]
结构指针 结构指针
函数指针 委托
wjq 2010-10-27
  • 打赏
  • 举报
回复
buffer用byte[]吧,而不是stringbuilder
xingyuebuyu 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flyerwing 的回复:]
public int port;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=2, ArraySubType=System.Runtime.InteropServices.UnmanagedType.S……
[/Quote]
z289830131 2010-10-27
  • 打赏
  • 举报
回复

帮lz 顶下
flyerwing 2010-10-27
  • 打赏
  • 举报
回复
public int port;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=2, ArraySubType=System.Runtime.InteropServices.UnmanagedType.SysUInt)]
public System.IntPtr[] buffer;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=2, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I4)]
public int[] fszie;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string pathname;
看这个行不.
卓才琣 2010-10-27
  • 打赏
  • 举报
回复
C#中 使用 fixed 来“钉”住一块内存,或者为了更好的扩展,可以写成


[StructLayout(LayoutKind.Sequential, Size = 2)]
public struct bufferStruct
{

}

struct test
{
public int port;

public bufferStruct buffer;
public int[] fsize;
public string pathname;
}
卓才琣 2010-10-27
  • 打赏
  • 举报
回复

struct test
{
public int port;
public fixed byte buffer[2];
public int[] fsize;
public string pathname;
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧