VS中添加监视$err,hr可以查询到错误码和对应的解释。也可以查看 winerror.h

方法1.

   

 1  ListBox.ResetContent();//清空CListBox
 2 
 3     LPVOID lpMsgBuf;
 4 
 5     for (int i=0;i<1000;i++)
 6 
 7     {
 8 
 9        DWORD dw =i; //在预测有错误的地方加上dw=GetLastError
10 
11        FormatMessage(
12 
13            FORMAT_MESSAGE_ALLOCATE_BUFFER |
14 
15            FORMAT_MESSAGE_FROM_SYSTEM,
16 
17            NULL,
18 
19            dw,
20 
21            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
22 
23            (LPTSTR) &lpMsgBuf,
24 
25            0, NULL );//将中文的GetLastError对应的值返回到lpMsgBuf
26 
27        if (lpMsgBuf != NULL)
28 
29        {
30 
31            CString str;
32 
33            str.Format("%d:%s",i,(LPTSTR)lpMsgBuf);
34 
35            ListBox.AddString(str);
36 
37        }
38 
39    
40 
41        LocalFree(lpMsgBuf);
42 
43     }

 

 

方法2.

 1 ListBox.ResetContent();
 2 
 3     HLOCAL hLocal;
 4 
 5     hLocal=NULL;
 6 
 7     BOOL bFun;
 8 
 9     HMODULE hDll=LoadLibraryEx(TEXT("netmsg.dll"),NULL,DONT_RESOLVE_DLL_REFERENCES);
10 
11     for(int i=0;i<=15082;i++)
12 
13     {
14 
15        DWORD dwError=i;
16 
17         bFun=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,dwError,MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),(PTSTR)&hLocal,0,NULL);
18 
19        if(!bFun)
20 
21        {
22 
23  
24 
25            if(!hDll)
26 
27            {
28 
29                FormatMessage(FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_FROM_SYSTEM,hDll,dwError,MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),(PTSTR)&hLocal,0,NULL);
30 
31               FreeLibrary(hDll);
32 
33            }
34 
35        }
36 
37  
38 
39        if(hLocal!=NULL)
40 
41        {
42 
43            CString str;
44 
45            str.Format("%d:%s",i,(PTSTR)LocalLock(hLocal));
46 
47            ListBox.AddString(str);
48 
49        }
50 
51  
52 
53     }
54 
55