GetLastError error lookup tool source codeThis snippet submitted by Ken Fitlike on 2005-01-20. It has been viewed 47476 times.Rating of 5.4 with 245 votes /*===========================================================================*/ /* Error code Lookup Tool Copyright (c) Ken Fitlike 2004 Free to good home Displays the text message associated with the numerical error codes returned by the GetLastError api function. Notes: GWL_USERDATA used instead of GWLP_USERDATA with GetWindowLongPtr and SetWindowLongPtr because latter not defined for MinGW w32api 3.1; the values are the same in any case. Disclaimer: Any problems you have with this code are just that: your problems. */ /*===========================================================================*/ #include <windows.h> #include <tchar.h> /*lcc-win32 may not define LR_SHARED*/ #if !defined LR_SHARED #define LR_SHARED 0x00008000 #endif /*control identifiers*/ enum { #if defined IDC_STATIC #undef IDC_STATIC #endif IDC_STATIC=-1, IDC_ERRNUM=100, IDC_BTNGO, IDC_ERRMSG, IDM_HELP=40000, IDM_ABOUT }; /*===========================================================================*/ LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam); HACCEL BuildAccelTable(void); void CentreWnd(HWND hwnd); BOOL CALLBACK SetChildFonts(HWND hChild,LPARAM lParam); void ShowErrMsg(HWND hEdit,int errnum); void AddSysMenu(HWND hwnd); void ShowAboutBox(HWND hParent); void ShowHelpBox(HWND hParent); /*===========================================================================*/ int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR pStr,int nCmd) { HWND hwnd; MSG msg; TCHAR classname[]=_T("kenf's_wnd"); WNDCLASSEX wcx={0}; wcx.cbSize =sizeof(wcx); wcx.lpfnWndProc =WndProc; wcx.hInstance =hInst; wcx.hIcon =(HICON)LoadImage(0,IDI_INFORMATION,IMAGE_ICON,0,0,LR_SHARED); wcx.hCursor =(HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED); wcx.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1); wcx.lpszClassName=classname; if (RegisterClassEx(&wcx)) { hwnd=CreateWindowEx(0,classname,_T("Error Message Tool"), WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME, 0, 0, 278, 178, 0,0,hInst,0); if (hwnd) { CentreWnd(hwnd); ShowWindow(hwnd,nCmd); UpdateWindow(hwnd); while (GetMessage(&msg,0,0,0)>0) { /*retrieve and use accel table handle from main wnd user data*/ if (!TranslateAccelerator(hwnd,(HACCEL)GetWindowLongPtr(hwnd,GWL_USERDATA), &msg)) { if (!IsDialogMessage(hwnd,&msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } return (int)msg.wParam; } else { MessageBox(0,_T("Wnd creation failure"),_T("Error"),MB_OK|MB_ICONERROR); return 0; } } MessageBox(0,_T("Wnd registration failure"),_T("Error"),MB_OK|MB_ICONERROR); return 0; } /*===========================================================================*/ LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { static HWND hEditNum,hBtnGo,hEditMsg; switch (uMsg) { case WM_CREATE: { /*create an accelerator table and store it as the main wnd user data*/ SetWindowLongPtr(hwnd,GWL_USERDATA,(LONG_PTR)BuildAccelTable()); AddSysMenu(hwnd); hEditNum=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T("0"), WS_CHILD|WS_VISIBLE|ES_NUMBER|WS_TABSTOP, 56,8,138,28, hwnd,(HMENU)IDC_ERRNUM,GetModuleHandle(0),0); SendMessage(hEditNum,EM_SETLIMITTEXT,5,0); CreateWindowEx(0,_T("static"),_T("Value:"), WS_CHILD|WS_VISIBLE|SS_CENTERIMAGE, 8,8,48,28, hwnd,(HMENU)IDC_STATIC,GetModuleHandle(0),0); hBtnGo=CreateWindowEx(0,_T("button"),_T("Show"), WS_VISIBLE|WS_CHILD|WS_TABSTOP, 208,8,56,28, hwnd,(HMENU)IDC_BTNGO,GetModuleHandle(0),0); CreateWindowEx(0,_T("static"),_T("Message:"), WS_CHILD|WS_VISIBLE|SS_CENTERIMAGE, 8,42,48,28, hwnd,(HMENU)IDC_STATIC,GetModuleHandle(0),0); hEditMsg=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),0, WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_READONLY| WS_TABSTOP, 8,70,256,68, hwnd,(HMENU)IDC_ERRNUM,GetModuleHandle(0),0); EnumChildWindows(hwnd,SetChildFonts,0); SetFocus(hEditNum); SendMessage(hEditNum,EM_SETSEL,0,-1); return 0; } case WM_COMMAND: { WORD wID,wNotify; wID=LOWORD(wParam); wNotify=HIWORD(wParam); if (wNotify==BN_CLICKED) { switch (wID) { case IDCANCEL: DestroyWindow(hwnd); return 0; case IDOK: /*'enter' key pressed when edit has focus*/ case IDC_BTNGO: { ShowErrMsg(hEditMsg,GetDlgItemInt(hwnd,IDC_ERRNUM,0,0)); return 0; } } } return 0; } case WM_DESTROY: DestroyAcceleratorTable((HACCEL)GetWindowLongPtr(hwnd,GWL_USERDATA)); PostQuitMessage(0); return 0; case WM_SYSCOMMAND: { UINT uCmd=(UINT)wParam; switch (uCmd) { case IDM_ABOUT: ShowAboutBox(hwnd); return 0; case IDM_HELP: ShowHelpBox(hwnd); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } } /*===========================================================================*/ void AddSysMenu(HWND hwnd) { /*retrieves copy of system (window) menu and adds a couple of options to it*/ HMENU hMenu; hMenu=GetSystemMenu(hwnd,0); AppendMenu(hMenu,MF_SEPARATOR,0,0); AppendMenu(hMenu,MF_STRING,IDM_HELP,_T("Help...")); AppendMenu(hMenu,MF_STRING,IDM_ABOUT,_T("About...")); } /*===========================================================================*/ HACCEL BuildAccelTable(void) { /*building accelerator table dynamically for simplicity of presentation only - use of resources should be preferred*/ ACCEL ac; ac.fVirt=FVIRTKEY; ac.key=VK_F1; ac.cmd=IDM_HELP; return CreateAcceleratorTable(&ac,1); } /*===========================================================================*/ void CentreWnd(HWND hwnd) { /*centre wnd on desktop*/ RECT rc,rcDesk; SystemParametersInfo(SPI_GETWORKAREA,0,&rcDesk,0); GetWindowRect(hwnd,&rc); MoveWindow(hwnd,(rcDesk.right-rc.right+rc.left)/2, (rcDesk.bottom-rc.bottom+rc.top)/2, rc.right-rc.left, rc.bottom-rc.top,0); } /*===========================================================================*/ BOOL CALLBACK SetChildFonts(HWND hChild,LPARAM lParam) { /*EnumChildProc*/ SendMessage(hChild,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(1,0)); return TRUE; } /*===========================================================================*/ void ShowAboutBox(HWND hParent) { /*using MessageBoxIndirect to give option of having user defined icon - this option is not taken in this example */ HWND hFocus; int len; MSGBOXPARAMS mbp; TCHAR *txt; TCHAR ab[]=_T("About "); len=lstrlen(ab)+GetWindowTextLength(hParent)+1; if ((txt=malloc(len*sizeof(TCHAR)))!=0) { lstrcpy(txt,ab); GetWindowText(hParent,&txt[lstrlen(ab)],len); } hFocus=GetFocus(); mbp.cbSize=sizeof(mbp); mbp.hwndOwner=hParent; /*if using own icon then set mbp.hInstance=GetModuleHandle(0);*/ mbp.hInstance=0; mbp.lpszText=_T("Copyright (c) 2004\n\nKen Fitlike"); if (txt) { mbp.lpszCaption=txt; } else { mbp.lpszCaption=ab; } mbp.dwStyle=MB_USERICON; mbp.lpszIcon=IDI_INFORMATION; mbp.dwContextHelpId=0; mbp.lpfnMsgBoxCallback=0; mbp.dwLanguageId=MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK); MessageBoxIndirect(&mbp); if (txt) {free(txt);} SetFocus(hFocus); } /*===========================================================================*/ void ShowErrMsg(HWND hEdit,int errnum) { HWND hFocus; void *pMsg; hFocus=GetFocus(); if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, 0, errnum, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPTSTR)&pMsg, 0, NULL)==0) { /*nothing doing*/ MessageBox(0, _T("There is no message associated\n with that error code."), _T("Error"), MB_OK|MB_ICONEXCLAMATION ); } else { /*show it*/ SetWindowText(hEdit,pMsg); LocalFree(pMsg); /*free memory*/ } SetFocus(hFocus); } /*===========================================================================*/ void ShowHelpBox(HWND hParent) { HWND hFocus; hFocus=GetFocus(); MessageBox(hParent, _T("F1\tHelp\nESC\tQuit\n\n") _T("Enter numerical error code in 'value' field\n") _T("and click the 'show' button to display any\n") _T("error message that corresponds to that value.\n"), _T("Help"), MB_OK); SetFocus(hFocus); } /*===========================================================================*/ More C and C++ source code snippets |