<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->voidCEx24cView::OnTestSpaceship()
{
CLSIDclsid;
LPCLASSFACTORYpClf;
LPUNKNOWNpUnk;
IMotion*pMot;
IVisual*pVis;
HRESULThr;
if((hr=::CLSIDFromProgID(L"Spaceship",&clsid))!=NOERROR)
{
TRACE("unabletofindProgramID--error=%x/n",hr);
return;
}
if((hr=::CoGetClassObject(clsid,CLSCTX_INPROC_SERVER,
NULL,IID_IClassFactory,(void**)&pClf))!=NOERROR){;
TRACE("unabletofindCLSID--error=%x/n",hr);
return;
}
pClf->CreateInstance(NULL,IID_IUnknown,(void**)&pUnk);
pUnk->QueryInterface(IID_IMotion,(void**)&pMot);//Allthree
pMot->QueryInterface(IID_IVisual,(void**)&pVis);//pointers
//shouldwork
TRACE("main:pUnk=%p,pMot=%p,pDis=%p/n",pUnk,pMot,pVis);
//Testalltheinterfacevirtualfunctions
pMot->Fly();
intnPos=pMot->GetPosition();
TRACE("nPos=%d/n",nPos);
pVis->Display();
pClf->Release();
pUnk->Release();
pMot->Release();
pVis->Release();
AfxMessageBox("Testsucceeded.SeeDebugwindowforoutput.");
}
{
CLSIDclsid;
LPCLASSFACTORYpClf;
LPUNKNOWNpUnk;
IMotion*pMot;
IVisual*pVis;
HRESULThr;
if((hr=::CLSIDFromProgID(L"Spaceship",&clsid))!=NOERROR)
{
TRACE("unabletofindProgramID--error=%x/n",hr);
return;
}
if((hr=::CoGetClassObject(clsid,CLSCTX_INPROC_SERVER,
NULL,IID_IClassFactory,(void**)&pClf))!=NOERROR){;
TRACE("unabletofindCLSID--error=%x/n",hr);
return;
}
pClf->CreateInstance(NULL,IID_IUnknown,(void**)&pUnk);
pUnk->QueryInterface(IID_IMotion,(void**)&pMot);//Allthree
pMot->QueryInterface(IID_IVisual,(void**)&pVis);//pointers
//shouldwork
TRACE("main:pUnk=%p,pMot=%p,pDis=%p/n",pUnk,pMot,pVis);
//Testalltheinterfacevirtualfunctions
pMot->Fly();
intnPos=pMot->GetPosition();
TRACE("nPos=%d/n",nPos);
pVis->Display();
pClf->Release();
pUnk->Release();
pMot->Release();
pVis->Release();
AfxMessageBox("Testsucceeded.SeeDebugwindowforoutput.");
}
// interface.h
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->structIMotion:publicIUnknown
{
STDMETHOD_(void,Fly)()=0;
STDMETHOD_(int&,GetPosition)()=0;
};
structIVisual:publicIUnknown
{
STDMETHOD_(void,Display)()=0;
};
{
STDMETHOD_(void,Fly)()=0;
STDMETHOD_(int&,GetPosition)()=0;
};
structIVisual:publicIUnknown
{
STDMETHOD_(void,Display)()=0;
};
// Spaceship.h : header file


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
voidITrace(REFIIDiid,constchar*str);
/////////////////////////////////////////////////////////////////////////////
//CSpaceshipcommandtarget
classCSpaceship:publicCCmdTarget
{
DECLARE_DYNCREATE(CSpaceship)
private:
intm_nPosition;//Wecanaccessthisfromalltheinterfaces
intm_nAcceleration;
intm_nColor;
protected:
CSpaceship();//protectedconstructorusedbydynamiccreation
//Attributes
public:
//Operations
public:
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CSpaceship)
public:
virtualvoidOnFinalRelease();
//}}AFX_VIRTUAL
//Implementation
protected:
virtual~CSpaceship();
//Generatedmessagemapfunctions
//{{AFX_MSG(CSpaceship)
//NOTE-theClassWizardwilladdandremovememberfunctionshere.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
DECLARE_OLECREATE(CSpaceship)
BEGIN_INTERFACE_PART(Motion,IMotion)
STDMETHOD_(void,Fly)();
STDMETHOD_(int&,GetPosition)();
END_INTERFACE_PART(Motion)
BEGIN_INTERFACE_PART(Visual,IVisual)
STDMETHOD_(void,Display)();
END_INTERFACE_PART(Visual)
DECLARE_INTERFACE_MAP()
};
// Spaceship.cpp : implementation file


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//////////////////////////////////////////////////////////////////////
//CSpaceship
//{692D03A4-C689-11CE-B337-88EA36DE9E4E}
staticconstIIDIID_IMotion=
{0x692d03a4,0xc689,0x11ce,
{0xb3,0x37,0x88,0xea,0x36,0xde,0x9e,0x4e}};
//{692D03A5-C689-11CE-B337-88EA36DE9E4E}
staticconstIIDIID_IVisual=
{0x692d03a5,0xc689,0x11ce,
{0xb3,0x37,0x88,0xea,0x36,0xde,0x9e,0x4e}};
IMPLEMENT_DYNCREATE(CSpaceship,CCmdTarget)
CSpaceship::CSpaceship()
{
TRACE("CSpaceshipctor/n");
m_nPosition=100;
m_nAcceleration=101;
m_nColor=102;
//TokeeptheapplicationrunningaslongasanOLEautomation
//objectisactive,theconstructorcallsAfxOleLockApp.
AfxOleLockApp();
}
CSpaceship::~CSpaceship()
{
TRACE("CSpaceshipdtor/n");
//Toterminatetheapplicationwhenallobjectscreatedwith
//OLEautomation,thedestructorcallsAfxOleUnlockApp.
AfxOleUnlockApp();
}
voidCSpaceship::OnFinalRelease()
{
//Whenthelastreferenceforanautomationobjectisreleased
//OnFinalReleaseiscalled.Thisimplementationdeletesthe
//object.Addadditionalcleanuprequiredforyourobjectbefore
//deletingitfrommemory.
deletethis;
}
BEGIN_MESSAGE_MAP(CSpaceship,CCmdTarget)
//{{AFX_MSG_MAP(CSpaceship)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//}}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()
//{692D03A3-C689-11CE-B337-88EA36DE9E4E}
IMPLEMENT_OLECREATE(CSpaceship,"Spaceship",0x692d03a3,0xc689,
0x11ce,0xb3,0x37,0x88,0xea,0x36,0xde,
0x9e,0x4e)
STDMETHODIMP_(ULONG)CSpaceship::XMotion::AddRef()
{
TRACE("CSpaceship::XMotion::AddRef/n");
METHOD_PROLOGUE(CSpaceship,Motion)
returnpThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG)CSpaceship::XMotion::Release()
{
TRACE("CSpaceship::XMotion::Release/n");
METHOD_PROLOGUE(CSpaceship,Motion)
returnpThis->ExternalRelease();
}
STDMETHODIMPCSpaceship::XMotion::QueryInterface(
REFIIDiid,LPVOID*ppvObj)
{
ITrace(iid,"CSpaceship::XMotion::QueryInterface");
METHOD_PROLOGUE(CSpaceship,Motion)
returnpThis->ExternalQueryInterface(&iid,ppvObj);
}
STDMETHODIMP_(void)CSpaceship::XMotion::Fly()
{
TRACE("CSpaceship::XMotion::Fly/n");
METHOD_PROLOGUE(CSpaceship,Motion)
TRACE("m_nPosition=%d/n",pThis->m_nPosition);
TRACE("m_nAcceleration=%d/n",pThis->m_nAcceleration);
return;
}
STDMETHODIMP_(int&)CSpaceship::XMotion::GetPosition()
{
TRACE("CSpaceship::XMotion::GetPosition/n");
METHOD_PROLOGUE(CSpaceship,Motion)
TRACE("m_nPosition=%d/n",pThis->m_nPosition);
TRACE("m_nAcceleration=%d/n",pThis->m_nAcceleration);
returnpThis->m_nPosition;
}
//////////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG)CSpaceship::XVisual::AddRef()
{
TRACE("CSpaceship::XVisual::AddRef/n");
METHOD_PROLOGUE(CSpaceship,Visual)
returnpThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG)CSpaceship::XVisual::Release()
{
TRACE("CSpaceship::XVisual::Release/n");
METHOD_PROLOGUE(CSpaceship,Visual)
returnpThis->ExternalRelease();
}
STDMETHODIMPCSpaceship::XVisual::QueryInterface(
REFIIDiid,LPVOID*ppvObj)
{
ITrace(iid,"CSpaceship::XVisual::QueryInterface");
METHOD_PROLOGUE(CSpaceship,Visual)
returnpThis->ExternalQueryInterface(&iid,ppvObj);
}
STDMETHODIMP_(void)CSpaceship::XVisual::Display()
{
TRACE("CSpaceship::XVisual::Display/n");
METHOD_PROLOGUE(CSpaceship,Visual)
TRACE("m_nPosition=%d/n",pThis->m_nPosition);
TRACE("m_nColor=%d/n",pThis->m_nColor);
}
//////////////////////////////////////////////////////////////////////
voidITrace(REFIIDiid,constchar*str)
{
OLECHAR*lpszIID;
::StringFromIID(iid,&lpszIID);
CStringstrTemp=(LPCWSTR)lpszIID;
TRACE("%s-%s/n",(constchar*)strTemp,(constchar*)str);
AfxFreeTaskMem(lpszIID);
}
正规DLL部分代码
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
/////////////////////////////////////////////////////////////////////////////
//CEx24bAppinitialization
BOOLCEx24bApp::InitInstance()
{
//RegisterallOLEserver(factories)asrunning.Thisenablesthe
//OLElibrariestocreateobjectsfromotherapplications.
COleObjectFactory::RegisterAll();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
//Specialentrypointsrequiredforinprocservers
STDAPIDllGetClassObject(REFCLSIDrclsid,REFIIDriid,LPVOID*ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
returnAfxDllGetClassObject(rclsid,riid,ppv);
}
STDAPIDllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
returnAfxDllCanUnloadNow();
}
//byexportingDllRegisterServer,youcanuseregsvr.exe
STDAPIDllRegisterServer(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
returnS_OK;
}
/////////////////////////////////////////////////////////////////////////////
//CEx24bAppinitialization
BOOLCEx24bApp::InitInstance()
{
//RegisterallOLEserver(factories)asrunning.Thisenablesthe
//OLElibrariestocreateobjectsfromotherapplications.
COleObjectFactory::RegisterAll();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
//Specialentrypointsrequiredforinprocservers
STDAPIDllGetClassObject(REFCLSIDrclsid,REFIIDriid,LPVOID*ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
returnAfxDllGetClassObject(rclsid,riid,ppv);
}
STDAPIDllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
returnAfxDllCanUnloadNow();
}
//byexportingDllRegisterServer,youcanuseregsvr.exe
STDAPIDllRegisterServer(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
returnS_OK;
}
要想运行客户端和组件程序,必须先注册组件来更新注册表,可以使用如下的代码:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->BOOLCRegCompApp::InitInstance()
{
SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));
//makesuretosetExploreroptionstoallowDLLstobevisible
CSpecialFileDialogdlgFile(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"OCXfiles(*.ocx)|*.ocx|DLLfiles(*.dll)|*.dll||");
dlgFile.m_ofn.lpstrTitle="注册OCX/DLLFile";
if(dlgFile.DoModal()!=IDOK)returnFALSE;
CStringstrDllPath=dlgFile.GetPathName();//获取文件名
//thiswouldn'tworkforadynamicallylinkedRegularDLL
HINSTANCEh=::LoadLibrary(strDllPath);//加载dll
if(h==NULL)
{
CStringmsg;
msg.Format("Failedtofindserver%s",strDllPath);
AfxMessageBox(msg);
returnFALSE;
}
FARPROCpFunc=::GetProcAddress((HMODULE)h,"DllRegisterServer");
if(pFunc==NULL){
AfxMessageBox("FailedtofindDllRegisterServerfunction");
returnFALSE;
}
(*pFunc)();//callthefunctiontoregistertheserver注册
AfxMessageBox("ServerregisteredOK");
returnFALSE;
}
{
SetRegistryKey(_T("LocalAppWizard-GeneratedApplications"));
//makesuretosetExploreroptionstoallowDLLstobevisible
CSpecialFileDialogdlgFile(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"OCXfiles(*.ocx)|*.ocx|DLLfiles(*.dll)|*.dll||");
dlgFile.m_ofn.lpstrTitle="注册OCX/DLLFile";
if(dlgFile.DoModal()!=IDOK)returnFALSE;
CStringstrDllPath=dlgFile.GetPathName();//获取文件名
//thiswouldn'tworkforadynamicallylinkedRegularDLL
HINSTANCEh=::LoadLibrary(strDllPath);//加载dll
if(h==NULL)
{
CStringmsg;
msg.Format("Failedtofindserver%s",strDllPath);
AfxMessageBox(msg);
returnFALSE;
}
FARPROCpFunc=::GetProcAddress((HMODULE)h,"DllRegisterServer");
if(pFunc==NULL){
AfxMessageBox("FailedtofindDllRegisterServerfunction");
returnFALSE;
}
(*pFunc)();//callthefunctiontoregistertheserver注册
AfxMessageBox("ServerregisteredOK");
returnFALSE;
}