Quick Search for:  in language:    
COM,tutorial,wrote,because,much,trouble,learn
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 196,174. lines
 Jobs: 103. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login


 

 


Latest Code Ticker for ASP/ VbScript.
Adjust a GIF's Hue/Sat/Lum
By Mark Kahn on 11/17


Search Engine
By vsim on 11/17


Click here to see a screenshot of this code!ADO Example 2
By Shannon Harmon on 11/17

(Screen Shot)

PureSourceCode2
By Enrico Rossini on 11/15


PureSourceCode
By Enrico Rossini on 11/15


Click here to see a screenshot of this code!SQL Code Wrapper for ASP
By Bret Dowell on 11/12

(Screen Shot)

Click here to see a screenshot of this code!Moveable DHTML Windows
By Mark Kahn on 11/11

(Screen Shot)

Simple image rotator
By moppy on 11/11


Admin Database Table
By cmsnyder on 11/10


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Writing a COM Object in Visual Basic

Print
Email
 

Submitted on: 5/26/2000 5:32:08 PM
By: Nathan Pond  
Level: Beginner
User Rating: By 10 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this article 10238 times.
 
(About the author)
 
     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
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
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 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 yea ' r is a leap year
Public Function IsLeapYear(yr As Variant) As Boolean
'If year is divisible by 4 and not divis ' ible by 100, or
'It is divisible by 400, it is a leap ye ' ar
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 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 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. 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 including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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
6/26/2000 10:32:11 AM:John Giles
You might want to go into how to register the COM item on an NT/IIS server. Other than that I like your article.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/23/2000 11:16:13 PM:donovan
and I was thinking I might have to write a console app in vb so that the server could read the stdout..cool this saved me decent amount of trial and error : )
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/27/2001 5:38:10 AM:Jon
I've been thinking about creating COM objects for ages but always thought it far to hard. Now I know otherwise, Thanks! I would like to see how to do it in C++ is possible.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/26/2001 5:32:31 PM:Ahmad Baigi
I like your artical on COM Object in VB and ASP. You did not mention how to register the com object on the server Thank you Ahmad
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/26/2003 4:18:16 PM:
I've been looking all over for this info. I figured it was pretty simple but am new to ASP, VBscript and Visual Basic so this really helped.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.