You are on page 1of 3

ActiveX Dll Tutorial --- by JDT

/*This tutorial was made in this format by me, Cavagnaro, I DO NOT take authority for it, it was
made by a guy JDT. ALL credits goes to him.*/

This tutorial is designed for those who know nothing about creating ActiveX Dll components in
VB6.

Skill level --------> Intermediate


Prerequisites ---> Basic understanding of programming concepts and working knowledge of VB6
Time --------------> About 15 minutes

First let’s get a brief understanding of what a Dll is and why we would use one.

Dynamic-link libraries (DLL) contain reusable functions and data. With the use of Dll files, you can
store often used functions in a common place, link to it through code, and leverage its
functionality in any of your applications without adding the common procedures to every
application. There is a down side to Dll’s; it must be installed on the users pc for your application
to access it. The up side is mostly felt in large-scale applications. A good example is the Windows
OS. Us VB programmers link to Dlls all the time (API) to get the functionality we need. Dividing
your large projects into smaller pieces using Dlls is a more efficient way to manage you projects
and fixing bugs in functions is a lot easier to just replace a small Dll than replacing a several meg
exe.

What is ActiveX?

ActiveX technology is based on the Component Object Model (COM). COM is Microsoft’s
standard that defines how software components interact with each other. COM is an
implementation of Object Oriented Programming (OOP) that is language independent, which is a
feature known as interoperability. ActiveX Dll's run in-process (uses memory space in your app).
Most importantly, VB does the real work for you. If you know VB6 then you are 95% of the way
there.

With those crash course descriptions out of the way, lets get started.

Make the Dll

1. Start VB and select ActiveX Dll for the project type.


2. In the properties window for the Class (the only code window in the project), name it
'CSomeDll'. Note - this is not the project properties window and is the same as naming a
form.
3. Select Project from the menu, select Project1 Properties from the drop down list, change the
project name to 'SomeDll', and change the Project Description to 'This is a useless DLL'. This
description will show up in the Object Browser and the References list and is VERY
IMPORTANT.
4. Now lets add a method (Function) to the class. Lets just have this take a string for its
parameter and return it spelled backwards. Of course this function already exists in VB, but it
will suit the needs of this simple example.
4.1. Make a Public function named 'ReverseString' in the class module. It must be Public if
you want it to be visible to your client applications.

Option Explicit
Public Function ReverseString(ByRef SomeString As String) As String

ReverseString = StrReverse(SomeString)

End Function

5. Lets compile it by selecting File, in the drop-down list select Make SomeDll.dll and choose a
safe place to save it.
6. Save the project wherever you like and close it.

That’s it, the Dll is ready for a client application to use it.

Lets now make a client application to implement our newly made Dll.

1. Start VB and Select 'Standard Exe' for the project type.


2. Add one textbox named Text1 and one Command Button named Command1 to the form.
3. Now lets reference our Dll by selecting Project from the menu bar, then select
References from the drop-down list. Now you need to scroll through the list and look for --
-> 'This is a useless DLL' <--- That is the Project Description we gave our Dll in step 4 of
creating the Dll. Put a check mark by it and click ok. If you don't see it, browse for it in the
location you saved it in step 5 of creating the Dll (should be unnecessary though)
4. Now our client app knows about the library we want to link to but we still need to assign
the class in the Dll to an object variable. So add this code to the general declarations
section of your forms code module:

Dim X As CSomeDll

'CSomeDll' is the name of the class in the DLL that has the method we want to use. We named it
this in step 2 of creating the Dll.

5. Now add this code to Command1:

Private Sub Command1_Click()

Set X = New CSomeDll '<---Assigns an object reference


Text1.Text = X.ReverseString(Text1)
Set X = Nothing '<--------Release resources to the object

End Sub

As you just seen (unless you pasted the code), VB provided Intellisense to the variable X and we
see the method we added to the Dll's class object.

6. Run the program and click the button a few times to see it work.

That’s it. Simple enough yet very powerful when the situation mandates it.

What about registering the Dll?


VB automatically registered the component when you compiled the Dll. If you are to distribute
this, you will need to use a good Installer (not PDW) to do it for you. To manually register this on
a pc, from the command prompt type this ---> REGSVR32 C:\PathToTheDll.dll <--- and to
unregister it, type this --->REGSVR32 /u C:\PathToTheDll.dll Note -- 'C:\PathToTheDll.dll' should
be the actual path to the Dll file.

Some noteworthy stuff about ActiveX dlls

You can use a Group project while designing the Dll. Highly recomended

ActiveX exe's do the same as ActiveX Dll's except exe's run out of process (their own address
space) and uses asynchronous notification.

ActiveX Dll's beat out ActiveX Exe's on performance

You can make a *real* Dll in VB and declare it like the API calls you might have used but this
can't be done with ActiveX. ActiveX is much more preferable. Unless you know C, this is quite
difficult,

JDT

You might also like