PB4 - Win32 - Show-API-Error ()

Share your advanced PureBasic knowledge/code with the community.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

PB4 - Win32 - Show-API-Error ()

Post 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) 

;-------------------------------------------------------------------------------------
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Great tip! This is going in my tools folder right away. Thanks for sharing it.
BERESHEIT
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Nice! I will try it next time if I fall in such a hole without an error message from pb. :D
Tranquil
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post 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
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post 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:
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

All the posted codes work :wink:
BERESHEIT
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

techjunkie wrote:What was wrong with Flype's code?!?
Nothing, but not PB3.94 compatible
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

gnozal wrote:
techjunkie wrote:What was wrong with Flype's code?!?
Nothing, but not PB3.94 compatible
Ahh, okey... :D
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

thanks for the feedback :)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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 ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

Yes, looks nice! Its exactly for what i made the procedure :-)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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:
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post 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
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post 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... ^^
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Post Reply