Call OCX COM sample

dilettante (MIS)
Start with a small script:

Jabber.vbs

CODE

Option Explicit

Function Jabber()
    Const ForReading = 1
    Dim FSO
    Dim strIn
    Dim tsIn
    Dim strOut
    Dim tsOut
    Dim strLine

    Set FSO = CreateObject("Scripting.FileSystemObject")
    strIn = InputBox("File to Jabber")
    If StrIn = "" Then
    Jabber = True
        Exit Function
    End If
    strOut = InputBox("New jabbered file")
    If strOut = "" Then
        Jabber = True
        Exit Function
    End If
    Set tsIn = FSO.OpenTextFile(strIn, ForReading)
    Set tsOut = FSO.CreateTextFile(strOut, True)
    Do Until tsIn.AtEndOfStream
        strLine = tsIn.ReadLine
        strLine = Replace(strLine, "a", vbFormFeed)
        strLine = Replace(strLine, "e", "a")
        strLine = Replace(strLine, "i", "e")
        strLine = Replace(strLine, "o", "i")
        strLine = Replace(strLine, "u", "o")
        strLine = Replace(strLine, vbFormFeed, "u")
        tsOut.WriteLine strLine
    Loop
    tsOut.Close
    Set tsOut = Nothing
    tsIn.Close
    Set tsIn = Nothing
    Set FSO = Nothing
    Jabber = False 'No cancels.
End Function

WScript.Echo "Start!"
If Jabber() Then
    WScript.Echo "Canceled!"
Else
    WScript.Echo "Done!"
End If

Then I started up VB5CCE and created an ActiveX control project.  I named the project Jabber and the UserControl Compiled.

Next I copied in the VBScript code for the function Jabber() into the UserControl’s code window.  Because VB5 did not have the Replace() function yet, I faced a decision.

One way would be to write my own Replace() using the string functions already in VB5.  The other is to write a function in the calling script that invokes VBScript’s Replace, and pass the calling script to the VB5 version of Jabber() as an Object, then invoke the function from the script as a method.

I chose the latter, and I called my Replace() wrapper function R().

Here is the VB5CCE code:

Compiled.ctl

CODE

Option Explicit

Public Function Jabber(ByVal Caller As Object)
    Const ForReading = 1
    Dim FSO
    Dim strIn
    Dim tsIn
    Dim strOut
    Dim tsOut
    Dim strLine

    Set FSO = CreateObject("Scripting.FileSystemObject")
    strIn = InputBox("File to Jabber")
    If strIn = "" Then
        Jabber = True
        Exit Function
    End If
    strOut = InputBox("New jabbered file")
    If strOut = "" Then
        Jabber = True
        Exit Function
    End If
    Set tsIn = FSO.OpenTextFile(strIn, ForReading)
    Set tsOut = FSO.CreateTextFile(strOut, True)
    Do Until tsIn.AtEndOfStream
        strLine = tsIn.ReadLine
        strLine = Caller.R(strLine, "a", vbFormFeed)
        strLine = Caller.R(strLine, "e", "a")
        strLine = Caller.R(strLine, "i", "e")
        strLine = Caller.R(strLine, "o", "i")
        strLine = Caller.R(strLine, "u", "o")
        strLine = Caller.R(strLine, vbFormFeed, "u")
        tsOut.WriteLine strLine
    Loop
    tsOut.Close
    Set tsOut = Nothing
    tsIn.Close
    Set tsIn = Nothing
    Set FSO = Nothing
    Jabber = False 'No cancels.
End Function

Not much has changed here from the original VBScript version.  I saved the project and compiled it, which registers the new Jabber.ocx on that machine in passing.

Then I slightly rewrote the calling script:

Jabber Compiled.vbs

CODE

Option Explicit

Dim Jabber

Function R(ByVal Expression, ByVal Find, ByVal ReplaceWith)
    R = Replace(Expression, Find, ReplaceWith)
End Function

WScript.Echo "Start!"

Set Jabber = CreateObject("Jabber.Compiled")
If Jabber.Jabber(Me) Then
    WScript.Echo "Canceled!"
Else
    WScript.Echo "Done!"
End If
Set Jabber = Nothing

The only deployment issue is making sure that the VB5 runtime is installed on target machines, and that Jabber.ocx gets copied and registered, such as by running:

regsrv32 {path}Jabber.ocx

This can be done from a command prompt or Start|Run dialog.  One could also create an OCX installer package, etc.

I think this is as close as you can get “for free” to compiling VBScript.  One might replace the calling script by using some free compiler for a language that supports ActiveX objects.  Then you’d have a 100% solution.

You can also easily use things like the Common Dialog control in VB5CCE to provide a nicer file dialog than you get by using Inputbox().  Adding a reference to the Microsoft Scripting Runtime lets you use early binding and defines the FSO constants, and for that matter you can use typed variables for better performance.

Leave a Reply

Your email address will not be published. Required fields are marked *