结构体内定义数组(50分)

  • 主题发起人 主题发起人 lingguang8888
  • 开始时间 开始时间
L

lingguang8888

Unregistered / Unconfirmed
GUEST, unregistred user!
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct TPosConfig
{
[MarshalAs(UnmanagedType.I4)]
public int iPosNo;//节点号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sServerIP;//服务器IP
[MarshalAs(UnmanagedType.I4)]
public int iPort;//服务器端口
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string sEncryptKey;//密钥
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string sEncryptMsg;//密函
};
目的是想做个现有服务器程序的客户端.
上面这个结构定义的字符串是以空结束的,定义8个,只能存7个.
请帮忙解决下,
换了个定义方法:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi,Size=39)]
public struct TPosConfig
{
[MarshalAs(UnmanagedType.I4)]
public int iPosNo;//节点号
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
private byte[] bServerIP;//服务器IP
[MarshalAs(UnmanagedType.I4)]
public int iPort;//服务器端口
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
private byte[] bEncryptKey;//密钥
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
private byte[] bEncryptMsg;//密函
public string sServerIP
{
get
{
return Encoding.Default.GetString(bServerIP);
}
set
{
bServerIP = Encoding.Default.GetBytes(value.Substring(0,15));
}
}
public string sEncryptKey
{
get
{
return Encoding.Default.GetString(bEncryptKey);
}
set
{
bEncryptKey = Encoding.Default.GetBytes(value.Substring(0,8));
}
}
public string sEncryptMsg
{
get
{
return Encoding.Default.GetString(bEncryptMsg);
}
set
{
bEncryptMsg = Encoding.Default.GetBytes(value.Substring(0,8));
}
}
};
调用出现异常:
TPosConfig tConfig = new TPosConfig();
int iLength = Marshal.SizeOf(tConfig);
//sEncryptKey = “tConfig.sEncryptKey”引发了“System.ArgumentNullException”类型的异常
 

Similar threads

后退
顶部