c++winapic-stringslpcwstr

Using, StringCchCat


I'm trying to use the StringCchCat function:

    HRESULT X;
    LPWSTR _strOutput =  new wchar_t[100];
    LPCWSTR Y =L"Sample Text";
    X = StringCchCat(_strOutput, 100, Y);

But for some reason I keep getting the "E_INVALIDARG One or more arguments are invalid." error from X. _strOutput Is also full of some random characters.

This is actually part of a bigger program. So what I'm trying to do is to concatenated the "sample text" to the empty _strOutput variable. This is inside a loop so it is going to happen multiple times. For this particular example it will be as if I'm assigning the Text "Sample Text" to _strrOutput.

Any Ideas?


Solution

  • String copy/concat functions look for null terminators to know where to copy/concat to. You need to initialize the first element of _strOutput to zero so the buffer is null terminated, then you can copy/concat values to it as needed:

    LPWSTR _strOutput =  new wchar_t[100];
    _strOutput[0] = L'\0`; // <-- add this
    X = StringCchCat(_strOutput, 100, Y);