在做项目移植的时候,经常会碰到数据类型的转换,而这一次碰到的是C/C++中的结构怎样转换到C#。 C/C++中的结构类型数据在C#下的转换
在做项目移植的时候,经常会碰到数据类型的转换,而我这一次碰到的是C/C++中的结构怎样转换到C#。例如我们在C/C++下的结构数据如下:
typedef struct
{
char sLibName[ 256 ];
char sPathToLibrary[ 256 ];
INT32 iEntries;
INT32 iUsed;
UINT16 iSort;
UINT16 iVersion;
BOOLEAN fContainsSubDirectories;
INT32 iReserved;
} LIBHEADER;
我们想把它转成C#下的结构类型如下:
public struct LIBHEADER
{
public char[] sLibName;
public char[] sPathToLibrary;
public Int32 iEntries;
public Int32 iUsed;
public UInt16 iSort;
public UInt16 iVersion;
public Boolean fContainsSubDirectories;
public Int32 iReserved;
}
看上去好像没问题了,呵呵呵,其实这样是不行的,我们得再给C#编译器一些信息,告诉它一些字符数组的大小。然后它们在C#下面长得样子就变成这样:
[StructLayout(LayoutKind.Sequential)]
public struct LIBHEADER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public char[] sLibName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public char[] sPathToLibrary;
public Int32 iEntries;
public Int32 iUsed;
public UInt16 iSort;
public UInt16 iVersion;
public Boolean fContainsSubDirectories;
public Int32 iReserved;
}
然后写一个函数负责转换。
public StructType ConverBytesToStructure(byte[] bytesBuffer)
{
// 检查长度。
if (bytesBuffer.Length != Marshal.SizeOf(typeof(StructType)))
{
throw new ArgumentException("bytesBuffer参数和structObject参数字节长度不一致。");
}
IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length);
for (int index = 0; index < bytesBuffer.Length; index++)
{
Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]);
}
StructType structObject = (StructType)Marshal.PtrToStructure(bufferHandler, typeof(StructType));
Marshal.FreeHGlobal(bufferHandler);
return structObject;
}
然后我们的函数用例是这样:
FileStream file = File.OpenRead(@"D:\Jagged Alliance 2 Gold\INSTALL.LOG");
byte[] buffer = new byte[Marshal.SizeOf(typeof(LIBHEADER))];
file.Read(buffer, 0, buffer.Length);
LIBHEADER testValue = CommonTools.ConverBytesToStructure(buffer);
string libName = new string(testValue.sLibName);
string pathToLibrary= new string(testValue.sPathToLibrary);
OK,搞定。
如果想去掉后面两句的char数组的转换哪代码如下
C#中的结构代码
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct LIBHEADER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string sLibName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string sPathToLibrary;
public Int32 iEntries;
public Int32 iUsed;
public UInt16 iSort;
public UInt16 iVersion;
public Boolean fContainsSubDirectories;
public Int32 iReserved;
}
其它代码不用作修改便可使用。
(1)定义结构体:
//命名空间
using System.Runtime.InteropServices;
//注意这个属性不能少
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]
struct TestStruct
{
public int c;
//字符串,SizeConst为字符串的最大长度
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string str;
//int数组,SizeConst表示数组的个数,在转换成
//byte数组前必须先初始化数组