Writing a COM Object in Visual Basic

Submitted on: 1/5/2015 7:13:00 AM
By: Nathan Pond (from psc cd)  
Level: Beginner
User Rating: By 10 Users
Compatibility: ASP (Active Server Pages)
Views: 598
     This a tutorial I wrote because I had so much trouble learning how to write a COM object, I wanted to put it in simple terms for anyone else who might want to get into it.

This article has accompanying files
				Let's face it, we all love ASP ans 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. Int 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 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 project "CheckYear" and our class "LeapYear".

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 ‘CheckYear’.

Now that we have a name for our project, we will want to name our class from Class1 to 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 'LeapYear'.

Now add the following code into your class:


Option Explicit
'Function to return if the specified year is a leap year
Public Function IsLeapYear(yr As Variant) As Boolean
'If year is divisible by 4 and not divisible by 100, or
'It is divisible by 400, it is a leap year
If (yr Mod 4 = 0 And yr Mod 100 <> 0) Or yr Mod 400 = 0 Then
IsLeapYear = True
IsLeapYear = False
End If

End Function

Even though I'm not trying to teach you visual basic, 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
2. Now I create the function. I have to create it as a Public function, because it needs to be called from outside this class (ie. the ASP page). If I made it a private function, then only code inside this class could call it.
3. Once inside the function, I use a simple If statement 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 my 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:

<% Option Explicit
Dim oCheckYear 'Create an object for the component
Dim IsLeapYear 'Create a string to hold the result
Dim Year 'Create a var to hold the year
Year = 1900 'You can cahnge this number to any year
'you want, keep the years 4 digits though
'Create an instance of the Component we just wrote
Set oCheckYear = CreateObject("CheckYear.LeapYear")
'Call the IsLeapYear function in our component, and
'store the result
IsLeapYear = oCheckYear.IsLeapYear(Year)
'Close the instance, good programming practice
Set oCheckYear = Nothing
<TITLE>The world population</TITLE>
'Let the user know
If IsLeapYear = True Then
Response.Write "<P>The year <b>" & Year & "</b> is a leap year.</P>"
Response.Write "<P>The year <b>" & Year & "</b> is not a leap year.</P>"
End If

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 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. Thanks for reading!

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.