Welcome to MSDN Blogs Sign in | Join | Help

GetLastError is a number. What does it mean?

A customer asked

 

I'm getting an error 12 back from a GetLastError call, and I've seen a number of sites that tell me I can figure out what that error is by sending it to SysErrorMessage.  But nothing I've seen shows how to declare SysErrorMessage.  It doesn't appear to be in Kernal32 because I get an error if I try to DECLARE it there.  Where does this API live?

 

 

Understanding an error number from GetLastError() is probably useful. If you have Visual Studio installed, these errors are defined in

C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinError.h

For error 12, that files says:

//

// MessageId: ERROR_INVALID_ACCESS

//

// MessageText:

//

//  The access code is invalid.

//

#define ERROR_INVALID_ACCESS             12L

 

 

You can also start VS and choose Tools->Error Lookup

 

I haven’t heard of the “SysErrorMessage” function and a search of MSDN turns up nothing. There are a few mentions of it from internet searches. Perhaps it is outdated.

 

To turn an error number into English text, you can use the FormatMessage function. A parameter specifies what language the string should be. For that, we call GetSystemDefaultLangID

 

 

#define FORMAT_MESSAGE_ALLOCATE_BUFFER 0x00000100

#define FORMAT_MESSAGE_IGNORE_INSERTS  0x00000200

#define FORMAT_MESSAGE_FROM_STRING     0x00000400

#define FORMAT_MESSAGE_FROM_HMODULE    0x00000800

#define FORMAT_MESSAGE_FROM_SYSTEM     0x00001000

#define FORMAT_MESSAGE_ARGUMENT_ARRAY  0x00002000

#define FORMAT_MESSAGE_MAX_WIDTH_MASK  0x000000FF

 

DECLARE integer GetSystemDefaultLangID IN win32api

 

DECLARE integer FormatMessage IN WIN32API integer dwFlags,;

      integer lpSource,;

      integer dwMessageId,;

      integer dwLanguageId,;

      string @ lpBuffer,;

      integer nSize

langid=BITAND(0xffff,GetSystemDefaultLangID())

?TRANSFORM(langid,"@0x")

 

nErr=122

cstr=SPACE(1024)

nlen=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0,nErr,langid, @cstr, LEN(cstr))

IF nlen>0

      cErr=LEFT(cstr,nlen)

      ?"Message=",cErr

ENDIF

 

Published Thursday, September 22, 2005 11:36 AM by Calvin_Hsia

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: GetLastError is a number. What does it mean?

Thursday, September 22, 2005 2:55 PM by msvista
If one does not have an option to write FormatMessage, the link below (that I use normally) should also help to find what the meaing of each error code returned from GetLastError.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes__0-499_.asp

# re: GetLastError is a number. What does it mean?

Friday, September 23, 2005 9:40 PM by Jonny
You can also type 'net helpmsg 12' from the command prompt.

# GetLastError: how does it work with DECLARE DLL ?

Thursday, September 27, 2007 8:35 PM by Calvin Hsia's WebLog

Many Windows APIs can fail for various reasons. For example FormatMessage indicates failure by returning

# re: GetLastError is a number. What does it mean?

Thursday, July 31, 2008 2:57 PM by Bryan Kelly

Good page.  Just what I needed.  Thanks for posting it.

# Calvin Hsia s WebLog GetLastError is a number What does it mean | work from home

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker