Creating your First COM Component
Before we continue, you will need to make sure you have a few things ready:
- Visual Basic 6 (for creating your component)
- PWS (Personal Web Server) or IIS4 (Internet Information Server v.4)
- Notepad editor or Visual InterDev (for ASP editing)
I will be giving ASP examples in both VBScript and Javascript so don't worry about
what scripting language your most familiar with.
We will be creating a COM Component that does one thing: adds two numbers and returns
the result.
Please follow each step as outlined below:
- Startup Microsoft Visual Basic 6.0
- From the New tab, click on
then click on the
Open button.
- Click on the Project menu then click on Project1 Properties....
- Change the project name from Project1 to Calculator.
- Click on the Unattended Execution checkbox - this will make sure that
Visual Basic does not compile your component if it contains illegal code such as
MsgBox().

- Click on the OK button.
- Change the name of the ClassModule to Adder as shown below:

NOTE: A Class Module in Visual Basic means the same thing as a COM Component,
the Project name is the default name of the COM Server (the DLL or EXE compiled
result), and the component's ProgID is a combination of
both the Project name and Class Module (Calculator.Adder).
- The easiest way to add properties to your component is by using the the
Class Builder. Click on the Add-Ins menu then click on
Add-In Manager....
- Double-Click on VB 6 Class Builder Utility then click on the OK
button.

- Click on the Add-Ins menu then click on Class Builder Utility....
- Click on the
class in the left pane.
- Click on the
button to add a new
property.
- Enter intOne as the Name: and change the Date Type: to
Integer
.
- Perform steps 12 and 13 again BUT this time set the property name to
intTwo.
- Click on the File menu then click on Update Project.
- Close the Class Builder. You will notice that the
Class Builder has added code to the code window.
- Now we need to add the function/method that will add intOne to
intTwo. Add the following code to the bottom of the code window:
Public Function add() As Integer
add = intOne + intTwo
End Function
- Save your work and call your files whatever you like.
- Next, we need to compile the component so click on the File then click on
Make Calculator.dll....
- Choose a location where to save your COM Server. Remember where it is as you will
need to register it on the PWS or IIS4 server machine in order to use it.
- Now we have to register the COM Server on the PWS or IIS4 server. If the
Calculator.dll COM Server is not on the web server, copy it to a location on
that server now.
- At the server, register the COM Server by using the following syntax:
regsvr32 C:\Path\Calculator.dll
Replace C:\Path with the actual location you stored the DLL. You can run
regsvr32 from either the Start/Run... dialogue box or the DOS
Command Prompt. Once registering your COM Server, you should get a confirmation
dialogue box like the one below:
Now that your all done creating and registering your first component,
let's get an ASP page ready to use it!
- Using Visual InterDev or Windows' Notepad, create an ASP page on your web server's
virtual directory called Calc.asp.
- Now we fill in the ASP page with code:
Using Javascript:
var objCalc = Server.CreateObject("Calculator.Adder");
objCalc.intOne = 5;
objCalc.intTwo = 4;
var ans = objCalc.add();
Response.Write(ans);
var objCalc = null
Using VBScript:
Set objCalc = Server.CreateObject("Calculator.Adder")
objCalc.intOne = 5
objCalc.intTwo = 4
ans = objCalc.add()
Response.Write(ans)
Set objCalc = Nothing
- Run the Calc.asp page from a browser. The only thing you should see on the
browser window is the number 9. This is a result of the method
objCalc.add being invoked which adds the values 5 and 4 (intOne and intTwo
respectively).
|