• Welcome to Valhalla Legends Archive.
 

Simple Question

Started by Spilled, September 21, 2005, 03:29 PM

Previous topic - Next topic

Spilled

In my SendPacket Coding Seen here:

void SendPacket(BYTE PacketID)
{
     char SendBuf[8026];
     int mWord = strlen(buffer);
     SendBuf[0] = 0xFF;
     SendBuf[1] = PacketID;
     *(unsigned short *)(SendBuf + 2) = (WORD) (sizeof(buffer) + 4); // insert packet size (ushort) starting at buffer[2]
     CopyMemory((char *) SendBuf + 2, buffer, mWord);
     mWord = strlen(SendBuf);
     send(wSock, SendBuf, mWord, 1);
}


I'm trying to add buffer (which is a char), to SendBuf starting at 4. How would i go about doing this because what im doing again working. Buffer contains 4 0x00 bytes.

Heres my insertbyte coding:


void SendPacket0x50()
{
     InsertBYTE(0x00);
     InsertBYTE(0x00);
     InsertBYTE(0x00);
     InsertBYTE(0x00);
     
     SendPacket(0x50);
}

void InsertBYTE(BYTE byte)
{
     buffer[bufferpos] = byte;
     bufferpos += 1;
}


As you can tell, im new to C++ so plz no flaming.
Thanks in advance!

rabbit

I use a variable named "buffer_len" that I update in all of my insertion routines...
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

UserLoser.

strlen() won't work because when it reaches a null character it'll stop counting.  You need to do what the bunny said and update a private variable everytime you insert something into the buffer to hold the length

Spilled

#3
Oh, should have seen that.... guess staring at the code for so long I didn't think about that. Thanks guys!!

Another problem i have ran into is another simple one and didn't think it was worth starting another topic so i'll just ask it here if thats ok.


*(unsigned short *)(SendBuf + 2) = (WORD) (buffer_len + 4);


Am i doing this right? For some reason its not looking right to me... Ideas on that?

Edit: After trial and error I have gotten it working and yes that was the right way Thanks to all that Helped!

Blaze

Couldn't you use sizeof() or is that for a size of an array or max length?
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

K

Quote from: Blaze on September 22, 2005, 05:18 PM
Couldn't you use sizeof() or is that for a size of an array or max length?

sizeof() will only return the size in bytes of an array that has been allocated on the stack.  For any dynamically allocated memory, sizeof() will return the size of the pointer to the memory.