Creating a Visual Basic COM Component and Calling it from ASP

IIS 6.0

In this lesson, you use Visual Basic to create a simple COM object, which you can call from an ASP page. This example requires Visual Basic with the ActiveX Wizard, and it is not supported on 64-bit platforms until the Visual Basic runtime is developed for 64-bit platforms. You create a 32-bit COM object that runs on a 64-bit platform, but you must call the 32-bit COM object from a 32-bit application. Because IIS is a 64-bit application on 64-bit platforms, it cannot call 32-bit objects.

Suppose you want to create a Web application that needs functionality VBScript does not have. In this case, you must create a custom procedure that is called, as needed, from any ASP page in your application.

Normally, this approach is an adequate solution for encapsulating custom functionality. However, imagine that you are creating a Web application intended to service thousands of users and that your procedure encapsulates proprietary functions you do not want other people to see. In this case, encapsulating your functionality in the form of a COM component is a superior approach. Components provide better security and performance than scripts because they are compiled code. Components also allow you to use functionality provided by Visual Basic, C++, Java, or any of the other COM-compliant languages.

The ActiveX DLL Wizard of Visual Basic is the easiest way to create a COM component. You can also use Microsoft Visual C++ to create a COM component, either by using the Active Template Library (ATL) or by writing all the code yourself. This example uses Visual Basic.

In this lesson, you learn how to create and encapsulate a Visual Basic function as a component. Visual Basic includes many financial functions not available to VBScript. This example computes the future value of an investment based on a fixed interest rate and periodic, fixed payments.

To create the ActiveX COM Object

  1. Open Visual Basic. If you don't see a window titled New Project, choose File and then click New Project.

  2. Select ActiveX DLL, and click OK.

  3. A window should open called Project1 - Class1 (Code). This is where you enter your code.

  4. From the Project menu, click Project1 Properties. In the General property sheet, in the Project Name box, type ASPTut. Your DLL is called ASPTut.dll. Select the Unattended Execution check box so that the project runs without user interaction and has no user interface elements. Make sure the Threading Model is Apartment Threaded so that more than one user at a time can use the DLL. Click OK.

  5. In Visual Basic, you define a class to group together methods and properties. Under the Project - ASPTut window, click the Class1 (Class1) node to list the class properties below. Under Properties - Class1, click in the text field beside (Name) and change the class name to Finance. When you call this COM component in an ASP page or other script, you will reference it with ASPTut.Finance. Click the drop-down box beside Instancing, and select 5 - MultiUse.

  6. Learn about the Visual Basic function you are about to use. FV is documented in the MSDN Online Library under the Visual Basic library.

  7. The window that was previously titled Project1 - Class1 (Code) should now be titled ASPTut - Finance (Code). Copy and paste the following text into that window:

    
    Option Explicit  
    
     'Declare the global variables that will be set by the Property functions.  
     Dim gAnnualIntRate As Double  
     Dim gNumPayPeriods As Integer  
     Dim gPayment As Double  
     Dim gPresentSavings As Variant 'Optional  
     Dim gWhenDue As Variant 'Optional  
    
     Public Function CalcFutureValue() As Double  
    
     'The global variables you pass to the FV function are set  
     'when user sets the properties in the ASP page.  
     'You could pass variables into the CalcFutureValue() function  
     'if you wanted to avoid using properties.  
     'CalcFutureValue becomes a method in your component.  
    
     Dim IntRatePerPeriod As Double  
     Dim FullFutureValue As Double  
    
     If (gAnnualIntRate = Null) Or (gNumPayPeriods = Null) Or (gPayment = Null) Then  
       CalcFutureValue = 0  
     Else  
       IntRatePerPeriod = gAnnualIntRate / 100 / 12  
       FullFutureValue = FV(IntRatePerPeriod, gNumPayPeriods, gPayment, gPresentSavings, gWhenDue)  
       CalcFutureValue = Round(FullFutureValue, 2)  
     End If  
    
     End Function  
    
     Public Property Get AnnualIntRate() As Double  
       'Get functions return the value of the global variables  
       'as if they were properties.  
       'In your ASP page, you might say x = oASPTut.Rate.  
       AnnualIntRate = gAnnualIntRate  
     End Property  
    
     Public Property Let AnnualIntRate(ByVal vAnnualIntRate As Double)  
       'Let functions set the global variables when your ASP page  
       'makes a call such as oASPTut.Rate = 5.  
       gAnnualIntRate = vAnnualIntRate  
     End Property  
    
     Public Property Get NumPayPeriods() As Integer  
       NumPayPeriods = gNumPayPeriods  
     End Property  
    
     Public Property Let NumPayPeriods(ByVal vNumPayPeriods As Integer)  
       gNumPayPeriods = vNumPayPeriods  
     End Property  
    
     Public Property Get Payment() As Double  
       Payment = gPayment  
     End Property  
    
     Public Property Let Payment(ByVal vPayment As Double)  
       gPayment = -(vPayment)  
     End Property  
    
     Public Property Get PresentSavings() As Variant  
       PresentSavings = gPresentSavings  
     End Property  
    
     Public Property Let PresentSavings(ByVal vPresentSavings As Variant)  
       gPresentSavings = -(vPresentSavings)  
     End Property  
    
     Public Property Get WhenDue() As Variant  
       WhenDue = gWhenDue  
     End Property  
    
     Public Property Let WhenDue(ByVal vWhenDue As Variant)  
       gWhenDue = vWhenDue  
     End Property  
    
    
    
  8. All server components require an entry (starting) point. This is the code that will be called when the object is first instantiated with Server.CreateObject Your ASPTut component does not have to do anything special when it is first called. For this reason, you can provide an empty Sub Main procedure. From the Project menu, select Add Module. In the Add Module window, under the New tab, select the Module icon and click Open. In the Add Module window, under the New tab, select the Module icon and click Open. In the Module 1 code window, type Sub Main and press Enter. An empty sub is created for you.

  9. Save your Sub Main module as Main.bas. Save your class file as Finance.cls. Save your project as ASPTut.vbp.

  10. Click File, and click Make ASPTut.dll. This compiles and registers the ASPTut.dll. After you have called ASPTut.dll from an ASP page, you cannot make the DLL in Visual Basic until you unload the application in which the ASP file is running. One way to do this is to use the IIS Manager to open the properties on your default Web site and click the Unload button. If you want to register your DLL on another Web server, copy ASPTut.dll to the server, click Start, click Start, click Run, and type cmd into the Open text box. In the same directory as ASPTut.dll, type regsvr32 ASPTut.dll.

  11. Exit Visual Basic.

This example ASP page uses a form to read in user data, creates an instance of your object, and calculates the future value of your savings plan.

Copy and paste the following code in your text editor, and save the file as CalculateFutureValue.asp in the x:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing http://localhost/Tutorial/CalculateFutureValue.asp in the address bar.

<%@ Language= "VBScript" %>  
<%  
Response.Expires = 0  
Payment = Server.HTMLEncode(Request.Form("Payment"))  
AnnualIntRate = Server.HTMLEncode(Request.Form("AnnualIntRate"))  
NumPayPeriods = Server.HTMLEncode(Request.Form("NumPayPeriods"))  
WhenDue = Server.HTMLEncode(Request.Form("WhenDue"))  
PresentSavings = Server.HTMLEncode(Request.Form("PresentSavings"))  
%>  
<HTML>  
<HEAD> 
<TITLE>Future Value Calculation</TITLE> 
</HEAD>  
<BODY>  
<FONT FACE="MS Gothic">  
<H2 align=center>Calculate the Future Value of a Savings Plan</H2>  
<FORM METHOD=POST ACTION="calculatefuturevalue.asp">  
<TABLE cellpadding=4 align=center>  
<TR>  
<TD>How much do you plan to save each month?</TD>  
<TD><INPUT TYPE=TEXT NAME=Payment VALUE=<%=Payment%>> (Required)</TD>  
</TR><TR>  
<TD>Enter the annual interest rate.</TD>  
<TD><INPUT TYPE=TEXT NAME=AnnualIntRate VALUE=<%=AnnualIntRate%>> (Required)</TD>  
</TR><TR>  
<TD>For how many months will you save?</TD>  
<TD><INPUT TYPE=TEXT NAME=NumPayPeriods VALUE=<%=NumPayPeriods%>> (Required)</TD>  
</TR><TR>  
<TD>When do you make payments in the month? </TD>  
<TD><INPUT TYPE=RADIO NAME=WhenDue VALUE=1 <%If 1=WhenDue Then Response.Write"CHECKED"%>>Beginning  
<INPUT TYPE=RADIO NAME=WhenDue VALUE=0 <%If 0=WhenDue Then Response.Write"CHECKED"%>>End </TD>  
</TR><TR>  
<TD>How much is in this savings account now?</TD>  
<TD><INPUT TYPE=TEXT NAME=PresentSavings VALUE=<%=PresentSavings%>> </TD>  
</TR>  
</TABLE>  
<P align=center><INPUT TYPE=SUBMIT VALUE=" Calculate Future Value ">  
</FORM>  
<%  
If ("" = Payment) Or ("" = AnnualIntRate) Or ("" = NumPayPeriods) Then  
 Response.Write "<H3 align=center>No valid input entered yet.</H3>"  
ElseIf (IsNumeric(Payment)) And (IsNumeric(AnnualIntRate)) And (IsNumeric(NumPayPeriods)) Then  
 Dim FutureValue  
 Set oASPTut = Server.CreateObject("ASPTut.Finance")  
 oASPTut.AnnualIntRate = CDbl(AnnualIntRate)  
 oASPTut.NumPayPeriods = CInt(NumPayPeriods)  
 oASPTut.Payment = CDbl(Payment)  

 If Not "" = PresentSavings Then oASPTut.PresentSavings = CDbl(PresentSavings)  

 oASPTut.WhenDue = WhenDue  
 FutureValue = oASPTut.CalcFutureValue  
 Response.Write "<H3 align=center>Future value = $" & FutureValue & "</H3>"  
Else  
 Response.Write "<H3 align=center>Some of your values are not numbers.</H3>"  
End If  
%>  
</FONT>  
</BODY>  
</HTML>
Show: