[size=x-large]真实的COM,程序同 inside com,Chapter07
1. 客户端:[/size]
[size=x-large]2. 组件定义:
此DLL中同时有,CA 和 CFactory 两个组件,一个为普通组件,一个为创建此组件的类厂组件
[/size]
[size=x-large]3. 省略的注册程序,注册之后,DLL 可以不现在在exe 同一文件夹下[/size]
1. 客户端:[/size]
//
// Client.cpp - client implementation
/*
CoCreateInstance:
在COM库中包含一个用于创建组件的名为CoCreatelnstancae的函数。此函数需要一
个CLSID参数,在此基础上创建相应组件的一个实例,并返回此组件实例的某个接口。
*/
//
#include <iostream>
#include <objbase.h>
#include "Iface.h"
using namespace std;
void trace(const char* msg) { cout << "Client: \t\t" << msg << endl ;}
//
// main function
//
int main()
{
// Initialize COM Library
CoInitialize(NULL) ;
trace("Call CoCreateInstance to create") ;
trace(" component and get interface IX.") ;
IX* pIX = NULL ;
HRESULT hr = ::CoCreateInstance(CLSID_Component1,
NULL,
CLSCTX_INPROC_SERVER,
IID_IX,
(void**)&pIX) ;
if (SUCCEEDED(hr))
{
trace("Succeeded getting IX.") ;
pIX->Fx() ; // Use interface IX.
trace("Ask for interface IY.") ;
IY* pIY = NULL ;
hr = pIX->QueryInterface(IID_IY, (void**)&pIY) ;
if (SUCCEEDED(hr))
{
trace("Succeeded getting IY.") ;
pIY->Fy() ; // Use interface IY.
pIY->Release() ;
trace("Release IY interface.") ;
}
else
{
trace("Could not get interface IY.") ;
}
trace("Ask for interface IZ.") ;
IZ* pIZ = NULL ;
hr = pIX->QueryInterface(IID_IZ, (void**)&pIZ) ;
if (SUCCEEDED(hr))
{
trace("Succeeded in getting interface IZ.") ;
pIZ->Fz() ;
pIZ->Release() ;
trace("Release IZ interface.") ;
}
else
{
trace("Could not get interface IZ.") ;
}
trace("Release IX interface.") ;
pIX->Release() ;
}
else
{
cout << "Client: \t\tCould not create component. hr = "
<< hex << hr << endl ;
}
// Uninitialize COM Library
CoUninitialize() ;
system("pause");
return 0 ;
}
[size=x-large]2. 组件定义:
此DLL中同时有,CA 和 CFactory 两个组件,一个为普通组件,一个为创建此组件的类厂组件
[/size]
//
// Cmpnt.cpp
//
#include <iostream.h>
#include <objbase.h>
#include "Iface.h" // Interface declarations
#include "Registry.h" // Registry helper functions
// Trace function
void trace(const char* msg) { cout << msg << endl ;}
///
//
// Global variables
//
static HMODULE g_hModule = NULL ; // DLL module handle
static long g_cComponents = 0 ; // Count of active components
static long g_cServerLocks = 0 ; // Count of locks
// Friendly name of component
const char g_szFriendlyName[] = "FirstCmpnt.Demo" ;
// Version-independent ProgID
const char g_szVerIndProgID[] = "Cmpnt.Demo" ;
// ProgID
const char g_szProgID[] = "Cmpnt.Demo.1" ;
///
//
// Component CA组件
//
class CA : public IX,
public IY
{
public:
// IUnknown
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ;
virtual ULONG __stdcall AddRef() ;
virtual ULONG __stdcall Release() ;
// Interface IX
virtual void __stdcall Fx() { cout << "Fx" << endl ;}
// Interface IY
virtual void __stdcall Fy() { cout << "Fy" << endl ;}
// Constructor
CA() ;
// Destructor
~CA() ;
private:
// Reference count
long m_cRef ;
} ;
//
// Constructor
//
CA::CA() : m_cRef(1)
{
InterlockedIncrement(&g_cComponents) ;
}
//
// Destructor
//
CA::~CA()
{
InterlockedDecrement(&g_cComponents) ;
trace("Component:\t\tDestroy self.") ;
}
//
// IUnknown implementation
//
HRESULT __stdcall CA::QueryInterface(const IID& iid, void** ppv)
{
if (iid == IID_IUnknown)
{
*ppv = static_cast<IX*>(this) ;
}
else if (iid == IID_IX)
{
*ppv = static_cast<IX*>(this) ;
trace("Component:\t\tReturn pointer to IX.") ;
}
else if (iid == IID_IY)
{
*ppv = static_cast<IY*>(this) ;
trace("Component:\t\tReturn pointer to IY.") ;
}
else
{
*ppv = NULL ;
return E_NOINTERFACE ;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
return S_OK ;
}
ULONG __stdcall CA::AddRef()
{
return InterlockedIncrement(&m_cRef) ;
}
ULONG __stdcall CA::Release()
{
if (InterlockedDecrement(&m_cRef) == 0)
{
delete this ;
return 0 ;
}
return m_cRef ;
}
///
//
// Class factory 类厂:
/*
类厂是组件,唯一功能就是创建其他的组件。
更精确地讲,某个特定的类。
创建只同某个特定的CLSID相应的组件。客户可以通过
创建组件的标准接口是IClassFactory。
*/
//
class CFactory : public IClassFactory
{
public:
// IUnknown
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ;
virtual ULONG __stdcall AddRef() ;
virtual ULONG __stdcall Release() ;
// Interface IClassFactory, 实现IClassFactory 接口的函数
virtual HRESULT __stdcall CreateInstance(IUnknown* pUnknownOuter,
const IID& iid,
void** ppv) ;
virtual HRESULT __stdcall LockServer(BOOL bLock) ;
// Constructor
CFactory() : m_cRef(1) {}
// Destructor
~CFactory() { trace("Class factory:\t\tDestroy self.") ;}
private:
long m_cRef ;
} ;
//
// Class factory IUnknown implementation
//
HRESULT __stdcall CFactory::QueryInterface(const IID& iid, void** ppv)
{
if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
{
*ppv = static_cast<IClassFactory*>(this) ;
}
else
{
*ppv = NULL ;
return E_NOINTERFACE ;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
return S_OK ;
}
ULONG __stdcall CFactory::AddRef()
{
return InterlockedIncrement(&m_cRef) ;
}
ULONG __stdcall CFactory::Release()
{
if (InterlockedDecrement(&m_cRef) == 0)
{
delete this ;
return 0 ;
}
return m_cRef ;
}
//
// IClassFactory implementation,实现
//
HRESULT __stdcall CFactory::CreateInstance(IUnknown* pUnknownOuter,
const IID& iid,
void** ppv)
{
trace("Class factory:\t\tCreate component.") ;
// Cannot aggregate.
if (pUnknownOuter != NULL)
{
return CLASS_E_NOAGGREGATION ;
}
// Create component.
CA* pA = new CA ;
if (pA == NULL)
{
return E_OUTOFMEMORY ;
}
// Get the requested interface.
HRESULT hr = pA->QueryInterface(iid, ppv) ;
// Release the IUnknown pointer.
// (If QueryInterface failed, component will delete itself.)
pA->Release() ;
return hr ;
}
// LockServer
HRESULT __stdcall CFactory::LockServer(BOOL bLock)
{
if (bLock)
{
InterlockedIncrement(&g_cServerLocks) ;
}
else
{
InterlockedDecrement(&g_cServerLocks) ;
}
return S_OK ;
}
///
//
// Exported functions
//
//
// Can DLL unload now?
//
STDAPI DllCanUnloadNow()
{
if ((g_cComponents == 0) && (g_cServerLocks == 0))
{
return S_OK ;
}
else
{
return S_FALSE ;
}
}
//
// Get class factory,获得类厂函数 CoGetClassObject
/*
在创建类厂时,COM库的 CoGetClassObject被调用,接着 DLL的DllGetClassObject被调用
其中:CoGetClassObject与CoCreateInstance相识
最大的区别在于 CoGetClassObject 返回是的指向所需组件的类厂而不是指向组件本身的一个指针。
客户可以用 CoGetClassObject所返回的指针一来创建所需的组件
*/
//
STDAPI DllGetClassObject(const CLSID& clsid,
const IID& iid,
void** ppv)
{
trace("DllGetClassObject:\tCreate class factory.") ;
// Can we create this component?
if (clsid != CLSID_Component1)
{
return CLASS_E_CLASSNOTAVAILABLE ;
}
// Create class factory.
CFactory* pFactory = new CFactory ; // No AddRef in constructor
if (pFactory == NULL)
{
return E_OUTOFMEMORY ;
}
// Get requested interface.
HRESULT hr = pFactory->QueryInterface(iid, ppv) ;
pFactory->Release() ;
return hr ;
}
//
// Server registration
//
STDAPI DllRegisterServer()
{
return RegisterServer(g_hModule,
CLSID_Component1,
g_szFriendlyName,
g_szVerIndProgID,
g_szProgID) ;
}
//
// Server unregistration
//
STDAPI DllUnregisterServer()
{
return UnregisterServer(CLSID_Component1,
g_szVerIndProgID,
g_szProgID) ;
}
///
//
// DLL module information
//
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD dwReason,
void* lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hModule = hModule ;
}
return TRUE ;
}
[size=x-large]3. 省略的注册程序,注册之后,DLL 可以不现在在exe 同一文件夹下[/size]