Writing a COM Object with VisualBasic 6.0By Nathan Pond
Let's face it, we all love ASP and VBScript, but it has it's limitations. However, we don't have to live by these limitations when we develop our web sites. Did you know that anything that can be done in languages like Visual Basic, Visual C++, Visual J++, and many others can be accomplished in your web developing. COM (Component Object Model) can be used to write objects that you can call from ASP. In this article I will show you how to write a COM object in Visual Basic 6, how to register it on the server, and how to call it from ASP; but first let me explain a little bit about what COM is.
COM Objects are usually
.dll files, and are compiled executable programs. This means that the
code will run faster than ASP code. They must be registered on the server that IIS is running on, and they
cannot be registered through ASP code. This means that you need to have access to the server to use them.
I am assuming you have no experience in Visual Basic, so I will hold your hand through all of this. If you are familiar with VB just bare with me. What we are going to do is create a component that you will pass a year to and it will return if that year is a leap year or not. We will use a simple algorithm from http://www.rog.nmm.ac.uk/leaflets/leapyear/leapyear.html to figure this out. Now, I know some of you are going to look at this code and tell me it could be done in ASP without the use of COM. Well, you're right, but this article is intended to show how to integrate Components into your ASP pages, so I want to make the example simple enough to understand instead of getting into complicated windows API calls that can't be accomplished in ASP.
The first thing we want to do is write the ActiveX dll, we can worry about getting ASP to call it later. So go ahead and open up Visual Basic. A dialog should pop up right away asking what kind of project you want to start, if it doesn't, click on "File | New Project" from the menu bar, and the dialog will open. Choose the "ActiveX DLL" icon for your project.
Now the project is created along with a default class, we should rename these to our liking. Let's name our
CheckYear and our class
If the Project Explorer is not already displayed, select "View | Project Explorer" from Visual Basicís menu. Youíll also need the Properties window showing, which can be displayed by selecting "View | Properties Window" from the menu.
Now click on the
Project1 name within the Project Explorer and look at the Properties window. It
shows the name of our project to be
Project1. Highlight the
Project1 text to the
right of the (Name) label within the Properties window. Change it to
Now that we have a name for our project, we will want to name our class from
LeapYear. In the Project Explorer window, click on
Class1. If you donít see the
Class1 name, and only the Class Moduleís text is showing, click the plus icon within the square
to display the class name. Now, down in the Proprieties window, highlight the
Class1 text next to
the (Name) label and change it to
Now add the following code into your class:
Even though I'm not trying to teach you VisualBasic, I will go through what I just did briefly to clear up some questions.
1. I set
Option Explicit; this is good programming practice in both Visual Basic and VBScript for ASP. For more information on using Option Explicit, visit http://www.4guysfromrolla.com/webtech/faq/Intermediate/faq6.shtml.
2. Now I create the function. I have to create it as a
Publicfunction, because it needs to be called from outside this class (ie. the ASP page). If I made it a
Privatefunction, then only code inside this class could call it.
3. Once inside the function, I use a simple
Ifstatement to implement the algorithm. If it is a leap year, I return True, otherwise I return False.
That's all the code that is needed, now we just need to compile our dll. Click on the "File" menu, and near the
bottom click on "Make CheckYear.dll..." Choose where to save your dll, and click ok. I have a directory in
InetPub directory called "Server Components" where I keep all of my components that I call
from ASP, but this isn't required, no matter where you save it it will work. When you compile the dll, it
is automatically registered on the server.
Now all we have to do is call our component from ASP. Create an ASP page, and add the following code:
The ASP code here is fairly simple, so I'm only going to explain the part where I connect to the component.
The syntax is
CreateObject("Projectname.Classname"). If you remember, we named our Project
CheckYear and our Class
LeapYear. Once you create the object, you have access to
all of the public functions in that class.
That's it, that's all there is to it. If you run your ASP page now you should see the results. I tried to keep things simple, but I didn't want to have anyone do another 'Hello world' project. :-) As always, if you have any questions, comments, or anything else at all to say about this article, please feel free to e-mail me at email@example.com. I don't guarantee speedy responses, but I try to get back to everyone who e-mails me. Also, I have shown how to do this using Visual Basic, if there is enough interest I might consider also writing an article showing how to write a component using Visual C++/ATL. So if you would like to see an article like that e-mail me and let me know.
If you're interested in reading how to create COM components using Java, be sure to read An Introduction to Using Java with ASP.
Thanks for reading, and happy programming!