Here's an example of how it -should- be done, though this adds another layer ontop of what I do seeing I never do the conversion to a byte[]Quote:
So what would be the correct way of doing it since it looks pretty much the same as your example.
//edit I think that I see what the problem is, uint uses 4 bytes making it 22 bytes total while it was stated before that 20 was the max causing the 2 leftover bytes to be dumped into invalid memory.
This means that ushort had to be used cause that uses 2 bytes which would end up at 20, correct?
Code:
/* extra function which I -don't- use, I use the pointer */
byte[] SafeArray(void* lpMemory, int Size)
{
byte[] Safe = new byte[Size];
#ifdef _MEMCPY_SUPPORTED_
fixed (byte* lpSafe = Safe)
memcpy(lpSafe, lpMemory, Size);
#else
byte* lpbMemory = (byte*)lpMemory;
for (int i = 0; i < Size; i++)
Safe[i] = lpbMemory[i];
#endif
return Safe;
}
// ....
struct SimplePacket
{
public ushort Size;
public ushort Type
public int Value;
}
// ....
SimplePacket simple = new SimplePacket();
simple.Size = (ushort)sizeof(SimplePacket);
simple.Type = 0x666;
simple.Value = 69;
return SafeArray(&simple, simple.Size);