home *** CD-ROM | disk | FTP | other *** search
- /*++
-
- Copyright 1996 - 1997 Microsoft Corporation
-
- Module Name:
-
- netmsg.c
-
- Abstract:
-
- The following sample illustrates how to display error text associated with
- Networking related error codes, in addition to displaying error text
- associated with system related error codes.
-
- If the supplied error number is in a specific range, the netmsg.dll
- message module is loaded and used to lookup the specified error number
- with the FormatMessage() Win32 API.
-
- Author:
-
- Scott Field (sfield) 29-Mar-96
-
- --*/
-
- #include <windows.h>
- #include <stdio.h>
-
- #include <lmerr.h>
-
- void
- DisplayErrorText(
- DWORD dwLastError
- );
-
- #define RTN_OK 0
- #define RTN_USAGE 1
- #define RTN_ERROR 13
-
- int
- __cdecl
- main(
- int argc,
- char *argv[]
- )
- {
- if(argc != 2) {
- fprintf(stderr,"Usage: %s <error number>\n", argv[0]);
- return RTN_USAGE;
- }
-
- DisplayErrorText( atoi(argv[1]) );
-
- return RTN_OK;
- }
-
- void
- DisplayErrorText(
- DWORD dwLastError
- )
- {
- HMODULE hModule = NULL; // default to system source
- LPSTR MessageBuffer;
- DWORD dwBufferLength;
-
- DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_IGNORE_INSERTS |
- FORMAT_MESSAGE_FROM_SYSTEM ;
-
- //
- // if dwLastError is in the network range, load the message source
- //
-
- if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
- hModule = LoadLibraryEx(
- TEXT("netmsg.dll"),
- NULL,
- LOAD_LIBRARY_AS_DATAFILE
- );
-
- if(hModule != NULL)
- dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
- }
-
- //
- // call FormatMessage() to allow for message text to be acquired
- // from the system or the supplied module handle
- //
-
- if(dwBufferLength = FormatMessageA(
- dwFormatFlags,
- hModule, // module to get message from (NULL == system)
- dwLastError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
- (LPSTR) &MessageBuffer,
- 0,
- NULL
- ))
- {
- DWORD dwBytesWritten;
-
- //
- // Output message string on stderr
- //
- WriteFile(
- GetStdHandle(STD_ERROR_HANDLE),
- MessageBuffer,
- dwBufferLength,
- &dwBytesWritten,
- NULL
- );
-
- //
- // free the buffer allocated by the system
- //
- LocalFree(MessageBuffer);
- }
-
- //
- // if we loaded a message source, unload it
- //
- if(hModule != NULL)
- FreeLibrary(hModule);
- }
-