如果我们自己写程序的时候检测到一个错误的时候,可能希望向用户显示错误的文本表述,而不是一个干巴巴的错误代码windows提供了一个函数可以将错误代码转换成错误文本描述,这个函数是FormatMessage。
TheFormatMessage function formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
DWORD FormatMessage( DWORD dwFlags, // source and processing options LPCVOID lpSource, // message source DWORD dwMessageId, // message identifier DWORD dwLanguageId, // language identifier LPTSTR lpBuffer, // message buffer DWORD nSize, // maximum size of message buffer va_list *Arguments // array of message inserts );参数说明: dwFlags:
标志位,决定如何说明lpSource参数,dwFlags的低位制定如何处理换行功能在输出缓冲区,也决定最大宽度的格式化输出行。
可选参数:
标志 |
标志说明 |
|
|
|
Arguments参数不是指向va_list结构体,但是是一个指向保存参数的数据。 |
|
|
|
|
|
|
|
|
如果函数调用成功,返回输出缓冲区的大小,除最后一个空字符。如果失败侧返回0。
资源文件:
// // Dialog // IDD_DIALOG1 DIALOGEX 0, 0, 177, 83 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_APPWINDOW CAPTION "Dialog" FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN DEFPUSHBUTTON "确定",IDOK,114,11,50,14 LTEXT "Error:",IDC_STATIC,10,12,20,14 EDITTEXT IDC_ERRORCODE,38,11,40,14,ES_AUTOHSCROLL EDITTEXT IDC_ERRORTEXT,10,28,156,45,ES_MULTILINE | ES_AUTOHSCROLL END
代码:
#include <windows.h> #include "resource.h" LRESULT CALLBACK DialogProc(HWND ,UINT,WPARAM,LPARAM) ; void OnInitDialog(HWND hDlg); void OnOK(HWND hDlg); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MSG msg; HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc); ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); while(GetMessage(&msg, NULL, 0, 0)) { if( !IsDialogMessage( hwnd, &msg ) ) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } LRESULT CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: OnInitDialog(hWnd); SetWindowPos(hWnd,NULL,500,200,0,0,SWP_NOSIZE); return TRUE; case WM_COMMAND: if(LOWORD(wParam) == IDOK) { OnOK(hWnd); return TRUE; } if(LOWORD(wParam) == IDCANCEL) { PostQuitMessage(0); return TRUE; } break; case WM_DESTROY: PostQuitMessage(0); break; } return FALSE; } void OnInitDialog(HWND hDlg) { SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1))); SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1))); SetDlgItemInt(hDlg, IDC_ERRORCODE, 0, FALSE); } void OnOK(HWND hDlg) { DWORD dwError = GetDlgItemInt(hDlg,IDC_ERRORCODE,NULL, FALSE); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (PTSTR)&lpMsgBuf, 0, NULL ); SetDlgItemText(hDlg,IDC_ERRORTEXT,(PCTSTR)lpMsgBuf); // Free the buffer. LocalFree(lpMsgBuf); }