Visual Basic 6

API-Fehler richtig auswerten

Fehlerbehandlung nach einem API-Aufruf

Die meisten API-Funktion liefern nach getaner Arbeit einen Status-Code zurück, der besagt, ob die Ausführung korrekt verlaufen ist oder nicht. Ist ein Fehler aufgetreten, so kann man den richtigen Fehlercode NUR über die LastDLLError-Eigenschaft des Err-Objekts abfragen! Wie in einem MSDN-Artikel beschrieben ist, sollte man als VB-Programmierer niemals auf den Gedanken kommen von der API-Funktion GetLastError Gebrauch zu machen, denn auch die VB-Runtime macht von der API regen Gebrauch und es ist nicht gesagt, dass der letzte Fehler der des eigenen API-Aufrufs war.

Wenn denn dann ein API-Fehler aufgetreten ist und die korrekte Fehlernummer über LastDLLError vorliegt, so wäre eine aussagekräftige Fehlerbeschreibung nicht schlecht. Da API-Fehler nichts mit VB-Fehlern zu tun haben, wird die Description-Eigenschaft des Err-Objekt leider leer sein. Zuständig für die Lieferung des Klartextes ist wiederum eine API-Funktion: FormatMessage.

Beispiel:

Deklarationen

Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000&
Private Const FORMAT_MESSAGE_IGNORE_INSERTS As Long = &H200&
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK As Long = &HFF&
Private Const LANG_USER_DEFAULT As Long = &H400&

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" ( _
   ByVal dwFlags As Long, _
   ByRef lpSource As Any, _
   ByVal dwMessageId As Long, _
   ByVal dwLanguageId As Long, _
   ByVal lpBuffer As String, _
   ByVal nSize As Long, _
   ByRef Arguments As Long) As Long

Prozedur

Public Function GetAPIErrorDescription( _
   ByVal lngLastDllError As Long) As String

   Dim strBuffer As String
   Dim lngLen As Long
   Dim lngFlags As Long

   strBuffer = Space$(1024)
   lngFlags = FORMAT_MESSAGE_FROM_SYSTEM Or _
      FORMAT_MESSAGE_MAX_WIDTH_MASK Or _
      FORMAT_MESSAGE_IGNORE_INSERTS

   lngLen = FormatMessage(lngFlags, ByVal 0&, lngLastDllError, _
      LANG_USER_DEFAULT, strBuffer, Len(strBuffer), 0)

   If lngLen > 0 Then
      GetAPIErrorDescription = Left$(strBuffer, lngLen)
   End If

End Function


Aufruf

Public Function Test()

   Dim lngRetVal As Long

   lngRetVal = <Aufruf der gewünschten API-Funktion>

   If lngRetVal <> 0 And Err.LastDllError <> 0 Then
      Err.Raise vbObjectError + Err.LastDllError, , _
         GetAPIErrorDescription(Err.LastDllError)
   End If

End Function
kick it on dotnet-kicks.de AddThis 0 wikio-Stimme(n) Trackback-Url...

224 Kommentare bislang...

  • snxxdzti viagra 5930 cialis eRFkX cialis 1327 cialis RZtjAh viagra kaufen 8771 compra viagra 6605
    224
    viagra : Samstag, 4. Februar 2012 03:15
  • tlystf viagra rVymR cialis =-] viagra :-O cialis alternatives 7629
    223
    viagra : Mittwoch, 1. Februar 2012 19:23
  • htoyvdmt payday loan XepSLp payday loan 3015 Payday loans online EwrVeO
    222
    payday loan : Sonntag, 29. Januar 2012 14:10
  • <strong><a href="http://www.authentic-eaglesjersey.com/" title="Authentic Eagles Jerseys">Authentic Eagles Jerseys</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/" title="Cheap Eagles Jerseys">Cheap Eagles Jerseys</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/shop-by-players-michael-vick-jerseys-c-1_13.html" title="Authentic Vick Jersey">Authentic Vick Jersey</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/shop-by-players-lesean-mccoy-jerseys-c-1_12.html" title="LeSean McCoy Jerseys">LeSean McCoy Jerseys</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/shop-by-players-michael-vick-jerseys-c-1_21.html" title="Authentic Young Jersey">Authentic Young Jersey</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/customized-jerseys-c-32.html" title="Custom Eagles Jerseys">Custom Eagles Jerseys</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/authentic-men-jerseys-c-22.html" title="Official Philadelphia Eagles Jersey">Official Philadelphia Eagles Jersey</a></strong>
    <strong><a href="http://www.authentic-eaglesjersey.com/authentic-throwback-jerseys-c-30.html" title="Authentic Eagles Throwback Jerseys">Authentic Eagles Throwback Jerseys</a></strong>
    <strong><a href="http://www.denverbroncosjerseysstore.com/shop-by-players-tim-tebow-jersey-c-1_29.html" title="Tim Tebow Jersey">Tim Tebow Jersey</a></strong>
    <strong><a href="http://www.denverbroncosjerseysstore.com/shop-by-players-von-miller-jersey-c-1_3.html" title="Von Miller Jersey">Von Miller Jersey</a></strong>
    <strong><a href="http://www.denverbroncosjerseysstore.com/" title="Broncos Jersey">Broncos Jersey</a></strong>
    <strong><a href="http://www.denverbroncosjerseysstore.com/" title="Denver Broncos Jersey">Denver Broncos Jersey</a></strong>
    <strong><a href="http://www.denverbroncosjerseysstore.com/authentic-jerseys-c-30.html" title="Denver Broncos Jerseys">Denver Broncos Jerseys</a></strong>
    <strong><a href="http://www.justinbiebershoes-sales.com/">Justin Bieber Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoes-sales.com/">Supra Tk Society</a></strong>
    <strong><a href="http://www.justinbiebershoes-sales.com/supra-high-tops-c-9.html">Supra High Tops Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoes-sales.com/justin-bieber-supra-c-2.html">Justin Bieber Supra Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoes-sales.com/men-supra-vaider-c-7.html">Men Supra Vaider Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoessupras.com">Justin Bieber Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoessupras.com">Supra Skytop Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoessupras.com/supra-shoes-2011-c-1.html">2011 Supra Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoessupras.com/new-supra-shoes-c-24.html">New Supra Shoes</a></strong>
    <strong><a href="http://www.justinbiebershoessupras.com/supra-tk-society-c-15.html">Supra TK Society Purple</a></strong>
    <strong>&l
    221
    Authentic Eagles Jerseys Cheap Eagles Jerseys Authentic Eagles Jerseys : Samstag, 28. Januar 2012 06:11
  • I will tell you right now that how to grow your penis if you want to be the best of the best then you must make sure that you are working extremely hard at what is the most I'm just going to be extremely honest here.
    220
    grow your penis : Samstag, 14. Januar 2012 23:56

Dein Kommentar hierzu...


Kommentar-Feed für diesen Beitrag
Gravatare werden unterstützt .:. eMail-Adressen werden nicht veröffentlicht
 

RSS-Feed

Die URL des Standard-Newsfeed von zerbit.de lautet:

http://www.zerbit.de/rssfeed.aspx

Login


 

 

Statistik



kürzlich kommentiert

Artikel 21

  • Datum: 11.04.2005
    Kategorie: Visual Basic 6
    Zugriffe: 5.475
    Kommentare: 224
    Trackbacks: 0

Letzte Beiträge

Kategorien

Buttons & More

Blog-Roll

Banner Piraten-Partei