Page 1 of 1

PB4 - Win32 - Show-API-Error ()

Posted: Wed Apr 26, 2006 2:56 am
by va!n
Code updated for 5.20+

I think some of you have tried to call any PB or API command and nothing happend. Now this small procedure may hopefully help you to find the problem :) Good Luck! Feedback would be nice!

Code: Select all

;-------------------------------------------------------------------------------------
; P u r e B a s i c   v4       S h o w - A P I - E r r o r ( ) 
; ------------------------------------------------------------------------------------- 
;
; Description:    This function examines, what was wrong with the last command call by
;                 API/PB like if nothing happens again! It returns a suitable error
;                 message string in your national language!
;
; Function:       ShowAPIError (CheckReturnValue)
;
; Parameters:     CheckReturnValue 
; 
;                 Enter here the value, where you have saved the return value of the 
;                 API/PB command you have called and want to check.                  
;
; ReturnValue:    A string including the message text will be returned.
;
; ------------------------------------------------------------------------------------- 
; Program by va!n aka Thorsten Will - 26/April/2006
; ------------------------------------------------------------------------------------- 

Procedure.s ShowAPIError (CheckReturnValue)
  Buffer.s      = Space (4096)
  NumberOfChars = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, CheckReturnValue, 0, Buffer.s, Len(Buffer.s), 0)
  ProcedureReturn Left (Buffer.s, NumberOfChars-2)
EndProcedure

; ------------------------------------------------------------------------------------- 
; S m a l l   e x a m p l e
; ------------------------------------------------------------------------------------- 

result = Sleep_(2)
Debug ShowAPIError(result)              ; No Error 

result = RegOpenKeyEx_(0, 0, 0, 0, 0) 
Debug ShowAPIError(result)              ; Error 
         
result = MessageBox_(0,0,0,0)
Debug ShowAPIError(result)              ; Error (after pressing the MsgBox) 

;-------------------------------------------------------------------------------------

Posted: Wed Apr 26, 2006 7:05 am
by netmaestro
Great tip! This is going in my tools folder right away. Thanks for sharing it.

Posted: Wed Apr 26, 2006 8:07 am
by Flype
That's good, and useful.

Just a little remark : you might 'protect' variables with 'Protected' to avoid possible conflicts with main program.

Code: Select all

Procedure.s ShowAPIError (CheckReturnValue)
  Protected Buffer.s = Space (4096)
  Protected NbChar.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, CheckReturnValue, 0, Buffer, Len(Buffer), 0)
  ProcedureReturn Left(Buffer, NbChar-2)
EndProcedure

Posted: Wed Apr 26, 2006 9:55 am
by Tranquil
Nice! I will try it next time if I fall in such a hole without an error message from pb. :D

Posted: Wed Apr 26, 2006 11:09 am
by ABBKlaus
corrected Flype code :

Code: Select all

Procedure.s ShowAPIError (CheckReturnValue) 
  Protected Buffer.s
  Protected NbChar.l
  Buffer = Space(4096) 
  NbChar = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, CheckReturnValue, 0, Buffer, Len(Buffer), 0) 
  ProcedureReturn Left(Buffer, NbChar-2) 
EndProcedure

Posted: Wed Apr 26, 2006 12:10 pm
by techjunkie
ABBKlaus wrote:corrected Flype code :

Code: Select all

Procedure.s ShowAPIError (CheckReturnValue) 
  Protected Buffer.s
  Protected NbChar.l
  Buffer = Space(4096) 
  NbChar = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, CheckReturnValue, 0, Buffer, Len(Buffer), 0) 
  ProcedureReturn Left(Buffer, NbChar-2) 
EndProcedure
:? :?:

What was wrong with Flype's code?!?

In PB 4.0 B11 documentation it says,
PB Help wrote:Syntax
Protected <variable> [= <expression>], <variable> [= <expression>], ...]
Only a re-format for easy reading or? :wink:

Posted: Wed Apr 26, 2006 12:15 pm
by netmaestro
All the posted codes work :wink:

Posted: Wed Apr 26, 2006 12:36 pm
by gnozal
techjunkie wrote:What was wrong with Flype's code?!?
Nothing, but not PB3.94 compatible

Posted: Wed Apr 26, 2006 12:38 pm
by techjunkie
gnozal wrote:
techjunkie wrote:What was wrong with Flype's code?!?
Nothing, but not PB3.94 compatible
Ahh, okey... :D

Posted: Wed Apr 26, 2006 3:52 pm
by va!n
thanks for the feedback :)

Posted: Wed Apr 26, 2006 4:19 pm
by Flype
from this topic and va!n proc, i had a little idea :
http://www.purebasic.fr/english/viewtopic.php?t=9926

Code: Select all

Procedure.l die(msg.s = #NULL$)
  If msg = #NULL$
    msg = Space(4096)
    FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError_(), 0, msg, Len(msg), 0)
  EndIf
  MessageRequester(GetFilePart(#PB_Compiler_File), msg, #MB_OK|#MB_ICONERROR)
EndProcedure

If ReadFile(0,"no-file.txt") Or Die()
  CloseFile(0)
EndIf
looks good, no ?

Posted: Wed Apr 26, 2006 4:26 pm
by va!n
Yes, looks nice! Its exactly for what i made the procedure :-)

Posted: Wed Apr 26, 2006 7:54 pm
by josku_x

Code: Select all

If ReadFile(0,"no-file.txt") Or Die() 
  CloseFile(0) 
EndIf 
THat reminds me when I do SQL stuff, I always use DoSomeThing() or Die("I am idiot.") :lol:

Posted: Thu Apr 27, 2006 7:33 am
by Shardik
Thank you va!n for your tip but I thought the use of the FormatMessage-Call is already commonly known. I myself have known and used this method already several years. Some postings from 2002:
http://www.purebasic.fr/english/viewtopic.php?t=3809
http://www.purebasic.fr/english/viewtopic.php?t=16792

Posted: Thu Apr 27, 2006 1:59 pm
by va!n
ohhh... i personally didnt know that it was already published on this forum, years ago... maybe, because i am trying now more and more to get in touch with the API instead only PB commands...

I found the tip on the web of a VB site and tried to get it work... ^^