Cannot convert CString to BYTE array
Posted
by chekalin-v
on Stack Overflow
See other posts from Stack Overflow
or by chekalin-v
Published on 2010-03-17T07:47:59Z
Indexed on
2010/03/17
7:51 UTC
Read the original article
Hit count: 435
I need to convert CString to BYTE array. I don't know why, but everything that I found in internet does not work :( For example, I have
CString str = _T("string");
I've been trying so 1)
BYTE *pbBuffer = (BYTE*)(LPCTSTR)str;
2)
BYTE *pbBuffer = new BYTE[str.GetLength()+1];
memcpy(pbBuffer, (VOID*)(LPCTSTR)StrRegID, str.GetLength());
3)
BYTE *pbBuffer = (BYTE*)str.GetString();
And always pbBuffer contains just first letter of str
DWORD dwBufferLen = strlen((char *)pbBuffer)+1;
is 2
But if I use const string:
BYTE *pbBuffer = (BYTE*)"string";
pbBuffer contains whole string
Where is my mistake?
© Stack Overflow or respective owner