[C++] Memory Problem (Structure & Packets)

Joined
Jul 11, 2006
Messages
188
Reaction score
185
Well I was playing with c++ and packets of MuOnline
But i have some troubles when trying to copy the memory of the structure to the buffer

example:

this is the packet data:

Code:
struct Packet01
{
  unsigned char op1;
  unsigned short op2;
};

on the packet builder i have this code:

Code:
Packet01 PacketInfo;
PacketInfo.op1 = 128;
PacketInfo.op2 = htons(7400);

unsigned char buff[255];

buff[0] = 0xC1;
buff[1] = 0x05; 
memcpy(&buff[2], &PacketInfo, sizeof(Packet01));

at this point, the buffer will store C1 05 80 FF 1C E8 ... and its wrong

it must be C1 05 80 1C E8

unsigned chars have length of 1 byte on memory
unsigned shorts have length of 2 bytes

so that structure must be size 3

and i dont know why that structure have size of 4

after some time i found some protocol files of mu, writed by someone named Evolver (i think)

and the code of on file is:

Code:
struct _OP30
{
  unsigned char op;
  _PDMC(npc_id, 2, unsigned short);
};

i think this PDMC fix my problem, but i dont have de #define of it

this PDMC, is a Packet Dynamic Cast (i see this in the readme file)

and i dont know de definition of this _PDMC, and how it works, i googled everything, dynamic casting, c++ casting, c++ memory structure, everything and i still dont know how to make the size correct.


sorry about my english, i dont like translators, i do my best hah :eek6:
 
that is because you don't use the #pragma pack if you are using the VC++, in msdn there are some tuts on how to handle this

for example look this code

struct PACKET1
{
BYTE a;
SHORT b;
INT c;
};

if i use sizeof(PACKET1) it will result in 8, but if i use the #pragma pack(1), the sizeof(PACKET1) wil l result on 7 , but don't forget after that put the default pack #pragma pack() , you can also use the __declspec(align(1)) in the declaration of the structure , i prefer the last one
 
Back