Article ID: 149409
Article Last Modified on 7/11/2005
/*
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.
This sample relies on the following import library:
User32.lib
*/
#include <windows.h>
#include <stdio.h>
#include <lmerr.h>
void
DisplayErrorText(
DWORD dwLastError
);
int
__cdecl
main(
void
)
{
//
// display a networking related error string
//
printf("Network related error string follows:\n");
DisplayErrorText(2226);
//
// display a system related error string
//
printf("\nSystem related error string follows:\n");
SetLastError(ERROR_FILE_NOT_FOUND);
DisplayErrorText(GetLastError());
return 0;
}
void
DisplayErrorText(
DWORD dwLastError
)
{
HMODULE hModule = NULL; // default to system source
LPSTR MessageBuffer;
DWORD dwBufferLength;
//
// 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
);
}
//
// call FormatMessage() to allow for message text to be acquired
// from the system or the supplied module handle
//
if(dwBufferLength = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM | // always consider system table
((hModule != NULL) ? FORMAT_MESSAGE_FROM_HMODULE : 0),
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 you loaded a message source, unload it
//
if(hModule != NULL)
FreeLibrary(hModule);
}
Additional query words: error string LanMan
Keywords: kbhowto kbapi kbnetwork kbcode KB149409