[ Home | Syllabus | Course Notes | Assignments | Search]
void CEx24cView::OnTestSpaceship( ) { CLSID clsid; LPCLASSFACTORY pClf; LPUNKNOWN pUnk; IMotion* pMot; IVisual* pVis; HRESULT hr; if ((hr = ::CLSIDFromProgID(L"Spaceship", &clsid)) != NOERROR) { return; } if ((hr = ::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (void **) &pClf)) != NOERROR) {; return; } pClf->CreateInstance(NULL, IID_IUnknown, (void**) &pUnk); pUnk->QueryInterface(IID_IMotion, (void**) &pMot); // All three pMot->QueryInterface(IID_IVisual, (void**) &pVis); // pointers // should work // Test all the interface virtual functions pMot->Fly(); int nPos = pMot->GetPosition(); pVis->Display(); pClf->Release(); pUnk->Release(); pMot->Release(); pVis->Release(); }
CSpaceship::CSpaceship() { m_nPosition = 100; m_nAcceleration = 101; m_nColor = 102; // To keep the application running as long as an OLE automation // object is active, the constructor calls AfxOleLockApp. AfxOleLockApp(); }
BEGIN_MESSAGE_MAP(CSpaceship, CCmdTarget) //{{AFX_MSG_MAP(CSpaceship) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() BEGIN_INTERFACE_MAP(CSpaceship, CCmdTarget) INTERFACE_PART(CSpaceship, IID_IMotion, Motion) INTERFACE_PART(CSpaceship, IID_IVisual, Visual) END_INTERFACE_MAP()
STDMETHODIMP_(void) CSpaceship::XMotion::Fly() { METHOD_PROLOGUE(CSpaceship, Motion) m_nPosition++; // move the spaceship return; } STDMETHODIMP_(int&) CSpaceship::XMotion::GetPosition() { METHOD_PROLOGUE(CSpaceship, Motion) return pThis->m_nPosition; }
STDMETHODIMP_(void) CSpaceship::XVisual::Display() { METHOD_PROLOGUE(CSpaceship, Visual) TRACE("m_nPosition = %d\n", pThis->m_nPosition); TRACE("m_nColor = %d\n", pThis->m_nColor); }