まず、FORMAT_MESSAGE_ALLOCATE_BUFFER と言うと、ポインタ以上を割り当てる必要はありません。次に、lpBuffer でそのポインターへのポインターを渡します。だからこれを試してください:
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;
}
また、LocalFree に電話することを忘れないでください。
または、バッファを自分で割り当てます。
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;
}
また、これを試してください:
#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;
}
変数のアドレスを変更しようとしても意味がありません。それがおそらく、コードが機能しない理由です。