Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (26)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
what is the right code to link the asp page t..
by amylisa on 22 Jul 2008 Go To Post

Can Loader.asp Get Form Elements
by azziham on 14 Jul 2008 Go To Post

Good asp resource sites
by codemylife on 3 Jul 2008 Go To Post

Re: Unable to insert data in an Access databa..
by asia on 3 Jul 2008 Go To Post

Re: problem with do while loop
by idsanjeev on 30 Jun 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : COM : Creating an ASP COM Component in Visual Basic
 

Creating an ASP COM Component in Visual Basic
by Faisal Khan.

COM stands for Component Object Model. COM components are an essential part of ASP. If you are not familiar with COM and would like to have an overview of what it is then please read Sample Chapter from Beginning ATL 3 COM Programming.

In this tutorial I will guide you step by step in creation of an ASP COM component using Microsoft Visual Basic. After this tutorial you will be able to develop on your own relatively simple components for use in your ASP pages.

Advantages of using Components in ASP

  • Easy code reuse and distribution.
  • Ease of maintenance and replacability.
  • Increased efficiency and performance advantages.
  • Hiding sensitive code.
  • Splitting tasks into distinct areas.
  • Ease of debugging.

Overview
We will create a component with just one method; Author, which will return the name of the author of the component. Yeah I understand it isn't a very useful component, but again the idea is just to show you how things are done. You can then later add your own methods according to your needs.

Getting Started

  • Step 1 : Start Microsoft Visual Basic 6 ( yes version 5 is also enough ).
  • Step 2 : From the 'New Project' dialogue box ( which pops up every time you start Microsoft Visual Basic ), select 'Activex DLL' and hit the 'open' button. An empty project 'Project1' with class module 'Class1' is created for you by default.
New Project Window
New Project Window
  • Step 3 : Edit the Project and Class names so that Project name becomes 'Stardeveloper' and Class name becomes 'Author'. Note you can edit Project and Class names by selecting the Project or Class name on the right Project window and then editing the name of it in the Properties window. Visual Basic always forms the ProgID by using ProjectName.ClassName and thus the ProgID of our component will be Stardeveloper.Author.
Project Properties
Project Properties
  • Step 4 : Now copy and paste the following code into the Code View window of our 'Stardeveloper' Project. Yes you can change the name of the author to your own if you want to.
Public Function Author() As String
	Author = "Faisal Khan"
End Function
  • Step 5 : Now go to 'File -> Make Stardeveloper.dll' and click the 'Make Stardeveloper.dll' button. A dialogue box will appear asking you for the location to save 'Stardeveloper.dll' to, just give it some location and hit 'ok'.
Make Stardeveloper.dll
Make Stardeveloper.dll

Now what ? Nothing more to do, we are done. Yes thats right. This is the beauty of Microsoft Visual Basic that it makes creating COM Components ( and other applications too ) a breeze for developers. We don't even need to register our component as Visual Basic does this for us automatically when it creates a component.

Distributing the Component
Although you may not want to distribute this component but just to elaborate things a bit or just in case if you would later want to move your component to some other server, you can do it by copying the 'Stardeveloper.dll' to any location on that server and then running the following command from the DOS prompt :

regsvr32 Stardeveloper.dll

It will register our Component on that server. Our component will then be available to ASP pages on that server there after. Now isn't it easy playing with components around ?

Calling Stardeveloper.Author Component from ASP
Create a new page and name it 'basic_com.asp'. Now copy and paste the following code into it and then save it :

<html>
<head>
</head>
<body>
<%
' Declaring variable
Dim sd
' Creating instance of our Component
	Set sd = Server.CreateObject("Stardeveloper.Author")
Response.Write "Name of author is : " & sd.Author
Set sd = Nothing
%>

Now place 'basic_com.asp' in some directory from where you can run ASP pages e.g. if you save the 'basic_com.asp' page in 'c:/inetpub/wwwroot/components/' directory, then enter 'http://127.0.0.1/components/basic_com.asp' in the URL box of your browser and hit 'enter'. You will see name of the author written on the top left corner of your page.

We are now done. We have successfully made a COM Component using Microsoft Visual Basic and then called it from the ASP page. We also saw that creating COM Components is very easy and that components have a lot of advantage over the traditional VBScript coded ASP pages.

You can now create your own components for use in your ASP pages. You can add as many functions or properties in your component as you want but just make sure that one COM Project in Visual Basic should contain only one Class Module and not more than that. Enjoy!


 ( No Further Pages )

Download Associated Files
2000041001.zip

Related Articles
  1. ASP Intrinsic Objects from within COM Component

Comments/Questions ( Threads: 5, Comments: 6 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Hi Faisal, Please can you help me.
  2. IIS 6 / Windows Server 2003 Problems
  3. help me please.
  4. Creating only one Class Module when creating COM Project in Visual Basic
  5. Problems accessing some interface of the COM object using ASP ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserverd.