Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB/CC: Formatted (text) System Error Messages

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PB/CC: Formatted (text) System Error Messages

    Converts a numeric system error number (ie, from GetLastError()), into a string description.

    Code:
    ' FormatMessage() API example by Lance C. Edmonds, April 2001
    ' PB/CC code.  Change PRINT to MSGBOX for PB/DLL (and delete WAITKEY$).
     
    #INCLUDE "win32api.inc"
    FUNCTION PBMAIN
        DIM ErrText AS ASCIIZ * 1024, Result&, ErNum&
     
        ErrNum& = %RPC_S_SERVER_UNAVAILABLE
        Result& = FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM, _
                    BYVAL 0, _
                    ErrNum&, _
                    MAKLNG(%LANG_NEUTRAL, %SUBLANG_DEFAULT), _
                    ErrText, _
                    SIZEOF(ErrText), _
                    BYVAL 0)
        REPLACE CHR$(13,10) WITH "" IN ErrText
        PRINT $DQ ErrText $DQ
        WAITKEY$
    END FUNCTION

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

  • #2
    Lance,

    There is a similar sample on the PB/CC disk called WINERR.BAS. It does not use the "default language" as yours does, however. You may want to update the WINERR.BAS example for the next release of PB/CC.

    ------------------
    Home of the BASIC Gurus
    www.basicguru.com

    [This message has been edited by Dave Navarro (edited April 27, 2001).]
    Home of the BASIC Gurus
    www.basicguru.com

    Comment


    • #3
      The language info needs to be pulled together by MAKELANGID, for which MAKLNG is,
      unfortunately, not a replacement. While we're at it, let's lose the static buffer.
      Code:
      ' FormatMessage() API example by Tom Hanlin, August 2001
        
      #COMPILE EXE
      #INCLUDE "win32api.inc"
       
      FUNCTION MAKELANGID (BYVAL p AS WORD, BYVAL s AS WORD) AS DWORD
      
          LOCAL dResult AS DWORD
      
          dResult = s
          SHIFT LEFT dResult, 10
          dResult = dResult OR p
      
          FUNCTION = dResult
      
      END FUNCTION
       
      FUNCTION WinErrorText (BYVAL dError AS DWORD) AS STRING
      
          LOCAL pBuffer   AS ASCIIZ PTR
          LOCAL ncbBuffer AS DWORD
      
          ncbBuffer = FormatMessage( _
                          %FORMAT_MESSAGE_ALLOCATE_BUFFER _
                       OR %FORMAT_MESSAGE_FROM_SYSTEM _
                       OR %FORMAT_MESSAGE_IGNORE_INSERTS, _
                          BYVAL %NULL, _
                          dError, _
                          BYVAL MAKELANGID(%LANG_NEUTRAL, %SUBLANG_DEFAULT), _
                          BYVAL VARPTR(pBuffer), _
                          0, _
                          BYVAL %NULL)
      
          IF ncbBuffer THEN
              FUNCTION = PEEK$(pBuffer, ncbBuffer)
              LocalFree pBuffer
          ELSE
              FUNCTION = "Unknown error, code = &H" + HEX$(dError, 8)
          END IF
      
      END FUNCTION
       
      FUNCTION PBMAIN() AS LONG
      #IF %DEF(%PBCC32)
          STDOUT WinErrorText(%ERROR_OUTOFMEMORY)
          WAITKEY$
      #ELSE
          MSGBOX WinErrorText(%ERROR_OUTOFMEMORY)
      #ENDIF
      END FUNCTION
      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment

      Working...
      X