Export (0) Print
Expand All

COM Interop Sample: COM Client and Windows Server 2003 

This sample demonstrates the interoperation of a COM Client and a Windows Server 2003 that performs mortgage calculations. In this example, the client creates and calls an instance of the managed Loan class, passes four arguments (one of those four being equal to zero) to the instance, and displays the computations. Code examples from this sample appear throughout this section.

COM Client

// ConLoan.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#import "..\LoanLib\LoanLib.tlb" raw_interfaces_only
using namespace LoanLib;

int main(int argc, char* argv[])
{
    HRESULT hr = CoInitialize(NULL);

    ILoanPtr pILoan(__uuidof(Loan));

    if (argc < 5) 
    {
        printf("Usage: ConLoan Balance Rate Term Payment\n");
        printf("    Either Balance, Rate, Term, or Payment must be 0\n");
        return -1;
    }

    double openingBalance = atof(argv[1]);
    double rate = atof(argv[2])/100.0;
    short  term = atoi(argv[3]);
    double payment = atof(argv[4]);

    pILoan->put_OpeningBalance(openingBalance);
    pILoan->put_Rate(rate);
    pILoan->put_Term(term);
    pILoan->put_Payment(payment);

    if (openingBalance == 0.00) 
         pILoan->ComputeOpeningBalance(&openingBalance);
    if (rate == 0.00) pILoan->ComputeRate(&rate);
    if (term == 0) pILoan->ComputeTerm(&term);
    if (payment == 0.00) pILoan->ComputePayment(&payment);

    printf("Balance = %.2f\n", openingBalance);
    printf("Rate    = %.1f%%\n", rate*100);
    printf("Term    = %.2i\n", term);
    printf("Payment = %.2f\n", payment);

    VARIANT_BOOL MorePmts;
    double Balance = 0.0;
    double Principal = 0.0;
    double Interest = 0.0;

    printf("%4s%10s%12s%10s%12s\n", "Nbr", "Payment", "Principal", "Interest", "Balance");
    printf("%4s%10s%12s%10s%12s\n", "---", "-------", "---------", 
"--------", "-------");

    pILoan->GetFirstPmtDistribution(payment, &Balance, &Principal, &Interest, &MorePmts);

    for (short PmtNbr = 1; MorePmts; PmtNbr++) 
    {
        printf("%4i%10.2f%12.2f%10.2f%12.2f\n",
        PmtNbr, payment, Principal, Interest, Balance);
      
        pILoan->GetNextPmtDistribution(payment, &Balance, &Principal, &Interest, &MorePmts); 
    }

    CoUninitialize();
    return 0;
}

Windows Server 2003

using System;
using System.Reflection;

[assembly:AssemblyKeyFile("sample.snk")]
namespace LoanLib {
   
    public interface ILoan {
        double OpeningBalance{get; set;}
        double Rate{get; set;}
        double Payment{get; set;}   
        short  Term{get; set;}
        String RiskRating{get; set;}
      
        double ComputePayment();
        double ComputeOpeningBalance();
        double ComputeRate();
        short ComputeTerm();
        bool GetFirstPmtDistribution(double PmtAmt, ref double Balance,
            out double PrinPortion, out double IntPortion);
        bool GetNextPmtDistribution(double PmtAmt, ref double Balance,
            out double PrinPortion, out double IntPortion);
    }

    public class Loan : ILoan {
        private double openingBalance;
        private double rate;
        private double payment;
        private short  term;
        private String riskRating;      
      
        public double OpeningBalance {
            get { return openingBalance; }
            set { openingBalance = value; }
        }
      
        public double Rate {
            get { return rate; }
            set { rate = value; }
        }
      
        public double Payment {
            get { return payment; }
            set { payment = value; }
        }
         
        public short Term {
            get { return term; }
            set { term = value; }
        }

        public String RiskRating {
            get { return riskRating; }
            set { riskRating = value; }
        }

        public double ComputePayment() {
             Payment = Util.Round(OpeningBalance * (Rate / (1 – 
                        Math.Pow((1 + Rate), -Term))), 2);
             return Payment;
        }

        public double ComputeOpeningBalance() {
            OpeningBalance = Util.Round(Payment / (Rate / (1 - Math.Pow((1 
                              + Rate), -Term))), 2);
             return OpeningBalance;
        }

        public double ComputeRate() {
            double DesiredPayment = Payment;

            for (Rate = 0.001; Rate < 28.0; Rate += 0.001) {
                Payment = Util.Round(OpeningBalance * (Rate / (1 – 
                           Math.Pow((1 + Rate), -Term))), 2);

                if (Payment >= DesiredPayment)
                    break;
            }
            return Rate;   
        }

        public short ComputeTerm() {
            double DesiredPayment = Payment;

            for (Term = 1; Term < 480 ; Term ++) {
                Payment = Util.Round(OpeningBalance * (Rate / (1 – 
                           Math.Pow((1 + Rate), -Term))),2);

                if (Payment <= DesiredPayment)
                    break;
            }

            return Term;   
        }

        public bool GetFirstPmtDistribution(double PmtAmt, ref double 
            Balance, out double PrinPortion, out double IntPortion) {
             Balance = OpeningBalance;
             return GetNextPmtDistribution(PmtAmt, ref Balance, out 
             PrinPortion, out IntPortion); 
        }

        public bool GetNextPmtDistribution(double PmtAmt, ref double 
           Balance, out double PrinPortion, out double IntPortion) {
            IntPortion = Util.Round(Balance * Rate, 2);
            PrinPortion = Util.Round(PmtAmt - IntPortion,2);
            Balance = Util.Round(Balance - PrinPortion,2);

            if (Balance <= 0.0) 
                return false;

            return true;
        }
     }

    internal class Util {
        public static double Round(double value, short digits) {
            double factor = Math.Pow(10, digits);
            return Math.Round(value * factor) / factor;
         }
    }
}

See Also

Community Additions

ADD
Show:
© 2014 Microsoft