Windows NT Systems Programming: Spring 2000

[ Home | Syllabus | Course Notes | Assignments | Search]


VB Automation (chapter 25)


VB calls C++ Component (EX25a)

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) hptrd_left.gif (955 bytes)
	DISP_FUNCTION(CBank, "Withdrawal", Withdrawal hptrd_left.gif (955 bytes), VT_R8, VTS_R8) hptrd_left.gif (955 bytes)
	DISP_FUNCTION(CBank, "Deposit", Deposit, VT_EMPTY, VTS_R8) hptrd_left.gif (955 bytes)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

rainbow.gif (2243 bytes)

 


Interface/Creation

BEGIN_INTERFACE_MAP(CBank, CCmdTarget) hptrd_left.gif (955 bytes)
	INTERFACE_PART(CBank, IID_IBank, Dispatch) hptrd_left.gif (955 bytes)
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) hptrd_left.gif (955 bytes)

rainbow.gif (2243 bytes)

 


Object Definition Language File (ODL)

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 hptrd_left.gif (955 bytes)
{
	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); hptrd_left.gif (955 bytes)
			[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}}
};

rainbow.gif (2243 bytes)

Visual Basic Accessing Code

Dim Bank As Object hptrd_left.gif (955 bytes)
Sub LoadBank() hptrd_left.gif (955 bytes)
    Set Bank = CreateObject("Ex25a.Bank") hptrd_left.gif (955 bytes)
End Sub

Sub UnloadBank()
    Set Bank = Nothing
End Sub

Sub DoDeposit()
    Range("D4").Select hptrd_left.gif (955 bytes)
    Bank.Deposit (ActiveCell.Value) hptrd_left.gif (955 bytes)
End Sub
Sub DoInquiry()hptrd_left.gif (955 bytes)
    Dim amt
    amt = Bank.Balance() 
    Range("G4").Select
    ActiveCell.Value = amt
End Sub

Sub DoWithdrawal() hptrd_left.gif (955 bytes)
    Range("E4").Select
    Dim amt
    amt = Bank.Withdrawal(ActiveCell.Value)
    Range("E5").Select
    ActiveCell.Value = amt
End Sub



rainbow.gif (2243 bytes)

 


Linking VB "macros" to buttons on spread sheet

ex25xls.gif (24738 bytes)


Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: March 23, 2000.