Creating COM Components in VB

FDN » COM/DCOM » Creating COM Components in VB

Creating COM Components in VB

Visual Basic 6 makes it easy to create COM DLLs that can be used from ASP, VBScript, other VB projects, and any COM-aware language.

Step 1: Create the Project

  1. In VB6, select ActiveX DLL as the project type.
  2. Set the project name to FlameNetUtils (this becomes part of the ProgID).
  3. Rename Class1 to StringHelper. The ProgID will be FlameNetUtils.StringHelper.

Step 2: Write the Class

' Class: StringHelper
' ProgID: FlameNetUtils.StringHelper
Option Explicit

Public Function Slugify(ByVal strInput As String) As String
    Dim strResult As String
    Dim i As Long
    strInput = LCase(Trim(strInput))

    For i = 1 To Len(strInput)
        Dim ch As String
        ch = Mid(strInput, i, 1)
        If ch >= "a" And ch <= "z" Then
            strResult = strResult & ch
        ElseIf ch >= "0" And ch <= "9" Then
            strResult = strResult & ch
        ElseIf ch = " " Or ch = "_" Then
            If Right(strResult, 1) <> "-" Then
                strResult = strResult & "-"
            End If
        End If
    Next i

    ' Trim trailing dash
    If Right(strResult, 1) = "-" Then
        strResult = Left(strResult, Len(strResult) - 1)
    End If

    Slugify = strResult
End Function

Public Function TruncateHTML(ByVal strHTML As String, ByVal lngMaxChars As Long) As String
    ' Strip HTML tags, truncate to lngMaxChars, append ellipsis
    Dim strPlain As String
    Dim blnInTag As Boolean
    Dim i As Long

    For i = 1 To Len(strHTML)
        Dim c As String
        c = Mid(strHTML, i, 1)
        If c = "<" Then
            blnInTag = True
        ElseIf c = ">" Then
            blnInTag = False
        ElseIf Not blnInTag Then
            strPlain = strPlain & c
        End If
    Next i

    If Len(strPlain) > lngMaxChars Then
        TruncateHTML = Left(strPlain, lngMaxChars) & "..."
    Else
        TruncateHTML = strPlain
    End If
End Function

Step 3: Compile and Register

  1. Go to FileMake FlameNetUtils.dll.
  2. The DLL is automatically registered on your machine.
  3. To register on another machine: regsvr32 FlameNetUtils.dll.

Step 4: Use from ASP

<%
Dim objHelper
Set objHelper = Server.CreateObject("FlameNetUtils.StringHelper")

Dim strSlug
strSlug = objHelper.Slugify("Hello World! This is a Test")
' Returns: "hello-world-this-is-a-test"

Response.Write "Slug: " & strSlug
Set objHelper = Nothing
%>

Binary Compatibility

To prevent breaking existing clients when you update the DLL, set Binary Compatibility in Project Properties → Component tab. VB will keep the same CLSIDs and interface IDs between builds.

« Developer Network ‹ COM Fundamentals DCOM Configuration ›