[ Home | Syllabus | Course Notes | Assignments | Search]
Here are the "worker" functions for a simple bank object with data member "double m_dBalance"
double CBank::Withdrawal(double dAmount)
{
if (dAmount < 0.0) {
return 0.0;
}
if (dAmount <= m_dBalance) {
m_dBalance -= dAmount;
return dAmount;
}
double dTemp = m_dBalance;
m_dBalance = 0.0;
return dTemp;
}
void CBank::Deposit(double dAmount)
{
if (dAmount < 0.0) {
return;
}
m_dBalance += dAmount;
}
double CBank::GetBalance()
{
return m_dBalance;
}
void CBank::SetBalance(double newValue)
// we need to provide for automation interface since it is expected (see below)
{
TRACE("Sorry, Dave, I can't do that!\n");
}
Use MFC Macros to set up Dispatch Map:
BEGIN_DISPATCH_MAP(CBank, CCmdTarget)
//{{AFX_DISPATCH_MAP(CBank)
DISP_PROPERTY_EX(CBank, "Balance", GetBalance, SetBalance, VT_R8)
DISP_FUNCTION(CBank, "Withdrawal", Withdrawal
, VT_R8, VTS_R8)
DISP_FUNCTION(CBank, "Deposit", Deposit, VT_EMPTY, VTS_R8)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
BEGIN_INTERFACE_MAP(CBank, CCmdTarget)INTERFACE_PART(CBank, IID_IBank, Dispatch)
END_INTERFACE_MAP() // {632B1E4C-F287-11CE-B5E3-00AA005B1574} // uuid - used below IMPLEMENT_OLECREATE2(CBank, "ex25a.Bank", 0x632b1e4c, 0xf287, 0x11ce, 0xb5, 0xe3, 0x0, 0xaa, 0x0, 0x5b, 0x15, 0x74)
Extension to IDL file
// ex25a.odl : type library source for ex25a.exe // This file will be processed by the MIDL compiler to produce the // type library (ex25a.tlb). [ uuid(A9515AA2-5B85-11D0-848F-00400526305B), version(1.0) ] library Ex25a{ importlib("stdole32.tlb"); // Primary dispatch interface for CBank [ uuid(A9515AB6-5B85-11D0-848F-00400526305B) ] dispinterface IBank { properties: // NOTE - ClassWizard will maintain property information here. // Use extreme caution when editing this section. //{{AFX_ODL_PROP(CBank) [id(1)] double Balance; //}}AFX_ODL_PROP methods: // NOTE - ClassWizard will maintain method information here. // Use extreme caution when editing this section. //{{AFX_ODL_METHOD(CBank) [id(2)] double Withdrawal(double dAount);
[id(3)] void Deposit(double dAmount); //}}AFX_ODL_METHOD }; // Class information for CBank [ uuid(632B1E4C-F287-11CE-B5E3-00AA005B1574) ] coclass Bank { [default] dispinterface IBank; }; //{{AFX_APPEND_ODL}} };
Dim Bank As ObjectSub LoadBank()
Set Bank = CreateObject("Ex25a.Bank")
End Sub Sub UnloadBank() Set Bank = Nothing End Sub Sub DoDeposit() Range("D4").Select
Bank.Deposit (ActiveCell.Value)
End Sub
Sub DoInquiry()Dim amt amt = Bank.Balance() Range("G4").Select ActiveCell.Value = amt End Sub Sub DoWithdrawal()
Range("E4").Select Dim amt amt = Bank.Withdrawal(ActiveCell.Value) Range("E5").Select ActiveCell.Value = amt End Sub

Use "Forms" toolbar to create button
Assign it to the corresponding VB function
(called a macro)