Drydo's Blog

Teenager of the Internet

This blog hosted by:
http://blogs.vbcity.com
  Home :: Syndication  :: Login   Community Forums   :: vbCity.com   :: DevCity.NET  

Just found this chunk of code knocking around in a code library and thought I would share it. Basically, its the method of retreiving error details when making a call to Win32 API function. I must admit I found it extremely useful on some of the more unusual P/Invoke conversion projects I've worked on... 

Code Copy
Public Class cGetLastDLLError
    Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
        (ByVal dwFlags As Integer, ByRef lpSource As Object, ByVal dwMessageId As Integer, _
            ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByRef Arguments As Integer) As Integer

    Public Sub GetLastMessage()
        Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000
        Dim sError As String
        Dim lErrNum As Integer
        Dim lErrMsg As Long

        sError = New String(CChar(" "), 500)
        lErrNum = Err.LastDllError
        lErrMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&, lErrNum, 0, sError, Len(sError), 0)
        MsgBox(Trim(sError))
    End Sub
End
Class

Have fun - M

posted on Wednesday, August 31, 2005 10:08 AM