In Windows' FormatMessage() function, the parameter:
_Out_ LPTSTR lpBuffer
FormatMessage() is documented as follows:
A pointer to a buffer that receives the null-terminated string that specifies the formatted message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc function, and places the pointer to the buffer at the address specified in lpBuffer.
So there are 2 different usages of
1) Provide your own buffer
const DWORD bufsize = ....; TCHAR buf[bufsize]; FormatMessage(.... buf, bufsize, ....); // buf is passed as a TCHAR*
2) FormatMessage allocates a buffer for you
const DWORD bufsize = ....; TCHAR* buf = 0; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | ...., .... (LPTSTR)&buf, bufsize, ....); // &buf is a TCHAR** so type-cast needed! .... LocalFree(buf);
In #1, you have to pass the address of the first
TCHAR in your buffer, and the function simply fills it the buffer.
In #2, the function needs to tell you where it allocates a new buffer, so you have to tell it where to place that address. You have to pass the address of a pointer variable that receives the address.
TCHAR*to an existing buffer
TCHAR**that receives a new buffer
That is why the
lpBuffer parameter has to be type-casted when using #2.