To read the article online, visit http://www.4GuysFromRolla.com/webtech/040300-1.shtml

Writing a COM Object with VisualBasic 6.0

By 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 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
    Else
        IsLeapYear = False
    End If
    
End Function

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 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

%>
<HTML>
<HEAD>
<TITLE>The world population</TITLE>
</HEAD>
<BODY>
<%
	'Let the user know 
	If IsLeapYear = True Then
		Response.Write "<P>The year <b>" & Year & "</b> is a leap year.</P>"
	Else
		Response.Write "<P>The year <b>" & Year & "</b> is not a leap year.</P>"
	End If
%>
</BODY>
</HTML>

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 npond@bgnet.bgsu.edu. 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!

  • By Nathan Pond


  • Article Information
    Article Title: Writing a COM Object with VisualBasic 6.0
    Article Author: Nathan Pond
    Published Date: Monday, April 03, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/040300-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers