CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Err.LastDLLError equivalent?

    How can I get similar functionality to Err.LastDllError (from VB5/6) in VB.Net? I presume GetLastError api call is still suspect because of API functions that may trample on the error code being called internally by the programming language?
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  2. #2
    Join Date
    Oct 2002
    Location
    Houston
    Posts
    6
    system.Runtime.InteropServices.Marshal.GetLastWin32Error()

    -Mike Huguet
    Software Architects, Inc.
    Mike Huguet
    Senior Consultant
    Software Architects, Inc.
    MCSD, MCDBA, MCSA

  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    **Resolved** Err.LastDllError equivalent

    If you declare a DLL import with the SetLastError attribute then you can get the equivalent of VB classic's :
    Code:
    Err.LastDllError
    with
    Code:
    System.Runtime.InteropServices.Marshal.GetLastWin32Error
    ..and like in VB classic you need to call the FormatMessage API call to get a description from this error:

    Code:
        Public Enum Format_Message_Flags
            FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
            FORMAT_MESSAGE_IGNORE_INSERTS = &H200
            FORMAT_MESSAGE_FROM_STRING = &H400
            FORMAT_MESSAGE_FROM_HMODULE = &H800
            FORMAT_MESSAGE_FROM_SYSTEM = &H1000
            FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
        End Enum
    
        Private Const MAX_MESSAGE_LENGTH = 512
    
        <DllImport("kernel32.dll", EntryPoint:="FormatMessageA", _
         CharSet:=CharSet.Ansi, _
         ExactSpelling:=True, _
         CallingConvention:=CallingConvention.StdCall)> _
        Private Function FormatMessage(ByVal dwFlags As Format_Message_Flags, ByVal lpSource As Int32, ByVal dwMessageId As Int32, ByVal dwLanguageId As Int32, ByVal lpBuffer As String, ByVal nSize As Int32, ByVal Arguments As Int32) As Int32
    
        End Function
    
       Public Function GetAPIErrorMessageDescription(ByVal ApiErrNumber As Int32) As String
    
            Dim sError As New StringBuilder(MAX_MESSAGE_LENGTH)
            Dim lErrorMessageLength As Int32
    
            lErrorMessageLength = FormatMessage(Format_Message_Flags.FORMAT_MESSAGE_FROM_SYSTEM, 0, ApiErrNumber, 0, sError, sError.Capacity, &H0)
            If lErrorMessageLength > 0 Then
                GetAPIErrorMessageDescription = sError.ToString
            End If
    
    
        End Function
    Thanks,
    Duncan
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured