gpt4 book ai didi

.net - 为什么在尝试使用 Marshal.PtrToStructure 将字节数组转换为类实例时会出现访问冲突?

转载 作者:行者123 更新时间:2023-12-03 23:01:35 26 4
gpt4 key购买 nike

在 C++ 中,我有这个:

struct BasePacketProto
{
unsigned short PACKET_OPCODE;
unsigned short PACKET_MAGIC_NUMBER;
unsigned short PACKET_REMAIN_DATA_LENGTH;
};

struct CM_Request_AcceptAccount : BasePacketProto
{
unsigned char ACCOUNT_LOGIN[16];
unsigned char ACCOUNT_PASSWORD[16];
};

static void SendPacket()
{
CM_Request_AcceptAccount packet;
packet.PACKET_OPCODE = opCM_Request_AcceptAccount;
packet.PACKET_MAGIC_NUMBER = 123;
packet.PACKET_REMAIN_DATA_LENGTH = sizeof(CM_Request_AcceptAccount) -
sizeof(BasePacketProto);
memcpy(packet.ACCOUNT_LOGIN, "asd", sizeof("asd") * sizeof(char));
memcpy(packet.ACCOUNT_PASSWORD, "asd_pass", sizeof("asd_pass") * sizeof(char));

//Send the packet to the server.
int lLength = send(lhSocket, (const char*)&packet, sizeof(CM_Request_AcceptAccount), 0);
}

C# , 这个:
[StructLayout(LayoutKind.Sequential)]
class BasePacketProto
{
public System.UInt16 PACKET_OPCODE;
public System.UInt16 PACKET_MAGIC_NUMBER;
public System.UInt16 PACKET_REMAIN_DATA_LENGTH;
}

[StructLayout(LayoutKind.Sequential)]
class CM_Request_AcceptAccount : BasePacketProto
{
public byte[] ACCOUNT_LOGIN = new byte[16];
public byte[] ACCOUNT_PASSWORD = new byte[16];
}

拆分数据包的类:
public class PacketProcessor
{
static List<byte> raw_packet = new List<byte>();
static int PACKET_HEADER_SIZE = Marshal.SizeOf(typeof(BasePacketProto));

static public void ProcessPacketBytes(byte[] bytes, int size)
{
for (int i = 0; i < size; i++)
raw_packet.Add(bytes[i]); //Adding bytes to own storage

if (raw_packet.Count < PACKET_HEADER_SIZE) //If we don't have enough bytes
//to build base packet, we will
//return and wait for more.
return;

//This packet building works fine!
BasePacketProto bpp =
ConvertBytesTo<BasePacketProto>(raw_packet.GetRange(
0, PACKET_HEADER_SIZE).ToArray());

if (raw_packet.Count >= (PACKET_HEADER_SIZE +
bpp.PACKET_REMAIN_DATA_LENGTH)) //If we have enough
bytes in storage to restore child packet.
{
switch ((ClientPacketOpcodes)bpp.PACKET_OPCODE)
{
case ClientPacketOpcodes.opCM_Request_AcceptAccount:
//But this one fails
bpp = ConvertBytesTo<CM_Request_AcceptAccount>(raw_packet.GetRange(
0, PACKET_HEADER_SIZE + bpp.PACKET_REMAIN_DATA_LENGTH).ToArray());

PacketHandler.Handle_opCM_Request_AcceptAccount((CM_Request_AcceptAccount)bpp);
break;

default:
break;
}

raw_packet.RemoveRange(0, PACKET_HEADER_SIZE + bpp.PACKET_REMAIN_DATA_LENGTH);
}
}

static T ConvertBytesTo<T>(byte[] data)
{
unsafe
{
fixed(byte *ptr = data)
{
//I am getting an access violation here when trying to
//build child packet :(
return (T)Marshal.PtrToStructure(new IntPtr(ptr), typeof(T));
}
}
}
}

在接收线程中的某处:
while (clientStream.CanRead)
{
byte[] temp_buff = new byte[1024];
int received = 0;

while ((received = clientStream.Read(temp_buff, 0, 1024)) > 0) //Here we receive 38 bytes
{
//Passing it to the packet splitter.
PacketProcessor.ProcessPacketBytes(temp_buff, received);
}
}

客户端向服务器发送数据包时的结果:

System.AccessViolationException was not handled
Attempt to read or write to protected memory. Most likely it points that other memory is corrupted.



为什么无法将 38 个字节转换为 CM_Request_AcceptAccount?我该怎么做才能让它发挥作用?

最佳答案

当你声明一个结构体时

struct CM_Request_AcceptAccount : BasePacketProto
{
unsigned char ACCOUNT_LOGIN[16];
unsigned char ACCOUNT_PASSWORD[16];
};

在 C++ 中,数组是“内联”和“固定”长度的,换句话说,每个数组为结构的“大小”贡献 16 个字节。

但是在 C# 中,您重新声明了与以下相同的结构:
[StructLayout(LayoutKind.Sequential)]
class CM_Request_AcceptAccount : BasePacketProto
{
public byte[] ACCOUNT_LOGIN = new byte[16];
public byte[] ACCOUNT_PASSWORD = new byte[16];
}

在这里,您没有提供前 16 个字节属于 ACCOUNT_LOGIN 的信息。数组和接下来的 16 到 ACCOUNT_PASSWORD .

线
字节[] ACCOUNT_LOGIN = 新字节[16]

没有告诉编码员任何事情。它只会导致 CLRCM_Request_AcceptAccount 的实例时,在堆上分配一个 16 字节的数组是在代码中创建的。

为了正确编码结构,请将 C# 声明更改为:
[StructLayout(LayoutKind.Sequential)]
class CM_Request_AcceptAccount : BasePacketProto
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] ACCOUNT_LOGIN;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] ACCOUNT_PASSWORD;
}

附加信息:由于您的字符数组旨在保存 C 样式字符串,您还可以使用
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
public string ACCOUNT_LOGIN;

这里要记住的一点是,编码器将在您在 C++ 代码中设置的 16 个字符中期望一个空终止字符。

关于.net - 为什么在尝试使用 Marshal.PtrToStructure 将字节数组转换为类实例时会出现访问冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com