I'm programming game server and it uses unicode since I'm in Korea. The problem is that I can't copy wchar string from packet.
I checked wcsncpy_s(chat, 100, inChat.c_str(), inChat.length());
this works fine but something like this doesn't work.
wchar_t strId[10]; // I'm trying to copy L"player11" here.
wcsncpy_s(
strId,
10,
(WCHAR*)(buffer[2]), // buffer[0] : packet size, buffer[1] : packet type
9
);
I checked so many times to figure out what I've done wrong but I have no idea what is wrong about it.
It throws the same exception every time.
Exception thrown at 0x00007FFF20DFE5A0 (ucrtbased.dll) in SimplestMMORPG-Server.exe: 0xC0000005: Access violation reading location 0x0000000000000070.
Please help me...
You need just to specify offset from the buffer
's beginning, instead of trying to interpret buffer[2]
as memory address:
wcsncpy_s(
strId,
10,
(WCHAR*)(buffer + 2),
9
);
Because: given that buffer
is declared as char* buffer
(or std::byte* buffer
) that is buffer contains a pointer to some memory (shown on screenshot), buffer[2]
takes 3-rd element from the buffer. buffer[2]
has type char
, and type char
is incompatible with 3rd parameter of wcsncpy_s
. (From the memory view 3-rd byte is 0x70. Then 0x70 was passed as third parameter to wcsncpy_s
, which expects 3rd parameter to be a memory address, and it tries to read from 0x70
memory address, and fails, that's why error message was Access violation reading location 0x0000000000000070
)
Instead you need to calculate an address of that string inside buffer, as buffer + 2
, buffer + 2
has type char*
. wcsncpy_s
expects exactly char*
as a third parameter.