ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
Subject:   Fully functional VB6 API dll's - with Forms
Date:   2007-05-01 09:12:36
From:   zzz123
Response to: Fully functional VB6 API dll's - with Forms

Hi Bill,
it's me again. After further tinkering, i managed to invoke vb function ( which is contained in vb6 dll) from powerbuilder successfully.
But first, my expressed gratefulness for having stumbled upon the most helpful article "Creating a Windows DLL with Visual Basic" by Ron Petrusha, upon which my little experiment was made successful.
You can find the article at


This is what i did -

Part 1
1. create a new Standard EXE project ( name = Project1, file = Link.vbp).
2. add code module ( name = Link, file = Link.bas).
copy and paste following to Link

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

3. From File menu, select "Make Link.exe"
4. file Link.exe is created.
5. save project and exit VB
6. goto Folder C:\Program Files\Microsoft Visual Studio\VB98.
In this Folder, rename Link.exe to LinkLnk.exe
( this Link.exe is different from the Link.exe in step 4.).
Next, copy the Link.exe created in 4. to this Folder.
7. proceed to Part 2 below. The Link.exe created in Part 1
is invoked the moment you make MathLib.dll in Part 2.

Part 2
1. create a new ActiveDLL project ( name = Project1, file = MathLib.vbp).
2. add code module ( name = MathLib, file = MathLib.bas).
copy and paste following to MathLib

Option Explicit

Public Function LibMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
Select Case fdwReason
' No per-process cleanup needed
LibMain = True
' No per-thread initialization needed
' No per-thread cleanup needed
End Select
End Function

Public Function Increment(var As Integer) As Integer
'If Not IsNumeric(var) Then Err.Raise 5

Increment = var + 1
End Function

Public Function Decrement(var As Integer) As Integer
'If Not IsNumeric(var) Then Err.Raise 5

Decrement = var - 1
End Function

Public Function Square(var As Long) As Long
'Public Function Square(ByVal var As Long) As Long
'If Not IsNumeric(var) Then Err.Raise 5

Square = var * var
End Function

3. add class module ( name = clsMathLib, file = MathLib.cls).
leave clsMathLib empty ( i.e. no code)

4. create text file MathLib.def in the same directory as MathLib.vbp.
the content of MathLib.def is as below

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

5. From File menu, select "Make MathLib.dll"
6. file MathLib.dll is created. copy it to windows\system directory

Part 3
0. create new application, say pbtstdll
1. create window w_test.
From Declare menu, select "Local External Functions".
add following line to "Local External Functions"

function long Square( ref long vale) library "MathLib.dll"

2. add button to w_test.
add following to button's clicked event

long ll, val = 11

ll = Square( val)

messagebox( string( ll), '')

3. save all changes.
4. run pbtstdll
5. click the button created in 2.
messagebox should pop up, thus verifying vb6 function named Square is successfully invoked.

1 to 1 of 1
  1. Fully functional VB6 API dll's - with Forms
    2009-01-28 04:23:19  GopiGrowlogic [View]

1 to 1 of 1