Microsoft Knowledge Base Email Alertz

(299714) - This step-by-step article demonstrates how to call a Component Object Model (COM) object from within an Active Server Pages (ASP) page. Requirements The following list outlines the recommended hardware, software, network infrastructure and service...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 299714 - Last Review: November 17, 2003 - Revision: 2.2

HOW TO: Call a COM Object from Active Server Pages

This article was previously published under Q299714

On This Page

SUMMARY

This step-by-step article demonstrates how to call a Component Object Model (COM) object from within an Active Server Pages (ASP) page.

Requirements

The following list outlines the recommended hardware, software, network infrastructure and service packs that you need:
  • Microsoft Internet Information Server (IIS) or Personal Web Server (PWS)
  • Microsoft Visual Basic

Calling a COM Object from an ASP Page

This sample illustrates the common scenario in which you develop a COM component, create the object from an ASP page, and then call the object's methods.

Create a Component in Visual Basic

To build a COM component with Visual Basic, follow these steps:
  1. Start Visual Basic.
  2. Create a new ActiveX DLL project named CustomerLib.
  3. Rename the class clsCustomer.
  4. Paste the following code in class module:
    	Function GetCustomerName() As String
    		GetCustomerName = "David Johnson"
    	End Function
    					
  5. On the File menu, click Make CustomerLib.dll to compile the project.

Create an Instance of a Component

Use the following code to instantiate the COM component from ASP:
<%@ Language=VBScript %>
<%
	dim objCustomer
	dim strCustomerName

	' Create the component.	
	set objCustomer=server.createobject("CustomerLib.clsCustomer")

	' Call the method.
	strCustomerName = objCustomer.GetCustomerName

	' Display the return value.
	Response.Write strCustomerName
%>
				

Understanding Component Interfaces

An interface is simply a list of related methods, properties, and events. The interface does not determine what happens within the methods. Instead, the interface determines what methods are available, as well as the structure or "signature" of each method (the method name, argument list, and return type). The component that implements the particular interface must determine how those methods are executed. Each COM component implements one or more of these custom interfaces, as well as one or more standard COM interface for COM plumbing.

In the preceding sample, Visual Basic creates the interface named _clsCustomer, which contains one method (GetCustomerName). The clsCustomer class then implements this interface as its default interface and defines how the method will be executed (by returning "David Johnson").

Another class can implement this interface differently and return any other string according to any logic that the class chooses. Although you can use the Implements keyword to implement more than one interface in Visual Basic, scripting clients (such as ASP) can only use the default interface. This is important to remember when you design components that you want to use from ASP.

Pitfalls

You must register COM objects on your server. If the vendor's installation program does not register COM objects for you, you can use Regsvr32.exe to register the component on the computer.

REFERENCES

For more information, refer to the following Microsoft Web sites:
Microsoft COM Home Page
http://www.microsoft.com/com/ (http://www.microsoft.com/com/)

COM Objects in ASP
http://msdn.microsoft.com/en-us/library/ms972341.aspx (http://msdn.microsoft.com/en-us/library/ms972341.aspx)

APPLIES TO
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Active Server Pages 4.0
Keywords: 
kbcodesnippet kbhowto kbhowtomaster KB299714
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Animesh DEvarshi - animeshdevarshi NOSPAM-AT-NOSPAM hotmail.com Report As Irrelevant  
Written: 3/25/2007 3:58 AM
It was good. But it is not running.