Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
C6276
Collapse the table of content
Expand the table of content

C6276

warning C6276: Cast between semantically different string types: char* to wchar_t*. Use of invalid string can lead to undefined behavior

This warning indicates a potentially incorrect cast from an ANSI string (char_t*) to a UNICODE string (wchar_t *). Because UNICODE strings have a character size of 2 bytes, this cast might yield strings that are not correctly terminated. Using such strings with the wcs* library of functions could cause buffer overruns and access violations.

The following code generates this warning:

#include <windows.h>
VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;

   pSrc = (LPWSTR)"a";
   wcscpy(szBuffer, pSrc); 
}

The following code corrects this warning by appending the letter L to represent the ASCII character as a wide character:

#include <windows.h>

VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;

   pSrc = L"a";
   wcscpy(szBuffer, pSrc);
}

The following code uses the safe string manipulation function, wcscpy_s, to correct this warning:

#include <windows.h>

VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;
   pSrc = L"a";
   wcscpy_s(szBuffer,8,pSrc); 
}
Show:
© 2015 Microsoft