QuickStart: Getting Started (VB6)

Limitations when using vbWatchdog with VB6

vbWatchdog requires that VB6 projects be compiled to P-Code format rather than Native-Code.

To do this, with your VB6 project open, go to the 'Project' menu and choose '[ProjectName] Properties...' (last menu option), and on the 'Compile' tab select the 'Compile to P-Code' option and click 'OK'.

Additionally, there are a few limitations when using vbWatchdog inside VB6 projects:
  • The VariablesInspector is only available when debugging (not available in compiled projects).
  • vbWatchdog can only list the sourcecode line (eg. with the ErrEx.SourceLineCode property) when debugging (not available in compiled projects).
  • ErrEx.SourceProjectIsCompiled property is not supported in this environment (will always return False).
  • ErrEx.SourceProjectIsSaved property is not supported in this environment (will always return False).
  • ErrEx.SourceVBEProject property is not supported in this environment (will always return Nothing).
  • Private subroutines in standard modules will be identified as '{PROC}#ID' where ID represents the ordinal number of the subroutine in the module.  The included LineNumberer IDE addin lists the subroutine ordinal numbers so that you can match up the ordinal number to the correct procedure easily.

Adding vbWatchdog to your project

To add vbWatchdog to your project, you need to add the vbWatchdog class modules into your application.   You do this by using the vbWatchdog developer Addin that is provided.  If you haven't yet installed the vbWatchdog developer AddIn, then you can get it in the Downloads section.  Registered users: use the download link in your receipt e-mail!

To use the addin, simply locate the "Addins" menu in the Visual Basic environment, choose "vbWatchdog", and then select "Add vbWatchdog to this project".  Now you should find that you've got four ErrEx class modules added to your project.

Using vbWatchdog in VB6

To use vbWatchdog in a VB6 project requires that you to create a class module that will expose a member that gets called when an exception is detected.

You then simply set the ErrEx.EvalObject property to an instance of that class, and call the ErrEx.Enable method with the name of the member that you want to be called on error.

For example:

Sub Main()
    Set ErrEx.EvalObject = New MyGlobalErrorHandler
    ErrEx.Enable "OnError"
    SimulateAnError
End Sub

Public Sub SimulateAnError()
    MsgBox 1 / 0     ' enforce a division by zero error for testing
End Sub

In a class called MyGlobalErrorHandler:

Public Sub OnError()
    LogErrorToFile
End Sub

Public Sub LogErrorToFile()
    
    Dim FilePath As String
    Dim FileNum As Long
    Dim LogLine As String
    
    On Error Resume Next ' If this procedure fails, something fairly major has gone wrong.
    
    ' We will write to a simple text file called SampleErrorLog in our MyDocuments folder
    FilePath = CreateObject("WScript.Shell").SpecialFolders("MYDOCUMENTS") & "\SampleErrorLog.txt"

    ' If you're new to vbWatchdog, don't worry about what follows just yet.
    ' It is basically iterating through the call stack to get more details about the error.

    FileNum = FreeFile
    Open FilePath For Append Access Write Lock Write As FileNum

        Print #FileNum, Now() & " - " & CStr(ErrEx.Number) & " - " & CStr(ErrEx.Description)
            
        'We will separate the call stack onto separate lines in the log
	With ErrEx.CallStack
            Do
                Print #FileNum, "       --> " & .ProjectName & "." & _
                                .ModuleName & "." & _
                                .ProcedureName & ", " & _
                                "#" & .LineNumber & ", " & _
                                .LineCode & vbCrLf
            Loop While .NextLevel
	End With

    Close FileNum

End Sub

NEXT:   QuickStart: The Global Error Trap