2012-02-27 111 views
0

在此示例中,dwerror为10045L。但此代码返回0x13d值作为错误。 如何获取格式信息?请看看它。纠正此错误:GetLastError 0x13d

TCHAR lpMsgBuf[512]; 
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; 
} 
+0

我建议你看看错误代码0x13d是什么意思,例如[here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.100% 29.aspx) – 2012-02-27 09:00:57

回答

1

0x13d == 317 == ERROR_MR_MID_NOT_FOUND。 您尝试查找的错误消息在SYSTEM中不存在。 也许你的错误起源于特定的dll驱动程序。 如果您知道哪个DLL \驱动程序尝试获取它的句柄,并指定FORMAT_MESSAGE_FROM_HMODULE而不是FORMAT_MESSAGE_FROM_SYSTEM,并在FormatMessage的调用中提供句柄作为源。

除此之外,如果你使用FORMAT_MESSAGE_ALLOCATE_BUFFER应声明LPTSTR类型的变量一样LPTSTR pMsg;,并把它传递给作为的FormatMessage (LPTSTR)&pMsg和当你与它完成使用LocalFree(pMsg)释放分配的内存。

1

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

它确实尝试更改变量的地址是没有意义的。这可能是你的代码无法工作的原因。