Error Handling(Chapter 1 of Windows Via C/C++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 #include "..\CommonFiles\CmnHdr.h" /* See Appendix A. */
2 #include <Windowsx.h>
3 #include <tchar.h>
4 #include "Resource.h"
5
6
7 ///////////////////////////////////////////////////////////////////////////////
8
9
10 #define ESM_POKECODEANDLOOKUP (WM_USER + 100)
11 const TCHAR g_szAppName[] = TEXT("Error Show");
12
13
14 ///////////////////////////////////////////////////////////////////////////////
15
16
17 BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {
18
19 chSETDLGICONS(hwnd, IDI_ERRORSHOW);
20
21 // Don't accept error codes more than 5 digits long
22 Edit_LimitText(GetDlgItem(hwnd, IDC_ERRORCODE), 5);
23
24 // Look up the command-line passed error number
25 SendMessage(hwnd, ESM_POKECODEANDLOOKUP, lParam, 0);
26 return(TRUE);
27 }
28
29
30 ///////////////////////////////////////////////////////////////////////////////
31
32
33 void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
34
35 switch (id) {
36
37 case IDCANCEL:
38 EndDialog(hwnd, id);
39 break;
40
41 case IDC_ALWAYSONTOP:
42 SetWindowPos(hwnd, IsDlgButtonChecked(hwnd, IDC_ALWAYSONTOP)
43 ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
44 break;
45
46 case IDC_ERRORCODE:
47 EnableWindow(GetDlgItem(hwnd, IDOK), Edit_GetTextLength(hwndCtl) > 0);
48 break;
49
50 case IDOK:
51 // Get the error code
52 DWORD dwError = GetDlgItemInt(hwnd, IDC_ERRORCODE, NULL, FALSE);
53
54 HLOCAL hlocal = NULL; // Buffer that gets the error message string
55
56 // Use the default system locale since we look for Windows messages.
57 // Note: this MAKELANGID combination has 0 as value
58 DWORD systemLocale = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
59
60 // Get the error code's textual description
61 BOOL fOk = FormatMessage(
62 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
63 FORMAT_MESSAGE_ALLOCATE_BUFFER,
64 NULL, dwError, systemLocale,
65 (PTSTR) &hlocal, 0, NULL);
66
67 if (!fOk) {
68 // Is it a network-related error?
69 HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL,
70 DONT_RESOLVE_DLL_REFERENCES);
71
72 if (hDll != NULL) {
73 fOk = FormatMessage(
74 FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS |
75 FORMAT_MESSAGE_ALLOCATE_BUFFER,
76 hDll, dwError, systemLocale,
77 (PTSTR) &hlocal, 0, NULL);
78 FreeLibrary(hDll);
79 }
80 }
81
82 if (fOk && (hlocal != NULL)) {
83 SetDlgItemText(hwnd, IDC_ERRORTEXT, (PCTSTR) LocalLock(hlocal));
84 LocalFree(hlocal);
85 } else {
86 SetDlgItemText(hwnd, IDC_ERRORTEXT,
87 TEXT("No text found for this error number."));
88 }
89
90 break;
91 }
92 }
93
94
95 ///////////////////////////////////////////////////////////////////////////////
96
97
98 INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
99
100 switch (uMsg) {
101 chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
102 chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
103
104 case ESM_POKECODEANDLOOKUP:
105 SetDlgItemInt(hwnd, IDC_ERRORCODE, (UINT) wParam, FALSE);
106 FORWARD_WM_COMMAND(hwnd, IDOK, GetDlgItem(hwnd, IDOK), BN_CLICKED,
107 PostMessage);
108 SetForegroundWindow(hwnd);
109 break;
110 }
111
112 return(FALSE);
113 }
114
115
116 ///////////////////////////////////////////////////////////////////////////////
117
118
119 int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {
120
121 HWND hwnd = FindWindow(TEXT("#32770"), TEXT("Error Show"));
122 if (IsWindow(hwnd)) {
123 // An instance is already running, activate it and send it the new #
124 SendMessage(hwnd, ESM_POKECODEANDLOOKUP, _ttoi(pszCmdLine), 0);
125 } else {
126 DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_ERRORSHOW),
127 NULL, Dlg_Proc, _ttoi(pszCmdLine));
128 }
129 return(0);
130 }