Page 1 of 1

System error messages

Posted: Thu Sep 12, 2002 1:05 am
by BackupUser
Restored from previous forum. Originally posted by Hi-Toro.
Code updated to 5.20

There's a post on the PB Resources site that does this, but it uses hard-coded strings; this will retrieve the string directly from the OS...

(BTW Can anyone tell me how you post on the PB Resources site? :)

Code: Select all

; ----------------------------------------------
; ShowError -- call on failure of a function...
; ----------------------------------------------

; The Windows GetLastError function applies to
; errors caused in the current program (ie. not
; system-wide).

; ------------------------------------------------------------
; IMPORTANT: Call InitErrors (0) at the start of your program!
; ------------------------------------------------------------

; INITERRORS (0)

; Needed, or GetLastError returns 126 ("Module not found") for some
; reason! Maybe something PB programs try to call while loading?

; You must pass 0 as the default parameter. You can see some
; of the other error messages available though, if you set it to
; another number, then call ShowError
Procedure InitErrors (DefaultError)
	SetLastError_ (DefaultError)
EndProcedure

; SHOWERROR (title$)

; Puts up a dialog box with last error message, only if an error
; has occurred. The title$ parameter appears in the title bar of
; the dialog box.

Procedure.s ShowError (title$)
	error = GetLastError_ ()
	If error
		*MemoryID = AllocateMemory (255)
		FormatMessage_ (#FORMAT_MESSAGE_FROM_SYSTEM, #Null, error, 0, *MemoryID, 255, #Null)
		e$ = PeekS (*MemoryID)
		FreeMemory (*MemoryID)
		MessageRequester (title$, e$, #MB_ICONWARNING) ; ProcedureReturn e$ ; Alternative to MessageRequester!
	EndIf
EndProcedure

; ----------------------------------------------
; D E M O . . .
; ----------------------------------------------

InitErrors (0)

If CreateFile (0, "bollox:\test.txt")
	CloseFile (0)
EndIf

ShowError ("Oh dear...")
--
See ya,
James L Boyd.
http://www.hi-toro.com/
--

Posted: Thu Sep 12, 2002 1:13 am
by BackupUser
Restored from previous forum. Originally posted by Paul.
Originally posted by Hi-Toro

There's a post on the PB Resources site that does this, but it uses hard-coded strings; this will retrieve the string directly from the OS...

(BTW Can anyone tell me how you post on the PB Resources site? :)

Hi James !
To submit files to the Resources Site, just press the big blue button labelled
UPLOAD on the bottom left of the page.
(it's the only button under "Submit Files")

Then follow the instructions :)



Visit the PB Resources Site at http://www.reelmediaproductions.com/pb

Posted: Thu Sep 12, 2002 6:48 am
by BackupUser
Restored from previous forum. Originally posted by Rings.

thx, this is realy shorter than the big Stringtable compiled by me.
And it's language independent too.
thx for sharing code.

Its a long way to the top if you wanna .....CodeGuru

Posted: Thu Sep 12, 2002 7:01 am
by BackupUser
Restored from previous forum. Originally posted by Hi-Toro.

Yeah, just be sure to call InitErrors (0), because it seems that there's an error generated by PB programs (probably trying because it didn't find an icon or something... no big deal :)

Paul -- thanks (great site) and 'Doh!' :)


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--

Posted: Sat Nov 22, 2003 7:45 pm
by Hi-Toro
Just thought I'd update this as I've noticed a few things since this was posted!

· PB no longer generates an error on running, so InitErrors () is no longer required;

· Windows always adds Chr (13) + Chr (10), for reasons known only to M$... looks horrible in debug view ("Error string||"), so I've stripped 'em... :)

Code: Select all


; ShowError () returns a system error message as a string... call any time a function fails!

; If BlahBlah ()
;   EverythingNormal ()
; Else
;   Debug ShowError ()
; EndIf

Procedure.s ShowError ()
  error = GetLastError_ () 
  If error
    length = FormatMessage_ (#FORMAT_MESSAGE_FROM_SYSTEM, #NULL, error, 0, AllocateMemory (0, 255), 255, #NULL)
    If length > 1 ; Some error messages are "" + Chr (13) + Chr (10)... stoopid M$... :(
        e$ = PeekS (MemoryID (), length - 2)
    Else
        e$ = "Unknown error!"
    EndIf
    FreeMemory (0)
    ProcedureReturn e$
  Else
    ProcedureReturn "No error has occurred!"
  EndIf 
EndProcedure 

; D E M O . . .

; Attempting to read an invalid filename...

If ReadFile (0, "ZZZ :\ Stupid f@#&ing filename!")
    MessageRequester ("Yay!", "Opened file!", 0)
    CloseFile (0)
Else
    MessageRequester ("Doh!", ShowError (), 0)
EndIf