1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
//
// 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[] = "Inside COM, Chapter 7 Example" ;
// Version-independent ProgID
const char g_szVerIndProgID[] = "InsideCOM.Chap07" ;
// ProgID
const char g_szProgID[] = "InsideCOM.Chap07.1" ;
///////////////////////////////////////////////////////////
//
// Component
//
class CA : public IX,
public IY,
public IZ
{
public:
// IUnknown
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ;
virtual ULONG __stdcall AddRef() ;
virtual ULONG __stdcall Release() ;
// Interface IX
virtual HRESULT __stdcall Fx() { cout << "Fx" << endl ; return S_OK; }
// Interface IY
virtual HRESULT __stdcall Fy(int x)
{ cout << "Fy: " << x << endl ; return S_OK; }
// Interface IZ
virtual HRESULT __stdcall Fz(int x, int * y)
{ cout << "Fz: " << x << endl ;
*y = 2 * x + 1;
return S_OK; }
// 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 if (iid == IID_IZ)
{
*ppv = static_cast<IZ*>(this) ;
trace("Component:\t\tReturn pointer to IZ.") ;
}
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
//
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
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
//
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 ; // Reference count set to 1
// 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) hModule ;
}
return TRUE ;
}
|