sizeof(lpszString)
is the size of the pointer, but you need the size in bytes of the buffer. You need to use:
sizeof(TCHAR)*(_tcsclen(lpszString)+1)
The code that reads the string should take care not to read off the end of the buffer by reading the value of cbData
that is supplied to it.
Remember that sizeof
evaluates at compile time. Keep that thought to the front of your mind when you use it and if ever you find yourself using sizeof
with something that you know to be dynamic, take a step back.
As an extra, free, piece of advice I suggest that you stop using TCHAR
and pick one character set. I would recommend Unicode. So, use wchar_t
in place of TCHAR
. You are already building a Unicode app.