Build a VB6 ActiveX DLL

In this example, you'll build a VB6-based ActiveX DLL (a COM component) that functions as a Web Service. For example, the VB6 DLL MortgageCalculatorWS functions in exactly the same way as the Mortgage Payment Calculator Web Service—it exposes two methods, called getPayment and totalPaymentAmount, that accept the same parameter types as the ch21_payment Web Service class methods. The VB6 code is shown in Listing 22.3. To create the DLL, open a new VB6 ActiveX DLL project, rename the project MortgageCalculatorWS (the WS stands for Web Service, but the extra text makes the fully qualified method names too long for COM), and rename the default class ch22_payment. I translated most of the code from the ch21-payment.asmx.cs class, changing syntax as needed to create equivalent VB6 code.

Listing 22.3: The VB6 MortgageCalculatorWS Project's ch22_payment Class Contains the Mortgage Payment Calculator getPayment and totalPaymentAmount Methods (ch22_Payment.cls)

Public Function getPayment

(ByVal AnnualPercentageRate As Double, ByVal TotalCost As Double, _

ByVal DurationInYears As Integer) As String Dim payment As Double

' divide the APR by 100 to get a decimal percentage, if necessary

If AnnualPercentageRate > 1 Then

AnnualPercentageRate = AnnualPercentageRate / 100 End If

' and then divide by 12 for the number of months ' in a year

' to get the percentage interest rate per payment. ' Multiply DurationInYears by 12 to get the total ' number of payments.

payment = Pmt(AnnualPercentageRate / 12, DurationInYears * 12, TotalCost)

' the Pmt function returns the payment as a ' negative number payment = Abs(payment)

' format the result and return it getPayment = Format$(payment, "$###,###,##0.00") End Function

Public Function totalPaymentAmount

(ByVal MonthlyPayment As Double, _

ByVal DurationInYears As Integer) As String totalPaymentAmount = Format$(MonthlyPayment * (DurationInYears * 12), "$###,###,##0.00") End Function

Compile the DLL and save your work.

Was this article helpful?

0 0

Post a comment