C结构转变成C#的问题

铛铛 2011-01-24 06:36:46
这是C\C++中的结构定义
struct RETURNLIST
{

int iHour; //时

int iMinute; //分

int iSecond; //秒

char strList[30][30]; //返回的数据内容

char strHold1[30]; //保留

};



请问该结构在C#中如何定义??

public struct RETURNLIST
{

public int iHour; //时

public int iMinute; //分

public int iSecond; //秒

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char strList//???关键是该项??? SizeConst 写多少???这是个二维数组

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] strHold1; //保留

};



...全文
136 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzysun 2011-01-25
  • 打赏
  • 举报
回复
用char[]也可以
铛铛 2011-01-25
  • 打赏
  • 举报
回复
把char[]写成byte[]....
我还是用char[]可以吗?
铛铛 2011-01-25
  • 打赏
  • 举报
回复
谢谢楼上的各位大牛
doubleu2005 2011-01-24
  • 打赏
  • 举报
回复
试试这个:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct RETURNLIST {

/// int
public int iHour;

/// int
public int iMinute;

/// int
public int iSecond;

/// char[900]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=900)]
public string strList;

/// char[30]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=30)]
public string strHold1;
}
xingyuebuyu 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zanfeng 的回复:]
C# code

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Struct
{

public int iHour; //时

public int iMinute; //分

int iSecond; //秒

……
[/Quote]


public struct RETURNLIST
{

public int iHour; //时

public int iMinute; //分

public int iSecond; //秒

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 900)]
public char[] strList//抓住重点,保证这个元素占用的内存空间跟C中一样大就可以了,一维,二维都可以处理的

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] strHold1; //保留

};
足球中国 2011-01-24
  • 打赏
  • 举报
回复

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Struct
{

public int iHour; //时

public int iMinute; //分

int iSecond; //秒

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public strList[] strList; //返回的数据内容
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public byte[] strHold1; //保留
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct strList
{

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public byte[] strList; //返回的数据内容
}


试试。
Alexsunmoon 2011-01-24
  • 打赏
  • 举报
回复
呵呵~~~学习了。我也会遇到这方面的问题。语言间的转化,有时让人抓狂啊
wuyq11 2011-01-24
  • 打赏
  • 举报
回复
C++ C#
char* string
传出的char* StringBuilder
short short
char byte
char[n] fixed byte[n]
http://topic.csdn.net/u/20101021/21/a2e442a2-edd0-4d26-b79b-40c6c33fd810.html
GoAwayZ 2011-01-24
  • 打赏
  • 举报
回复
public char[,] strList

110,569

社区成员

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

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

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