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

  1. /*++
  2.  
  3. Copyright 1996 - 1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     netmsg.c
  8.  
  9. Abstract:
  10.  
  11.     The following sample illustrates how to display error text associated with
  12.     Networking related error codes, in addition to displaying error text
  13.     associated with system related error codes.
  14.  
  15.     If the supplied error number is in a specific range, the netmsg.dll
  16.     message module is loaded and used to lookup the specified error number
  17.     with the FormatMessage() Win32 API.
  18.  
  19. Author:
  20.  
  21.     Scott Field (sfield)    29-Mar-96
  22.  
  23. --*/
  24.  
  25. #include <windows.h>
  26. #include <stdio.h>
  27.  
  28. #include <lmerr.h>
  29.  
  30. void
  31. DisplayErrorText(
  32.     DWORD dwLastError
  33.     );
  34.  
  35. #define RTN_OK 0
  36. #define RTN_USAGE 1
  37. #define RTN_ERROR 13
  38.  
  39. int
  40. __cdecl
  41. main(
  42.     int argc,
  43.     char *argv[]
  44.     )
  45. {
  46.     if(argc != 2) {
  47.         fprintf(stderr,"Usage: %s <error number>\n", argv[0]);
  48.         return RTN_USAGE;
  49.     }
  50.  
  51.     DisplayErrorText( atoi(argv[1]) );
  52.  
  53.     return RTN_OK;
  54. }
  55.  
  56. void
  57. DisplayErrorText(
  58.     DWORD dwLastError
  59.     )
  60. {
  61.     HMODULE hModule = NULL; // default to system source
  62.     LPSTR MessageBuffer;
  63.     DWORD dwBufferLength;
  64.  
  65.     DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
  66.         FORMAT_MESSAGE_IGNORE_INSERTS |
  67.         FORMAT_MESSAGE_FROM_SYSTEM ;
  68.  
  69.     //
  70.     // if dwLastError is in the network range, load the message source
  71.     //
  72.  
  73.     if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
  74.         hModule = LoadLibraryEx(
  75.             TEXT("netmsg.dll"),
  76.             NULL,
  77.             LOAD_LIBRARY_AS_DATAFILE
  78.             );
  79.  
  80.         if(hModule != NULL)
  81.             dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  82.     }
  83.  
  84.     //
  85.     // call FormatMessage() to allow for message text to be acquired
  86.     // from the system or the supplied module handle
  87.     //
  88.  
  89.     if(dwBufferLength = FormatMessageA(
  90.         dwFormatFlags,
  91.         hModule, // module to get message from (NULL == system)
  92.         dwLastError,
  93.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
  94.         (LPSTR) &MessageBuffer,
  95.         0,
  96.         NULL
  97.         ))
  98.     {
  99.         DWORD dwBytesWritten;
  100.  
  101.         //
  102.         // Output message string on stderr
  103.         //
  104.         WriteFile(
  105.             GetStdHandle(STD_ERROR_HANDLE),
  106.             MessageBuffer,
  107.             dwBufferLength,
  108.             &dwBytesWritten,
  109.             NULL
  110.             );
  111.  
  112.         //
  113.         // free the buffer allocated by the system
  114.         //
  115.         LocalFree(MessageBuffer);
  116.     }
  117.  
  118.     //
  119.     // if we loaded a message source, unload it
  120.     //
  121.     if(hModule != NULL)
  122.         FreeLibrary(hModule);
  123. }
  124.