home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / resource / msgtable / msgtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  6.9 KB  |  170 lines

  1. /* Microsoft Developer Support
  2.    Copyright (c) 1992-1997 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. *                    DWORD dwLangId)                                *
  20. *                                                                   *
  21. * PURPOSE: format and output error dwMsgId, using string defined    *
  22. *          for language dwLangId, with insert strings lpArgs, from  *
  23. *          the messagetable resource in the DLL referenced by       *
  24. *          handle hLib                                              *
  25. *                                                                   *
  26. * INPUT: Library handle, insert strings, message ID number, and     *
  27. *        language ID as defined in the .mc file                     *
  28. *                                                                   *
  29. * RETURNS: none                                                     *
  30. ********************************************************************/
  31.  
  32. void myPutMsg(HINSTANCE hLib, LPVOID lpArgs, DWORD dwMsgId, DWORD dwLangId)
  33. {
  34.   BOOL bSuccess;
  35.   LPTSTR msgBuf;  /* hold text of the error message that we build */
  36.   int dwCode;  /* hold various codes extracted from dwMsgId */
  37.  
  38.   /* Here is the layout of the message ID:
  39.  
  40.    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  41.    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
  42.   +---+-+-+-----------------------+-------------------------------+
  43.   |Sev|C|R|     Facility          |               Code            |
  44.   +---+-+-+-----------------------+-------------------------------+
  45.  
  46.   where
  47.  
  48.       Sev - is the severity code
  49.       C - is the Customer code flag
  50.       R - is a reserved bit
  51.       Facility - is the facility code
  52.       Code - is the facility's status code
  53.   */
  54.  
  55.   printf("Severity: ");
  56.   /* output the severity and facility code. Mask off the severity */
  57.   /* bits with SEVERITY_MASK and shift them down */
  58.   dwCode = (dwMsgId & SEVERITY_MASK) >> 30;
  59.   switch (dwCode)
  60.     {
  61.     case STATUS_SEVERITY_WARNING:
  62.       printf("STATUS_SEVERITY_WARNING"); break;
  63.     case STATUS_SEVERITY_SUCCESS:
  64.       printf("STATUS_SEVERITY_SUCCESS"); break;
  65.     case STATUS_SEVERITY_INFORMATIONAL:
  66.       printf("STATUS_SEVERITY_INFORMATIONAL"); break;
  67.     case STATUS_SEVERITY_ERROR:
  68.       printf("STATUS_SEVERITY_ERROR"); break;
  69.     default:
  70.       printf("Unknown!"); break;
  71.     }
  72.   printf ("\nFacility: ");
  73.   /* Mask off the facility bits with FACILITY_MASK and shift them down */
  74.   dwCode = (dwMsgId & FACILITY_MASK) >> 16;
  75.   switch (dwCode)
  76.     {
  77.     case FACILITY_SYSTEM:
  78.       printf("FACILITY_SYSTEM"); break;
  79.     case FACILITY_STUBS:
  80.       printf("FACILITY_STUBS"); break;
  81.     case FACILITY_RUNTIME:
  82.       printf("FACILITY_RUNTIME"); break;
  83.     case FACILITY_IO_ERROR_CODE:
  84.       printf("FACILITY_IO_ERROR_CODE"); break;
  85.     default:
  86.       printf("Unknown!"); break;
  87.     }
  88.   /* retrieve and format the message from the messagetable DLL. */
  89.   bSuccess = FormatMessage(
  90.       FORMAT_MESSAGE_FROM_HMODULE | /* get the message from the DLL */
  91.       FORMAT_MESSAGE_ALLOCATE_BUFFER | /* allocate the msg buffer for us */
  92.       FORMAT_MESSAGE_ARGUMENT_ARRAY | /* lpArgs is an array of 32-bit values */
  93.       60, /* line length for the mesages */
  94.       hLib, /* the messagetable DLL handle */
  95.       dwMsgId, /* message ID */
  96.       dwLangId, /* language ID as defined in .mc file */
  97.       (LPTSTR) &msgBuf, /* address of pointer to buffer for message */
  98.       MAX_MSG_LENGTH, /* maximum size of the message buffer */
  99.       lpArgs); /* array of insert strings for the message */
  100.   if (!bSuccess)
  101.     printf("Error %d from FormatMessage\n", GetLastError());
  102.   else
  103.     {
  104.     /* mask off the actual message number with MSG_ID_MASK and show it */
  105.     printf("\nError: %d: %s", dwMsgId & MSG_ID_MASK, msgBuf);
  106.     /* Free the buffer that FormatMessage allocated for us. */
  107.     LocalFree((HLOCAL) msgBuf);
  108.     }
  109.   puts("\n__________\n");
  110.   CloseHandle(hLib);
  111. }
  112.  
  113. /********************************************************************
  114. * FUNCTION: main()                                                  *
  115. *                                                                   *
  116. * PURPOSE: Load the message resource DLL, and call myPutMsg() to    *
  117. *          format and output error messages to the user             *
  118. *                                                                   *
  119. * INPUT: none                                                       *
  120. *                                                                   *
  121. * RETURNS: none                                                     *
  122. ********************************************************************/
  123.  
  124. int main()
  125. {
  126.   HINSTANCE hLib;  /* handle to the messagetable DLL */
  127.   PTCHAR aInsertStrs[8];  /* array of 32-bit insert values for FormatMessage*/
  128.   WORD wLangID;
  129.  
  130.   /* Check to make sure we are running on Windows NT */
  131.   if( GetVersion() & 0x80000000 )
  132.     {
  133.     MessageBox(NULL, "Sorry, this application requires Windows NT.\n"
  134.         "This application will now terminate.",
  135.         "Error: Windows NT Required to Run",  MB_OK );
  136.     return(1);
  137.     }
  138.   /* Load the resource library without calling any entry points since */
  139.   /* this is a resource-only DLL */
  140.   hLib = LoadLibraryEx("messages.dll", NULL, DONT_RESOLVE_DLL_REFERENCES);
  141.   if (!hLib)
  142.     printf("Error %d from LoadLibrary\n", GetLastError());
  143.   /* Messages in the .mc file have been defined using standard locale ID's:
  144.      see MAKELANGID and LANG_* definitions in winnt.h */
  145.  
  146.   wLangID = LANG_USER_DEFAULT;
  147.   /* to force a different language, define the language ID as one of the values
  148.      defined in the Language statement in the .mc file. Alternatively, you
  149.      can choose a different language in the International applet in the control
  150.      panel and stick with LANG_USER_DEFAULT. */
  151.   //wLangID = 0x411;
  152.  
  153.   /* Output some error messages from the messagetable DLL */
  154.   /* The first three have no insert strings */
  155.   myPutMsg(hLib, NULL, MSG_BAD_COMMAND, wLangID);
  156.   myPutMsg(hLib, NULL, MSG_BAD_PARM1, wLangID);
  157.   myPutMsg(hLib, NULL, MSG_STRIKE_ANY_KEY, wLangID);
  158.   /* wait for user to hit enter as per last error message */
  159.   getchar();
  160.   /* The next two messages contain insert strings - set up our array */
  161.   /* of insert strings and pass the array to myPutMsg() */
  162.   aInsertStrs[0] = "foo.c";
  163.   aInsertStrs[1] = "BAR";
  164.   myPutMsg(hLib, aInsertStrs, MSG_CMD_DELETE, wLangID);
  165.   aInsertStrs[0] = (PTCHAR) 47;
  166.   aInsertStrs[1] = (PTCHAR) 5;
  167.   myPutMsg(hLib, aInsertStrs, MSG_RETRYS, wLangID);
  168.   return(0);
  169. }
  170.