Click to See Complete Forum and Search --> : ActiveX DLL


johnpc1
January 12th, 2000, 01:54 PM
I am interested in creating an ActiveX DLL that will simply accept two
numbers from a VB program, add them together and pass back
their sum to the calling program. I guess my problem has two parts:

Part 1. How can one create a DLL in VB?. I am presuming that by
starting a new VB ActiveXDll project and compiling it to a
DLL the DLL is created.

Part 2. Having created the ActiveXDLL, how can it be accessed via VB?
The reason I used a simple addition function as an example
is because I have developed some much more sophisticated functions
used in some of my programs and I don't want the algorithm,
logic and coding to be available to end users.

So I guess what I am looking for is a simple example of a project where an
ActiveXDLL is created and then accessed via VB. Any assistance will be greatly
appreciated, thanks.

Chris Eastwood
January 13th, 2000, 02:54 AM
ActiveX DLL's are so useful (and easy to create) that you'll start to wonder how you ever managed without them.

Start VB and choose the New ActiveX DLL style project.

You'll see that it's created a project with one class module (Class1) - let's make things more interesting :

1. Right click on the 'Project1' in your treeview / project explorer and choose properties

2. Change the 'Project Name' to 'MathLib'

3. Change the 'Project Description' to 'A Test Math DLL'

4. Close the Project Properties window.

5. Click on the 'Class1' module in the project explorer to select it, then change it's name (in the properties window) to 'cMath'

6. Now open the code window for your 'cMath' class module and type / paste in the following code :

option Explicit
'
public Function AddTwoNumbers(byval NumberOne as Long, byval NumberTwo as Long) as Long
AddTwoNumbers = NumberOne + NumberTwo
End Function
'
public Function MaxValue(byval NumberOne as Long, byval NumberTwo as Long) as Long
If NumberOne > NumberTwo then
MaxValue = NumberOne
ElseIf NumberTwo > NumberOne then
MaxValue = NumberTwo
else
MaxValue = NumberOne
End If
End Function




This has given your class module two methods - these can then be called from a 'client' program that uses your dll - let's create that now :

1. Go to the VB 'File-Add Project' menu and choose 'Standard EXE'

2. Add a button (Command1) to your form.

3. Now go to the VB 'Project->References' menu and look for 'MathLib' (NB. if you'd compiled the DLL, you would be looking for 'A Test Math DLL' - select it and then close the project references window

4. Now double click on the Command1 button to get the the Command1_Click code and paste in the following :


private Sub Command1_Click()
Dim lValue1 as Long
Dim lValue2 as Long
Dim oMath as MathLib.cMath
'
' Create an instance of the 'cMath' class
'
set oMath = new MathLib.cMath
'
lValue1 = 100
lValue2 = 200
'
' Show the results of adding the two numbers
'
MsgBox oMath.AddTwoNumbers(lValue1, lValue2)
'
' Show the 'Max' value from these two numbers
'
MsgBox oMath.MaxValue(lValue1, lValue2)
'
set oMath = nothing
'
End Sub




5. Now you are just about ready to run the Test client program - one more thing though - right click on the 'Project1' in the project explorer (ie. the project you've just added) and select 'Set As Startup'

6. Now Run the program (debugging if you want to look through the code)





Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb