首先,当你说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