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 330 331 332
|
///////////////////////////////////////////////////////////
//
// CFactory
// - Base class for reusing a single class factory for
// all components in a DLL
//
#include <objbase.h>
#include "Registry.h"
#include "CFactory.h"
///////////////////////////////////////////////////////////
//
// Static variables
//
LONG CFactory::s_cServerLocks = 0 ; // Count of locks
HMODULE CFactory::s_hModule = NULL ; // DLL module handle
#ifdef _OUTPROC_SERVER_
DWORD CFactory::s_dwThreadID = 0 ;
#endif
///////////////////////////////////////////////////////////
//
// CFactory implementation
//
CFactory::CFactory(const CFactoryData* pFactoryData)
: m_cRef(1)
{
m_pFactoryData = pFactoryData ;
}
//
// IUnknown implementation
//
HRESULT __stdcall CFactory::QueryInterface(REFIID iid, void** ppv)
{
IUnknown* pI ;
if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
{
pI = this ;
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
pI->AddRef() ;
*ppv = pI ;
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)
{
// Aggregate only if the requested IID is IID_IUnknown.
if ((pUnknownOuter != NULL) && (iid != IID_IUnknown))
{
return CLASS_E_NOAGGREGATION ;
}
// Create the component.
CUnknown* pNewComponent ;
HRESULT hr = m_pFactoryData->CreateInstance(pUnknownOuter,
&pNewComponent) ;
if (FAILED(hr))
{
return hr ;
}
// Initialize the component.
hr = pNewComponent->Init();
if (FAILED(hr))
{
// Initialization failed. Release the component.
pNewComponent->NondelegatingRelease() ;
return hr ;
}
// Get the requested interface.
hr = pNewComponent->NondelegatingQueryInterface(iid, ppv) ;
// Release the reference held by the class factory.
pNewComponent->NondelegatingRelease() ;
return hr ;
}
// LockServer
HRESULT __stdcall CFactory::LockServer(BOOL bLock)
{
if (bLock)
{
::InterlockedIncrement(&s_cServerLocks) ;
}
else
{
::InterlockedDecrement(&s_cServerLocks) ;
}
// If this is an out-of-proc server, check to see
// whether we should shut down.
CloseExe() ; //@local
return S_OK ;
}
///////////////////////////////////////////////////////////
//
// GetClassObject
// - Create a class factory based on a CLSID.
//
HRESULT CFactory::GetClassObject(const CLSID& clsid,
const IID& iid,
void** ppv)
{
if ((iid != IID_IUnknown) && (iid != IID_IClassFactory))
{
return E_NOINTERFACE ;
}
// Traverse the array of data looking for this class ID.
for (int i = 0; i < g_cFactoryDataEntries; i++)
{
const CFactoryData* pData = &g_FactoryDataArray[i] ;
if (pData->IsClassID(clsid))
{
// Found the ClassID in the array of components we can
// create. So create a class factory for this component.
// Pass the CFactoryData structure to the class factory
// so that it knows what kind of components to create.
*ppv = (IUnknown*) new CFactory(pData) ;
if (*ppv == NULL)
{
return E_OUTOFMEMORY ;
}
return NOERROR ;
}
}
return CLASS_E_CLASSNOTAVAILABLE ;
}
//
// Determine if the component can be unloaded.
//
HRESULT CFactory::CanUnloadNow()
{
if (CUnknown::ActiveComponents() || IsLocked())
{
return S_FALSE ;
}
else
{
return S_OK ;
}
}
//
// Register all components.
//
HRESULT CFactory::RegisterAll()
{
for(int i = 0 ; i < g_cFactoryDataEntries ; i++)
{
RegisterServer(s_hModule,
*(g_FactoryDataArray[i].m_pCLSID),
g_FactoryDataArray[i].m_RegistryName,
g_FactoryDataArray[i].m_szVerIndProgID,
g_FactoryDataArray[i].m_szProgID,
*(g_FactoryDataArray[i].m_pLIBID)) ;
}
return S_OK ;
}
HRESULT CFactory::UnregisterAll()
{
for(int i = 0 ; i < g_cFactoryDataEntries ; i++)
{
UnregisterServer(*(g_FactoryDataArray[i].m_pCLSID),
g_FactoryDataArray[i].m_szVerIndProgID,
g_FactoryDataArray[i].m_szProgID) ;
}
return S_OK ;
}
#ifndef _OUTPROC_SERVER_
//////////////////////////////////////////////////////////
//
// Exported functions
//
STDAPI DllCanUnloadNow()
{
return CFactory::CanUnloadNow() ;
}
//
// Get class factory
//
STDAPI DllGetClassObject(const CLSID& clsid,
const IID& iid,
void** ppv)
{
return CFactory::GetClassObject(clsid, iid, ppv) ;
}
//
// Server registration
//
STDAPI DllRegisterServer()
{
return CFactory::RegisterAll() ;
}
STDAPI DllUnregisterServer()
{
return CFactory::UnregisterAll() ;
}
///////////////////////////////////////////////////////////
//
// DLL module information
//
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD dwReason,
void* lpReserved )
{
if (dwReason == DLL_PROCESS_ATTACH)
{
CFactory::s_hModule = (HMODULE) hModule ;
}
return TRUE ;
}
#else
//////////////////////////////////////////////////////////
//
// Out of process Server support
//
//
// Start factories
//
BOOL CFactory::StartFactories()
{
CFactoryData* pStart = &g_FactoryDataArray[0] ;
const CFactoryData* pEnd =
&g_FactoryDataArray[g_cFactoryDataEntries - 1] ;
for(CFactoryData* pData = pStart ; pData <= pEnd ; pData++)
{
// Initialize the class factory pointer and cookie.
pData->m_pIClassFactory = NULL ;
pData->m_dwRegister = NULL ;
// Create the class factory for this component.
IClassFactory* pIFactory = new CFactory(pData) ;
// Register the class factory.
DWORD dwRegister ;
HRESULT hr = ::CoRegisterClassObject(
*pData->m_pCLSID,
static_cast<IUnknown*>(pIFactory),
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE,
// REGCLS_MULTI_SEPARATE, //@Multi
&dwRegister) ;
if (FAILED(hr))
{
pIFactory->Release() ;
return FALSE ;
}
// Set the data.
pData->m_pIClassFactory = pIFactory ;
pData->m_dwRegister = dwRegister ;
}
return TRUE ;
}
//
// Stop factories
//
void CFactory::StopFactories()
{
CFactoryData* pStart = &g_FactoryDataArray[0] ;
const CFactoryData* pEnd =
&g_FactoryDataArray[g_cFactoryDataEntries - 1] ;
for (CFactoryData* pData = pStart ; pData <= pEnd ; pData++)
{
// Get the magic cookie and stop the factory from running.
DWORD dwRegister = pData->m_dwRegister ;
if (dwRegister != 0)
{
::CoRevokeClassObject(dwRegister) ;
}
// Release the class factory.
IClassFactory* pIFactory = pData->m_pIClassFactory ;
if (pIFactory != NULL)
{
pIFactory->Release() ;
}
}
}
#endif //_OUTPROC_SERVER_
|