结构指针和固定字符
struct pointer and fixed char
我的 C++ 中有这样的结构
struct ST_HDTextInfo
{
char* font_name;
char* text;
unsigned int font_color;
unsigned int background_color;
int style;
int font_size;
struct ST_HDTextInfo* next;
};
我在我的 C# 代码中像这样重新声明它
[StructLayout(LayoutKind.Sequential,Pack=4)]
unsafe struct ST_HDTextInfo
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 25)]
public fixed char font_name[25];
[MarshalAs(UnmanagedType.ByValTStr, ArraySubType = UnmanagedType.I1, SizeConst = 20)]
public char[] text;
[MarshalAs(UnmanagedType.SysUInt)]
public uint font_color;
[MarshalAs(UnmanagedType.SysUInt)]
public uint background_color;
[MarshalAs(UnmanagedType.SysInt)]
public EN_FontStyle style;
[MarshalAs(UnmanagedType.SysInt)]
public int font_size;
[MarshalAs(UnmanagedType.SysInt)]
public ST_HDTextInfo* next;
};
字段 next 是指向同一结构的指针。当我使用 char[] var 作为字符串变量时,因为结构没有固定大小 VS 在 public ST_HDTextInfo* [=21 上出错=]下一个
但是当使用 fixed char var[SIZE_OF_VAR] 时问题解决了但是又出现了另一个错误。填充 char* 变量是一个新问题!我使用此代码将计数字符串转换为 char*
IntPtr p = Marshal.StringToHGlobalAuto("Poor Richard");
char* newCharStr = (char*)(p.ToPointer());
text_info.font_name = newCharStr;
本站我的字符串转成char*但是VS报错"The left-hand side of an assignment must be a variable, property or indexer "!!!
我解决了这个问题。我使用这个示例代码
[StructLayoutAttribute(LayoutKind.Explicit, CharSet = CharSet.Auto, Size=80)]
internal unsafe struct ST_HDTextInfo
{
[FieldOffset(0)]
public fixed char text[25];
[FieldOffset(0)]
public fixed char font_name[20];
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public uint font_color;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public uint background_color;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public EN_FontStyle style;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public int font_size;
[FieldOffset(80 - sizeof(long))]
public ST_HDTextInfo* next;
};
我的 C++ 中有这样的结构
struct ST_HDTextInfo
{
char* font_name;
char* text;
unsigned int font_color;
unsigned int background_color;
int style;
int font_size;
struct ST_HDTextInfo* next;
};
我在我的 C# 代码中像这样重新声明它
[StructLayout(LayoutKind.Sequential,Pack=4)]
unsafe struct ST_HDTextInfo
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 25)]
public fixed char font_name[25];
[MarshalAs(UnmanagedType.ByValTStr, ArraySubType = UnmanagedType.I1, SizeConst = 20)]
public char[] text;
[MarshalAs(UnmanagedType.SysUInt)]
public uint font_color;
[MarshalAs(UnmanagedType.SysUInt)]
public uint background_color;
[MarshalAs(UnmanagedType.SysInt)]
public EN_FontStyle style;
[MarshalAs(UnmanagedType.SysInt)]
public int font_size;
[MarshalAs(UnmanagedType.SysInt)]
public ST_HDTextInfo* next;
};
字段 next 是指向同一结构的指针。当我使用 char[] var 作为字符串变量时,因为结构没有固定大小 VS 在 public ST_HDTextInfo* [=21 上出错=]下一个 但是当使用 fixed char var[SIZE_OF_VAR] 时问题解决了但是又出现了另一个错误。填充 char* 变量是一个新问题!我使用此代码将计数字符串转换为 char*
IntPtr p = Marshal.StringToHGlobalAuto("Poor Richard");
char* newCharStr = (char*)(p.ToPointer());
text_info.font_name = newCharStr;
本站我的字符串转成char*但是VS报错"The left-hand side of an assignment must be a variable, property or indexer "!!!
我解决了这个问题。我使用这个示例代码
[StructLayoutAttribute(LayoutKind.Explicit, CharSet = CharSet.Auto, Size=80)]
internal unsafe struct ST_HDTextInfo
{
[FieldOffset(0)]
public fixed char text[25];
[FieldOffset(0)]
public fixed char font_name[20];
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public uint font_color;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public uint background_color;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public EN_FontStyle style;
[MarshalAs(UnmanagedType.I4)]
[FieldOffset(0)]
public int font_size;
[FieldOffset(80 - sizeof(long))]
public ST_HDTextInfo* next;
};