oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Creating a Windows DLL with Visual Basic
Pages: 1, 2, 3

Creating the Windows DLL

So, after examining an ActiveX DLL's export table, intercepting Visual Basic's call to the compiler, intercepting Visual Basic's call to the linker, and comparing the arguments passed to the linker with those required by a C/C++ compiler to generate a Windows DLL, we've finally identified why we aren't able to successfully create a Windows DLL with Visual Basic. And fortunately, we can work around that restriction. We should be able to create a standard Windows DLL if we do the following:

  1. Create a .def file for our project. We can specify our exported functions in the .def file in several ways, but it's best to keep it simple:

    NAME MathLib
    LIBRARY MathMod
    DESCRIPTION "Add-on Library of Mathematical Routines"
    EXPORTS DllMain @1
            Increment @2
            Decrement @3
            Square @4

    The NAME statement defines the name of the DLL. The LIBRARY statement must either precede the list of exported functions or appear on the same line as the first function. The .def file should also list the ordinal position of each exported function preceded by an @ symbol.

  2. Decide how we want to intercept the call to the linker. Two major techniques are available to do this:

    • Patching the Import Address Table (IAT), which requires that we build a Visual Basic add-in that modifies the IAT in order to intercept particular calls by Visual Basic to the Win32 API. Although it's certainly the most elegant method, its complexity makes it a worthy subject for a separate article.

    • Building a proxy linker that intercepts the call to the real linker, modifies the command-line arguments to be passed to the linker, and then calls the linker with the correct command-line arguments. This is the approach we used to discover what arguments Visual Basic was passing to the compiler and linker, and it's the approach we'll adopt to create a Windows DLL.

    In building our proxy linker, we want a sufficiently flexible design so that we can generate other kinds of files, if need be.

  3. Modify the arguments to the linker to add the /DEF switch along with the path and filename of our .def file. To do this, you must create a Visual Basic Standard EXE project, add a reference to the Microsoft Scripting Runtime Library, remove the form from the project, and add a code module. The source code for the proxy linker is as follows:

    Option Explicit
    Public Sub Main()
       Dim SpecialLink As Boolean, fCPL As Boolean, fResource As Boolean
       Dim intPos As Integer
       Dim strCmd As String
       Dim strPath As String
       Dim strFileContents As String
       Dim strDefFile As String, strResFile As String
       Dim oFS As New Scripting.FileSystemObject
       Dim fld As Folder
       Dim fil As File
       Dim ts As TextStream, tsDef As TextStream
       strCmd = Command
       Set ts = oFS.CreateTextFile(App.Path & "\lnklog.txt")
       ts.WriteLine "Beginning execution at " & Date & " " & Time()
       ts.WriteBlankLines 1
       ts.WriteLine "Command line arguments to LINK call:"
       ts.WriteBlankLines 1
       ts.WriteLine "   " & strCmd
       ts.WriteBlankLines 2
       ' Determine if .DEF file exists
       ' Extract path from first .obj argument
       intPos = InStr(1, strCmd, ".OBJ", vbTextCompare)
       strPath = Mid(strCmd, 2, intPos + 2)
       intPos = InStrRev(strPath, "\")
       strPath = Left(strPath, intPos - 1)
       ' Open folder
       Set fld = oFS.GetFolder(strPath)
       ' Get files in folder
       For Each fil In fld.Files
          If UCase(oFS.GetExtensionName(fil)) = "DEF" Then
             strDefFile = fil
             SpecialLink = True
          End If
          If UCase(oFS.GetExtensionName(fil)) = "RES" Then
             strResFile = fil
             fResource = True
          End If
          If SpecialLink And fResource Then Exit For
       ' Change command line arguments if flag set
       If SpecialLink Then
          ' Determine contents of .DEF file
          Set tsDef = oFS.OpenTextFile(strDefFile)
          strFileContents = tsDef.ReadAll
          If InStr(1, strFileContents, "CplApplet", vbTextCompare) > 0 Then
             fCPL = True
          End If
          ' Add module definition before /DLL switch
          intPos = InStr(1, strCmd, "/DLL", vbTextCompare)
          If intPos > 0 Then
             strCmd = Left(strCmd, intPos - 1) & _
                   " /DEF:" & Chr(34) & strDefFile & Chr(34) & " " & _
                   Mid(strCmd, intPos)
          End If
          ' Include .RES file if one exists
          If fResource Then
             intPos = InStr(1, strCmd, "/ENTRY", vbTextCompare)
             strCmd = Left(strCmd, intPos - 1) & Chr(34) & strResFile & _
                      Chr(34) & " " & Mid(strCmd, intPos)
          End If
          ' If Control Panel applet, change "DLL" extension to "CPL"
          If fCPL Then
             strCmd = Replace(strCmd, ".dll", ".cpl", 1, , vbTextCompare)
          End If
          ' Write linker options to output file
          ts.WriteLine "Command line arguments after modification:"
          ts.WriteBlankLines 1
          ts.WriteLine "   " & strCmd
          ts.WriteBlankLines 2
       End If
       ts.WriteLine "Calling LINK.EXE linker"
       Shell "linklnk.exe " & strCmd
       If Err.Number <> 0 Then
          ts.WriteLine "Error in calling linker..."
       End If
       ts.WriteBlankLines 1
       ts.WriteLine "Returned from linker call"
    End Sub

    This proxy linker modifies only the command-line arguments passed to the linker if a .def file is present in the directory that contains the Visual Basic project; otherwise it simply passes the command-line arguments on to the linker unchanged. If a .def file is present, it adds a /DEF switch to the command line. It also determines whether any resource files are to be added to the linked file list. Finally, it examines the export table to determine if a function named CplApplet is present; if it is, it changes the output file's extension from .dll to .cpl.

  4. To install the proxy linker, rename the original Visual Basic linker LinkLnk.exe, copy the proxy linker to the Visual Basic directory, and name it Link.exe.

Once we create our proxy linker, we can reload our MathLib project and compile it into a DLL by selecting the Make MathLib.exe option from the File menu.

Testing the DLL

Once we create our Windows DLL, the final step is to test it to make sure that it works. To do this, create a new Standard EXE project (let's call it MathLibTest) and add a code module. To make sure that code in our project can access the functions exported by the DLL, we use the standard Visual Basic Declare statement. We declare our three exported math routines in the code module as follows:

Option Explicit

Public Declare Function Increment Lib "C:\VBProjects\MathLib\mathlib.dll" ( _
                        value As Integer) As Integer
Public Declare Function Decrement Lib "C:\VBProjects\MathLib\mathlib.dll" ( _
                        value As Integer) As Integer
Public Declare Function Square Lib "C:\VBProjects\MathLib\mathlib.dll" ( _
                        value As Long) As Long

We can then use the following code in the form module to call the routines in the DLL:

Option Explicit

Private Sub cmdDecrement_Click()
   txtDecrement.Text = Decrement(CInt(txtDecrement.Text))
End Sub

Private Sub cmdIncrement_Click()
   txtIncrement.Text = Increment(CInt(txtIncrement.Text))
End Sub

Private Sub cmdSquare_Click()
   txtSquare.Text = Square(CLng(txtSquare.Text))
End Sub

Private Sub Form_Load()
   txtIncrement.Text = 0
   txtDecrement.Text = 100
   txtSquare.Text = 2
End Sub

When we call each of the MathLib functions, the application window might appear as it does in Figure 2, confirming that the calls to the MathLib routines work as expected.

Figure 2
Figure 2: Testing calls to MathLib.dll

Ron Petrusha is the author and coauthor of many books, including "VBScript in a Nutshell."

Return to the Windows DevCenter.