COM stands for Component Object Model. COM components are an essential part of ASP. If you are
not familiar with COM and would like to have an overview of what it is then please read
Sample Chapter from Beginning ATL 3 COM Programming.
In this tutorial I will guide you step by step in creation of an ASP COM component using Microsoft
Visual Basic. After this tutorial you will be able to develop on your own relatively simple components
for use in your ASP pages.
Advantages of using Components in ASP
- Easy code reuse and distribution.
- Ease of maintenance and replacability.
- Increased efficiency and performance advantages.
- Hiding sensitive code.
- Splitting tasks into distinct areas.
- Ease of debugging.
Overview
We will create a component with just one method; Author, which will return the name of the
author of the component. Yeah I understand it isn't a very useful component, but again the idea is just
to show you how things are done. You can then later add your own methods according to your needs.
Getting Started
- Step 1 :
Start Microsoft Visual Basic 6 ( yes version 5 is also enough ).
- Step 2 :
From the 'New Project' dialogue box ( which pops up every time you start Microsoft Visual Basic ), select
'Activex DLL' and hit the 'open' button. An empty project 'Project1' with class module 'Class1' is created
for you by default.
New Project Window
- Step 3 :
Edit the Project and Class names so that Project name becomes 'Stardeveloper' and Class name becomes
'Author'. Note you can edit Project and Class names by selecting the Project or Class name on the right
Project window and then editing the name of it in the Properties window. Visual Basic always forms the
ProgID by using ProjectName.ClassName and thus the ProgID of our component will be
Stardeveloper.Author.
Project Properties
- Step 4 :
Now copy and paste the following code into the Code View window of our 'Stardeveloper' Project.
Yes you can change the name of the author to your own if you want to.
Public Function Author() As String
Author = "Faisal Khan"
End Function
- Step 5 :
Now go to 'File -> Make Stardeveloper.dll' and click the 'Make Stardeveloper.dll' button. A
dialogue box will appear asking you for the location to save 'Stardeveloper.dll' to, just
give it some location and hit 'ok'.
Make Stardeveloper.dll
Now what ? Nothing more to do, we are done. Yes thats right. This is the beauty of Microsoft
Visual Basic that it makes creating COM Components ( and other applications too ) a breeze for
developers. We don't even need to register our component as Visual Basic does this for us automatically
when it creates a component.
Distributing the Component
Although you may not want to distribute this component but just to elaborate things a bit or just
in case if you would later want to move your component to some other server, you can do it by copying
the 'Stardeveloper.dll' to any location on that server and then running the following command from the
DOS prompt :
regsvr32 Stardeveloper.dll
It will register our Component on that server. Our component will then be available to ASP pages
on that server there after. Now isn't it easy playing with components around ?
Calling Stardeveloper.Author Component from ASP
Create a new page and name it 'basic_com.asp'. Now copy and paste the following code into it
and then save it :
<html>
<head>
</head>
<body>
<%
' Declaring variable
Dim sd
' Creating instance of our Component
Set sd = Server.CreateObject("Stardeveloper.Author")
Response.Write "Name of author is : " & sd.Author
Set sd = Nothing
%>
Now place 'basic_com.asp' in some directory from where you can run ASP pages e.g. if you save the
'basic_com.asp' page in 'c:/inetpub/wwwroot/components/' directory, then enter
'http://127.0.0.1/components/basic_com.asp' in the URL box of your browser and hit 'enter'. You
will see name of the author written on the top left corner of your page.
We are now done. We have successfully made a COM Component using Microsoft Visual Basic and then
called it from the ASP page. We also saw that creating COM Components is very easy and that components
have a lot of advantage over the traditional VBScript coded ASP pages.
You can now create your own components for use in your ASP pages. You can add as many
functions or properties in your component as you want but just make sure that one COM Project in
Visual Basic should contain only one Class Module and not more than that. Enjoy!