Open a URL in VB6

I was modifying a VB6 application to include a button that launches a browser to navigate to a URL. I wanted to use the default browser and open the link in an existing window, if one existed. This was accomplished easily with the ShellExecute API command.

First, you need your API function declaration in a code module:

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpszOp As String, _
     ByVal lpszFile As String, ByVal lpszParams As String, _
     ByVal LpszDir As String, ByVal FsShowCmd As Long) _
    As Long

Then you can use the API to open your URL, like so:

ShellExecute 0, vbNullString, "http://adamprescott.net", vbNullString, vbNullString, vbNormalFocus
Advertisement

C# Interop Tutorial

Creating a .net library for use with a VB6 application can be a tricky thing, but it’s really not that difficult when you follow the correct steps. This article will walk you through the tasks necessary to create an interop class in c# and invoke its methods from a VB6 application.

Create your interface

Just make an interface. Note the ComVisible attribute on the class and the DispId attribute on the methods.

namespace Sample.Vb6Interop
{
    [ComVisible(true)]
    public interface IMyInterop
    {
        [DispId(1)]
        string HelloWorld();
    }
}

Implement the interface in a class

Now implement your interface. Once again, I need to use the ComVisible attribute on the class.

namespace Sample.Vb6Interop
{
    [ComVisible(true)]
    public class MyInterop : IMyInterop
    {
        public string HelloWorld()
        {
            return "Hello, world!";
        }
    }
}

Register the assembly

You’ll use RegAsm to register your DLL. The key to this step is to use the /tlb option to create and register a COM type library. I also use the /codebase option since I’m not installing my DLL in the GAC.

regasm /tlb /codebase Sample.Vb6Interop.dll

Instantiate and use

Now the hard part’s done. In your VB6 project, you can instantiate and use your new class. I use late-binding in my project, but you could also add a reference to your project and use early-binding. (Early-binding is necessary for event handling.)

Private Sub Command1_Click()
    
    Dim objMyInterop As Object
    
    Set objMyInterop = CreateObject("Sample.Vb6Interop.MyInterop")
    
    Call MsgBox(objMyInterop.HelloWorld)
    
End Sub

This will give you a working example, but it only scratches the surface. Things get more complicated quickly when you start talking about events and callbacks.

Parse US Street Addresses with Regular Expression in VB6

I have a confession to make: this article was actually implemented in VB6 originally. The meat of it is the same, but here’s the VB6 edition in case it’s of interest to anybody.

Enjoy!

Private Sub Form_Load()
    
    Call ClearControls
    
End Sub

Private Sub ClearControls()
    uxHouseNumber = ""
    uxStreetPrefix = ""
    uxStreetName = ""
    uxStreetType = ""
    uxStreetSuffix = ""
    uxApt = ""
    uxAdditionalInfo = ""
End Sub

'---------------------------------------------------------------------------------------
' Procedure : ConstructRegex
' Purpose   : Returns a regular expression pattern for parsing US street addresses
'---------------------------------------------------------------------------------------
'
Private Function ConstructRegex() As String
    
'    ConstructRegex = "^" & _                           -> begin string
'        "(\d+)" & _                                    -> 1 or more digits
'        "(\s+(?:" & GetStreetPrefixes() & "))?" & _    -> whitespace + valid prefix (optional)
'        "(\s+.*?)" & _                                 -> whitespace + one or characters
'        "(?:" & _                                      -> group (optional) {
'            "(\s+(?:" & GetStreetTypes() & "))" & _    ->   whitespace + valid street type
'            "(\s+(?:" & GetStreetSuffixes() & "))?" & _->   whitespace + valid street suffix (optional)
'            "(\s+.*)?" & _                             ->   whitespace + anything else (optional)
'        ")?" & _                                       -> }
'        "$"                                            -> end string

    ConstructRegex = "^" & _
        "(\d+)" & _
        "(\s+(?:" & GetStreetPrefixes() & "))?" & _
        "(\s+.*?)" & _
        "(?:" & _
            "(\s+(?:" & GetStreetTypes() & "))" & _
            "(\s+(?:" & GetStreetSuffixes() & "))?" & _
            "(\s+.*)?" & _
        ")?" & _
        "$"


End Function

'---------------------------------------------------------------------------------------
' Procedure : GetStreetPrefixes
' Purpose   : Returns a pipe-delimited list of valid street prefixes
'---------------------------------------------------------------------------------------
'
Private Function GetStreetPrefixes() As String
    
    GetStreetPrefixes = "TE|NW|HW|RD|E|MA|EI|NO|AU|SE|GR|OL|W|MM|OM|SW|ME|HA|JO|OV|S|OH|NE|K|N"
    
End Function

'---------------------------------------------------------------------------------------
' Procedure : GetStreetTypes
' Purpose   : Returns a pipe-delimited list of valid street types
'---------------------------------------------------------------------------------------
'
Private Function GetStreetTypes() As String
    
    GetStreetTypes = "TE|STCT|DR|SPGS|PARK|GRV|CRK|XING|BR|PINE|CTS|TRL|VI|RD|PIKE|MA|LO|TER|UN|CIR|WALK|CO|RUN|FRD|LDG|ML|AVE|NO|PA|SQ|BLVD|VLGS|VLY|GR|LN|HOUSE|VLG|OL|STA|CH|ROW|EXT|JC|BLDG|FLD|CT|HTS|MOTEL|PKWY|COOP|ACRES|ESTS|SCH|HL|CORD|ST|CLB|FLDS|PT|STPL|MDWS|APTS|ME|LOOP|SMT|RDG|UNIV|PLZ|MDW|EXPY|WALL|TR|FLS|HBR|TRFY|BCH|CRST|CI|PKY|OV|RNCH|CV|DIV|WA|S|WAY|I|CTR|VIS|PL|ANX|BL|ST TER|DM|STHY|RR|MNR"
    
End Function

'---------------------------------------------------------------------------------------
' Procedure : GetStreetSuffixes
' Purpose   : Returns a pipe-delimited list of valid street suffixes
'---------------------------------------------------------------------------------------
'
Private Function GetStreetSuffixes() As String
    
    GetStreetSuffixes = "NW|E|SE|W|SW|S|NE|N"
    
End Function

'---------------------------------------------------------------------------------------
' Procedure : uxAddress_Change
' Purpose   : Parses user input and displays components to user
'---------------------------------------------------------------------------------------
'
Private Sub uxAddress_Change()
    
    Dim strInput As String
    Dim re As RegExp
    Dim mc As MatchCollection
    Dim ma As Match
    
    Call ClearControls
    
    strInput = UCase$(uxAddress.Text)
    
    Set re = New RegExp
    re.Pattern = ConstructRegex()
    re.Global = True
    
    If re.Test(strInput) Then
        Set mc = re.Execute(strInput)
        Set ma = mc(0)
        
        uxHouseNumber = Trim$(ma.SubMatches(0))
        uxStreetPrefix = Trim$(ma.SubMatches(1))
        uxStreetName = Trim$(ma.SubMatches(2))
        uxStreetType = Trim$(ma.SubMatches(3))
        uxStreetSuffix = Trim$(ma.SubMatches(4))
        uxApt = Trim$(ma.SubMatches(5))
        
        Set ma = Nothing
        Set mc = Nothing
    Else
        uxStreetName = strInput
    End If
    
    Set re = Nothing
    
End Sub
%d bloggers like this: