Throughout the examples in this chapter, you might have noticed that whenever an error occurs, the ProcessCDError function is called. This isn't a system function; rather, it's a function I wrote that gets the extended error from the common dialog library and then displays a message box that describes the problem. When a common dialog function fails, an application can call the CommDlgExtendedError function, which returns an error value that identifies the cause of the most recent error. I created a string ID table containing descriptions of each error. In a switch statement, I use the error code to determine which string to load and then display in the message box.
void ProcessCDError(DWORD dwErrorCode, HWND hWnd)
{
WORD wStringID;
char buf [MAX_PATH];
switch (dwErrorCode)
{
case CDERR_STRUCTSIZE: wStringID = IDS_STRUCTSIZE; break;
case CDERR_INITIALIZATION: wStringID = IDS_INITIALIZATION; break;
case CDERR_NOTEMPLATE: wStringID = IDS_NOTEMPLATE; break;
case CDERR_NOHINSTANCE: wStringID = IDS_NOHINSTANCE; break;
case CDERR_LOADSTRFAILURE: wStringID = IDS_LOADSTRFAILURE; break;
case CDERR_FINDRESFAILURE: wStringID = IDS_FINDRESFAILURE; break;
case CDERR_LOADRESFAILURE: wStringID = IDS_LOADRESFAILURE; break;
case CDERR_LOCKRESFAILURE: wStringID = IDS_LOCKRESFAILURE; break;
case CDERR_MEMALLOCFAILURE: wStringID = IDS_MEMALLOCFAILURE;
break;
case CDERR_MEMLOCKFAILURE: wStringID = IDS_MEMLOCKFAILURE; break;
case CDERR_NOHOOK: wStringID = IDS_NOHOOK; break;
case PDERR_PARSEFAILURE: wStringID = IDS_PARSEFAILURE; break;
case PDERR_RETDEFFAILURE: wStringID = IDS_RETDEFFAILURE; break;
case PDERR_LOADDRVFAILURE: wStringID = IDS_LOADDRVFAILURE; break;
case PDERR_GETDEVMODEFAIL: wStringID = IDS_GETDEVMODEFAIL; break;
case PDERR_INITFAILURE: wStringID = IDS_INITFAILURE; break;
case PDERR_NODEVICES: wStringID = IDS_NODEVICES; break;
case PDERR_NODEFAULTPRN: wStringID = IDS_NODEFAULTPRN; break;
case PDERR_DNDMMISMATCH: wStringID = IDS_DNDMMISMATCH; break;
case PDERR_CREATEICFAILURE: wStringID = IDS_CREATEICFAILURE;
break;
case PDERR_PRINTERNOTFOUND: wStringID = IDS_PRINTERNOTFOUND;
break;
case CFERR_NOFONTS: wStringID = IDS_NOFONTS; break;
case FNERR_SUBCLASSFAILURE: wStringID = IDS_SUBCLASSFAILURE;
break;
case FNERR_INVALIDFILENAME: wStringID = IDS_INVALIDFILENAME;
break;
case FNERR_BUFFERTOOSMALL: wStringID = IDS_BUFFERTOOSMALL; break;
case 0: // user might have clicked Cancel,
return; // or this is a *very* random error
default:
wStringID = IDS_UNKNOWNERROR;
}
LoadString (NULL, wStringID, buf, sizeof (buf));
MessageBox (hWnd, buf, NULL, MB_OK);
return;
}