I'm writing this answer to notify you (so you see the red 1 at the top of any Stack Overflow page) because you had the same bug yesterday (in your message box) and I now realize I neglected to say this in my answer yesterday.
Keep in mind that the new[]
operator on a built-in type like WCHAR
or int
does NOT initialize the data at all. The memory you get will have whatever garbage was there before the call to new[]
, whatever that is. The same happens if you say WCHAR x[100];
as a local variable. You must be careful to initialize data before using it. Compilers are usually good at warning you about this. (I believe C++ objects have their constructors called for each element, so that won't give you an error... unless you forget to initialize something in the class, of course. It's been a while.)
In many cases you'll want everything to be zeroes. The '\0'
/L'\0'
character is also a zero. The Windows API has a function ZeroMemory()
that's a shortcut for filling memory with zeroes:
ZeroMemory(array, size of array in bytes)
So to initialize a WCHAR str[100]
you can say
ZeoMemory(str, 100 * sizeof (WCHAR))
where the sizeof (WCHAR)
turns 100 WCHAR
s into its equivalent byte count.
As the other answers say, simply setting the first character of a string to zero will be sufficient for a string. Your choice.
Also just to make sure: have you read the other answers to your other question? They are more geared toward the task you were trying to do (and I'm not at all knowledgeable on the process APIs; I just checked the docs for my answer).