Two FormatMessage solutions.

Share your advanced PureBasic knowledge/code with the community.
User avatar
SimpleMind
Enthusiast
Enthusiast
Posts: 112
Joined: Sun May 18, 2003 12:40 pm
Location: Netherlands

Two FormatMessage solutions.

Post by SimpleMind »

Code updated for 5.20+

Hi,

Two solutions to translate the error number into a text message. The first found on this Forum and adapted, the second translated from VB. Use the one you like the best

Code: Select all

Procedure.s GetSystemMessage(lngErrorCode.l)
  MemoryID.i = AllocateMemory (255)
  tchar.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, #Null, lngErrorCode, 0, MemoryID, 255, #Null)
  e$ = PeekS(MemoryID)
  FreeMemory(MemoryID)
  If tchar > 0
    e$ = Trim(Left(e$, tchar))
  Else
    e$ = ""
  EndIf
  ProcedureReturn e$
EndProcedure 

Code: Select all

Procedure.s GetSystemMessage(lngErrorCode.l)
  strMsgBuff.STRING\s = Space(255)
  tchar.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, #Null, lngErrorCode, 0, @strMsgBuff\s, 255, #Null)
  If tchar > 0
    strMsgBuff\s = Trim(Left(strMsgBuff\s, tchar))
  Else
    strMsgBuff\s = ""
  EndIf
  ProcedureReturn strMsgBuff\s
EndProcedure
Give me books, fruit, french wine, fine weather and a little music.
John Keats