|
|
1.1 root 1: /* Microsoft Developer Support
2: Copyright (c) 1992, 1993 Microsoft Corporation */
3:
4: #include <windows.h>
5: #include <stdio.h>
6:
7: /* messages.h is created by mc.exe when compiling messages.mc */
8: #include "messages.h"
9:
10: #define MAX_MESSAGES 5
11: #define MAX_MSG_LENGTH 1024
12: #define SEVERITY_MASK 0xC0000000
13: #define FACILITY_MASK 0x0FFF0000
14: #define MSG_ID_MASK 0x0000FFFF
15:
16:
17: /********************************************************************
18: * FUNCTION: myPutMsg(HINSTANCE hLib, LPVOID lpArgs, DWORD dwMsgId) *
19: * *
20: * PURPOSE: format and output error dwMsgId, with insert strings *
21: * lpArgs, from the messagetable resource in the DLL *
22: * referenced by handle hLib *
23: * *
24: * INPUT: Library handle, insert strings, and message ID number *
25: * *
26: * RETURNS: none *
27: ********************************************************************/
28:
29: void myPutMsg(HINSTANCE hLib, LPVOID lpArgs, DWORD dwMsgId)
30: {
31: BOOL bSuccess;
32: LPTSTR msgBuf; /* hold text of the error message that we build */
33: int dwCode; /* hold various codes extracted from dwMsgId */
34:
35: /* Here is the layout of the message ID:
36:
37: 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
38: 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
39: +---+-+-+-----------------------+-------------------------------+
40: |Sev|C|R| Facility | Code |
41: +---+-+-+-----------------------+-------------------------------+
42:
43: where
44:
45: Sev - is the severity code
46: C - is the Customer code flag
47: R - is a reserved bit
48: Facility - is the facility code
49: Code - is the facility's status code
50: */
51:
52: printf("Severity: ");
53: /* output the severity and facility code. Mask off the severity */
54: /* bits with SEVERITY_MASK and shift them down */
55: dwCode = (dwMsgId & SEVERITY_MASK) >> 30;
56: switch (dwCode)
57: {
58: case STATUS_SEVERITY_WARNING:
59: printf("STATUS_SEVERITY_WARNING"); break;
60: case STATUS_SEVERITY_SUCCESS:
61: printf("STATUS_SEVERITY_SUCCESS"); break;
62: case STATUS_SEVERITY_INFORMATIONAL:
63: printf("STATUS_SEVERITY_INFORMATIONAL"); break;
64: case STATUS_SEVERITY_ERROR:
65: printf("STATUS_SEVERITY_ERROR"); break;
66: default:
67: printf("Unknown!"); break;
68: }
69: printf ("\nFacility: ");
70: /* Mask off the facility bits with FACILITY_MASK and shift them down */
71: dwCode = (dwMsgId & FACILITY_MASK) >> 16;
72: switch (dwCode)
73: {
74: case FACILITY_SYSTEM:
75: printf("FACILITY_SYSTEM"); break;
76: case FACILITY_STUBS:
77: printf("FACILITY_STUBS"); break;
78: case FACILITY_RUNTIME:
79: printf("FACILITY_RUNTIME"); break;
80: case FACILITY_IO_ERROR_CODE:
81: printf("FACILITY_IO_ERROR_CODE"); break;
82: default:
83: printf("Unknown!"); break;
84: }
85: /* retrieve and format the message from the messagetable DLL */
86: bSuccess = FormatMessage(
87: FORMAT_MESSAGE_FROM_HMODULE | /* get the message from the DLL */
88: FORMAT_MESSAGE_ALLOCATE_BUFFER | /* allocate the msg buffer for us */
89: FORMAT_MESSAGE_ARGUMENT_ARRAY | /* lpArgs is an array of pointers */
90: 60, /* line length for the mesages */
91: hLib, /* the messagetable DLL handle */
92: dwMsgId, /* message ID */
93: MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), /* language ID */
94: (LPTSTR) &msgBuf, /* address of pointer to buffer for message */
95: MAX_MSG_LENGTH, /* maximum size of the message buffer */
96: lpArgs); /* array of insert strings for the message */
97: if (!bSuccess)
98: printf("Error %d from FormatMessage\n", GetLastError());
99: else
100: {
101: /* mask off the actual message number with MSG_ID_MASK and show it */
102: printf("\nError: %d: %s", dwMsgId & MSG_ID_MASK, msgBuf);
103: /* Free the buffer that FormatMessage allocated for us. */
104: LocalFree((HLOCAL) msgBuf);
105: }
106: puts("\n__________\n");
107: CloseHandle(hLib);
108: }
109:
110: /********************************************************************
111: * FUNCTION: main() *
112: * *
113: * PURPOSE: Load the message resource DLL, and call myPutMsg() to *
114: * format and output error messages to the user *
115: * *
116: * INPUT: none *
117: * *
118: * RETURNS: none *
119: ********************************************************************/
120:
121: int main()
122: {
123: HINSTANCE hLib; /* handle to the messagetable DLL */
124: char *aInsertStrs[8]; /* array of pointers to insert strings */
125: /* Check to make sure we are running on Windows NT */
126: if( GetVersion() & 0x80000000 )
127: {
128: MessageBox(NULL, "Sorry, this application requires Windows NT.\n"
129: "This application will now terminate.",
130: "Error: Windows NT Required to Run", MB_OK );
131: return(1);
132: }
133: /* Load the resource library without calling any entry points since */
134: /* this is a resource-only DLL */
135: hLib = LoadLibraryEx("messages.dll", NULL, DONT_RESOLVE_DLL_REFERENCES);
136: if (!hLib)
137: printf("Error %d from LoadLibrary\n", GetLastError());
138:
139: /* Output some error messages from the messagetable DLL */
140: /* The first three have no insert strings */
141: myPutMsg(hLib, NULL, MSG_BAD_COMMAND);
142: myPutMsg(hLib, NULL, MSG_BAD_PARM1);
143: myPutMsg(hLib, NULL, MSG_STRIKE_ANY_KEY);
144: /* wait for user to hit enter as per last error message */
145: getchar();
146: /* The next two messages contain insert strings - set up our array */
147: /* of insert strings and pass the array to myPutMsg() */
148: aInsertStrs[0] = "foo.c";
149: aInsertStrs[1] = "BAR";
150: myPutMsg(hLib, aInsertStrs, MSG_CMD_DELETE);
151: aInsertStrs[0] = "47";
152: aInsertStrs[1] = "5";
153: myPutMsg(hLib, aInsertStrs, MSG_RETRYS);
154: return(0);
155: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.