SUMMARY
This step-by-step article demonstrates how to call a Component Object Model (COM) object from within an Active Server Pages (ASP) page.
back to the top
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
back to the top
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.
back to the top
Create a Component in Visual Basic
To build a COM component with Visual Basic, follow these steps:
- Start Visual Basic.
- Create a new ActiveX DLL project named CustomerLib.
- Rename the class clsCustomer.
- Paste the following code in class module:
Function GetCustomerName() As String
GetCustomerName = "David Johnson"
End Function
- On the File menu, click Make CustomerLib.dll to compile the project.
back to the top
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
%>
back to the top
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.
back to the top
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.
back to the top