Partager via


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

Comments