![]() |
|
|
Published: Monday, April 03, 2000 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 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 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
Now that we have a name for our project, we will want to name our class from 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.
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 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 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!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||