2012-02-27 108 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; 
} 

它確實嘗試更改變量的地址是沒有意義的。這可能是你的代碼無法工作的原因。