Algorithms
Electronics
File Formats
Games Programming
Hardware/Architecture
Humour
Java/Javascript
Link Site,Library
Linux,GNU
Magazines/Books
Networks
New/Latest Stuff
Programming Languages
Protocols
Servers/ISAPI/ IO Completion Ports
Sound and Music
Source Code
Visual Basic
Windows
Winsock

Text Error Messages From GetLastError()

The Sample Code below shows a program that demonstrates how to use the FormatMessage function to display an error message from windows that was the last error on that thread.

It uses GetLastError to get the Error Code and then feeds it into FormatMessage which returns the message from the Operating System. GetLastError returns the error code of the last completed API call on that thread. The Error paramater holds the value returned by GetLastError.

Sample Program

include <windows.h>
include <stdio.h>

DisplayErrorMessage(int Error)
{
LPVOID ptr = 0;
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
               NULL,Error,
               MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
               (LPTSTR)&ptr,
               0,NULL);

printf("%s\n",ptr);
LocalFree(ptr);
}

int main()
{
int Err = GetLastError();
LoadLibrary("adfasdf");
DisplayErrorMessage(Err);
}

The Program will say something like "Cannot Find Library" or similiar.

Copyright Geoffrey Smith 2000