Errormessages from Last-DLL-Error in ASCII

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Errormessages from Last-DLL-Error in ASCII

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Rings.

Too big to post here, download at
http://www.reelmediaproductions.com/pb/ ... essage.zip

Code: Select all

;NOTE: this code has been posted in lieu of the broken link above

Procedure.s GetErrorMessage()
  Define errCode, errMsgLength, errMessage.s, *msgBuffer
  errCode = GetLastError_() 
  If errCode
    *msgBuffer = AllocateMemory(255) 
    If *msgBuffer
      errMsgLength = FormatMessage_ (#FORMAT_MESSAGE_FROM_SYSTEM, #Null, 
                                     errCode, 0, *msgBuffer, 255, #Null) 
      If errMsgLength > 1
        errMessage = PeekS(*msgBuffer, errMsgLength - 2) 
      Else
        errMessage = "Undefined error." 
      EndIf  
      FreeMemory(*msgBuffer)
    Else
      errMessage = "Unable to retrieve error message." + Chr(10) +
                   "(look up error code: " + Str(errCode) + ")"
    EndIf
  Else 
    errMessage = "No error found." 
  EndIf 
  ProcedureReturn errMessage
EndProcedure 

;turn debugger off to test

LoadImage(1, "x:\xx.xxx")   ;load non-existent file
MessageRequester("GetLastError:", GetErrorMessage())
Its a long way to the top if you wanna .....CodeGuru