1 string GetErroMessage(DWORD dwError)
 2 {
 3     HLOCAL hLocal;
 4     hLocal=NULL;
 5     BOOL bFun;
 6     HMODULE hDll=LoadLibraryEx(TEXT("netmsg.dll"),NULL,DONT_RESOLVE_DLL_REFERENCES);
 7     bFun=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,dwError,MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),(PTSTR)&hLocal,0,NULL);
 8     if(!bFun)
 9     {
10 
11         if(!hDll)
12         {
13             FormatMessage(FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_FROM_SYSTEM,hDll,dwError,MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),(PTSTR)&hLocal,0,NULL);
14             FreeLibrary(hDll);
15         }
16     }
17 
18     if(hLocal!=NULL)
19     {
20         char temp[6];
21         itoa(dwError,temp,10);
22         string str("错误代码 ");
23         str += temp;
24         str += ":";
25         str +=(PTSTR)LocalLock(hLocal);
26         return str;
27     }
28 
29 }

 

 

测试:

 1 #include <iostream>
 2 #include <winsock2.h>//2.0版本
 3 using namespace std;
 4 #include <string>
 5 
 6 int main(void) 
 7 {     
 8 
 9  string strErroMsg;
10 
11 
12  HINSTANCE hInstance=LoadLibrary("d.dll");
13  if (hInstance == NULL)
14  {
15   cout<<GetErroMessage(GetLastError());//错误代码 126:找不到指定的模块。
16  }
17 
18 }