First of all, when you say FORMAT_MESSAGE_ALLOCATE_BUFFER, you do not need to allocate more than a pointer. Then you pass a pointer to that pointer in lpBuffer. So try this:
TCHAR* lpMsgBuf;
if(!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL ))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
And do not forget to call LocalFree
or you allocate the buffer yourself:
TCHAR lpMsgBuf[512];
if(!FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) lpMsgBuf,
512, NULL ))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
Also, try this:
#include <cstdio>
#include <cstdlib>
int alloc(char** pbuff,unsigned int n)
{
*pbuff=(char*)malloc(n*sizeof(char));
}
int main()
{
char buffer[512];
printf("Address of buffer before: %p\n",&buffer);
// GCC sais: "cannot convert char (*)[512] to char** ... "
// alloc(&buffer,128);
// if i try to cast:
alloc((char**)&buffer,128);
printf("Address of buffer after: %p\n",&buffer);
// if i do it the right way:
char* p_buffer;
alloc(&p_buffer,128);
printf("Address of buffer after: %p\n",p_buffer);
return 0;
}
It does not make sense to try to change the address of a variable. That is probably why your code does not work.