首先,當你說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;
}
它確實嘗試更改變量的地址是沒有意義的。這可能是你的代碼無法工作的原因。
我建議你看看錯誤代碼0x13d是什麼意思,例如[here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.100% 29.aspx) – 2012-02-27 09:00:57