xref: /AOO41X/main/shell/source/win32/shlxthandler/prophdl/propertyhdl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2010 by Sun Microsystems, Inc.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_shell.hxx"
30*cdf0e10cSrcweir #include "internal/global.hxx"
31*cdf0e10cSrcweir #include "internal/PropertyHdl.hxx"
32*cdf0e10cSrcweir #include "internal/fileextensions.hxx"
33*cdf0e10cSrcweir #include "internal/metainforeader.hxx"
34*cdf0e10cSrcweir #include "internal/utilities.hxx"
35*cdf0e10cSrcweir #include "internal/config.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <propkey.h>
38*cdf0e10cSrcweir #include <propvarutil.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <malloc.h>
41*cdf0e10cSrcweir #include <strsafe.h>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "internal/stream_helper.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //---------------------------
46*cdf0e10cSrcweir // Module global
47*cdf0e10cSrcweir //---------------------------
48*cdf0e10cSrcweir long g_DllRefCnt = 0;
49*cdf0e10cSrcweir HINSTANCE g_hModule = NULL;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //
52*cdf0e10cSrcweir // Map of property keys to the locations of their value(s) in the .??? XML schema
53*cdf0e10cSrcweir //
54*cdf0e10cSrcweir struct PROPERTYMAP
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     PROPERTYKEY key;
57*cdf0e10cSrcweir     PCWSTR pszXPathParent;
58*cdf0e10cSrcweir     PCWSTR pszValueNodeName;
59*cdf0e10cSrcweir };
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir PROPERTYMAP g_rgPROPERTYMAP[] =
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     { PKEY_Title,          L"OpenOffice.org",          L"Title" },
64*cdf0e10cSrcweir     { PKEY_Author,         L"OpenOffice.org",          L"Author" },
65*cdf0e10cSrcweir     { PKEY_Subject,        L"OpenOffice.org",          L"Subject" },
66*cdf0e10cSrcweir     { PKEY_Keywords,       L"OpenOffice.org",          L"Keyword" },
67*cdf0e10cSrcweir     { PKEY_Comment,        L"OpenOffice.org",          L"Comments" },
68*cdf0e10cSrcweir };
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir size_t gPropertyMapTableSize = sizeof(g_rgPROPERTYMAP)/sizeof(g_rgPROPERTYMAP[0]);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir //----------------------------
73*cdf0e10cSrcweir //
74*cdf0e10cSrcweir //----------------------------
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir CPropertyHdl::CPropertyHdl( long nRefCnt ) :
77*cdf0e10cSrcweir     m_RefCnt( nRefCnt ),
78*cdf0e10cSrcweir     m_pCache( NULL )
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir     OutputDebugStringFormat( "CPropertyHdl: CTOR\n" );
81*cdf0e10cSrcweir     InterlockedIncrement( &g_DllRefCnt );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir //----------------------------
85*cdf0e10cSrcweir //
86*cdf0e10cSrcweir //----------------------------
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir CPropertyHdl::~CPropertyHdl()
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir     if ( m_pCache )
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         m_pCache->Release();
93*cdf0e10cSrcweir         m_pCache = NULL;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     InterlockedDecrement( &g_DllRefCnt );
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir //-----------------------------
99*cdf0e10cSrcweir // IUnknown methods
100*cdf0e10cSrcweir //-----------------------------
101*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     *ppvObject = 0;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     if (IID_IUnknown == riid || IID_IPropertyStore == riid)
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         OutputDebugStringFormat( "CPropertyHdl: QueryInterface (IID_IPropertyStore)\n" );
108*cdf0e10cSrcweir         IUnknown* pUnk = static_cast<IPropertyStore*>(this);
109*cdf0e10cSrcweir         pUnk->AddRef();
110*cdf0e10cSrcweir         *ppvObject = pUnk;
111*cdf0e10cSrcweir         return S_OK;
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir     else if (IID_IPropertyStoreCapabilities == riid)
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         OutputDebugStringFormat( "CPropertyHdl: QueryInterface (IID_IPropertyStoreCapabilities)\n" );
116*cdf0e10cSrcweir         IUnknown* pUnk = static_cast<IPropertyStore*>(this);
117*cdf0e10cSrcweir         pUnk->AddRef();
118*cdf0e10cSrcweir         *ppvObject = pUnk;
119*cdf0e10cSrcweir         return S_OK;
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir     else if (IID_IInitializeWithStream == riid)
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         OutputDebugStringFormat( "CPropertyHdl: QueryInterface (IID_IInitializeWithStream)\n" );
124*cdf0e10cSrcweir         IUnknown* pUnk = static_cast<IInitializeWithStream*>(this);
125*cdf0e10cSrcweir         pUnk->AddRef();
126*cdf0e10cSrcweir         *ppvObject = pUnk;
127*cdf0e10cSrcweir         return S_OK;
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir     OutputDebugStringFormat( "CPropertyHdl: QueryInterface (something different)\n" );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     return E_NOINTERFACE;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir //----------------------------
135*cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CPropertyHdl::AddRef( void )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	return InterlockedIncrement( &m_RefCnt );
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir //----------------------------
141*cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CPropertyHdl::Release( void )
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 	long refcnt = InterlockedDecrement( &m_RefCnt );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	if ( 0 == m_RefCnt )
146*cdf0e10cSrcweir 		delete this;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	return refcnt;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir //-----------------------------
152*cdf0e10cSrcweir // IPropertyStore
153*cdf0e10cSrcweir //-----------------------------
154*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::GetCount( DWORD *pcProps )
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir     HRESULT hr = E_UNEXPECTED;
157*cdf0e10cSrcweir     if ( m_pCache && pcProps )
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         hr = m_pCache->GetCount( pcProps );
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     return hr;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir //-----------------------------
166*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::GetAt( DWORD iProp, PROPERTYKEY *pKey )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     HRESULT hr = E_UNEXPECTED;
169*cdf0e10cSrcweir     if ( m_pCache )
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         hr = m_pCache->GetAt( iProp, pKey );
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     return hr;
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir //-----------------------------
178*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::GetValue( REFPROPERTYKEY key, PROPVARIANT *pPropVar )
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     HRESULT hr = E_UNEXPECTED;
181*cdf0e10cSrcweir     if ( m_pCache )
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         hr = m_pCache->GetValue( key, pPropVar );
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     return hr;
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir //-----------------------------
190*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::SetValue( REFPROPERTYKEY key, REFPROPVARIANT propVar )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     HRESULT hr = E_UNEXPECTED;
193*cdf0e10cSrcweir     if ( m_pCache )
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         hr = STG_E_ACCESSDENIED;
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir     return hr;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir //-----------------------------
201*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::Commit()
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     return S_OK;
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir //-----------------------------
207*cdf0e10cSrcweir // IPropertyStore
208*cdf0e10cSrcweir //-----------------------------
209*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::IsPropertyWritable( REFPROPERTYKEY key )
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     // We start with read only properties only
212*cdf0e10cSrcweir     return S_FALSE;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir //-----------------------------
216*cdf0e10cSrcweir // IInitializeWithStream
217*cdf0e10cSrcweir //-----------------------------
218*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CPropertyHdl::Initialize( IStream *pStream, DWORD grfMode )
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir     if ( grfMode & STGM_READWRITE )
221*cdf0e10cSrcweir         return STG_E_ACCESSDENIED;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     if ( !m_pCache )
224*cdf0e10cSrcweir     {
225*cdf0e10cSrcweir #ifdef __MINGW32__
226*cdf0e10cSrcweir         if ( FAILED( PSCreateMemoryPropertyStore( IID_IPropertyStoreCache, reinterpret_cast<void**>(&m_pCache) ) ) )
227*cdf0e10cSrcweir #else
228*cdf0e10cSrcweir         if ( FAILED( PSCreateMemoryPropertyStore( IID_PPV_ARGS( &m_pCache ) ) ) )
229*cdf0e10cSrcweir #endif
230*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::Initialize: PSCreateMemoryPropertyStore failed" );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         zlib_filefunc_def z_filefunc;
233*cdf0e10cSrcweir         pStream = PrepareIStream( pStream, z_filefunc );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         CMetaInfoReader *pMetaInfoReader = NULL;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         try
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             pMetaInfoReader = new CMetaInfoReader( (void*)pStream, &z_filefunc );
240*cdf0e10cSrcweir             LoadProperties( pMetaInfoReader );
241*cdf0e10cSrcweir             delete pMetaInfoReader;
242*cdf0e10cSrcweir         }
243*cdf0e10cSrcweir         catch (const std::exception& e)
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::Initialize: Caught exception [%s]", e.what() );
246*cdf0e10cSrcweir             return E_FAIL;
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir /*
249*cdf0e10cSrcweir     // load extended properties and search content
250*cdf0e10cSrcweir     _LoadExtendedProperties();
251*cdf0e10cSrcweir     _LoadSearchContent();
252*cdf0e10cSrcweir */
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     return S_OK;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir //-----------------------------
259*cdf0e10cSrcweir void CPropertyHdl::LoadProperties( CMetaInfoReader *pMetaInfoReader )
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir     OutputDebugStringFormat( "CPropertyHdl: LoadProperties\n" );
262*cdf0e10cSrcweir     PROPVARIANT propvarValues;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     for ( UINT i = 0; i < (UINT)gPropertyMapTableSize; ++i )
265*cdf0e10cSrcweir     {
266*cdf0e10cSrcweir         PropVariantClear( &propvarValues );
267*cdf0e10cSrcweir         HRESULT hr = GetItemData( pMetaInfoReader, i, &propvarValues);
268*cdf0e10cSrcweir         if (hr == S_OK)
269*cdf0e10cSrcweir         {
270*cdf0e10cSrcweir             // coerce the value(s) to the appropriate type for the property key
271*cdf0e10cSrcweir             hr = PSCoerceToCanonicalValue( g_rgPROPERTYMAP[i].key, &propvarValues );
272*cdf0e10cSrcweir             if (SUCCEEDED(hr))
273*cdf0e10cSrcweir             {
274*cdf0e10cSrcweir                 // cache the value(s) loaded
275*cdf0e10cSrcweir                 hr = m_pCache->SetValueAndState( g_rgPROPERTYMAP[i].key, &propvarValues, PSC_NORMAL );
276*cdf0e10cSrcweir             }
277*cdf0e10cSrcweir         }
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir //-----------------------------
282*cdf0e10cSrcweir HRESULT CPropertyHdl::GetItemData( CMetaInfoReader *pMetaInfoReader, UINT nIndex, PROPVARIANT *pVarData )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     switch (nIndex) {
285*cdf0e10cSrcweir     case 0: {
286*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
287*cdf0e10cSrcweir             pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagData( META_INFO_TITLE ).c_str() );
288*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Title=%S.\n", pMetaInfoReader->getTagData( META_INFO_TITLE ).c_str() );
289*cdf0e10cSrcweir             return S_OK;
290*cdf0e10cSrcweir     }
291*cdf0e10cSrcweir     case 1: {
292*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
293*cdf0e10cSrcweir 			pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagData( META_INFO_AUTHOR ).c_str() );
294*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Author=%S.\n", pMetaInfoReader->getTagData( META_INFO_AUTHOR ).c_str() );
295*cdf0e10cSrcweir 			return S_OK;
296*cdf0e10cSrcweir 	}
297*cdf0e10cSrcweir     case 2: {
298*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
299*cdf0e10cSrcweir 			pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagData( META_INFO_SUBJECT ).c_str() );
300*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Subject=%S.\n", pMetaInfoReader->getTagData( META_INFO_SUBJECT ).c_str() );
301*cdf0e10cSrcweir 			return S_OK;
302*cdf0e10cSrcweir 	}
303*cdf0e10cSrcweir     case 3: {
304*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
305*cdf0e10cSrcweir             pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagData( META_INFO_KEYWORDS ).c_str() );
306*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Keywords=%S.\n", pMetaInfoReader->getTagData( META_INFO_KEYWORDS ).c_str() );
307*cdf0e10cSrcweir             return S_OK;
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir     case 4: {
310*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
311*cdf0e10cSrcweir             pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagData( META_INFO_DESCRIPTION ).c_str() );
312*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Description=%S.\n", pMetaInfoReader->getTagData( META_INFO_DESCRIPTION ).c_str() );
313*cdf0e10cSrcweir             return S_OK;
314*cdf0e10cSrcweir     }
315*cdf0e10cSrcweir     case 5: {
316*cdf0e10cSrcweir             pVarData->vt = VT_BSTR;
317*cdf0e10cSrcweir             pVarData->bstrVal = SysAllocString( pMetaInfoReader->getTagAttribute( META_INFO_DOCUMENT_STATISTIC, META_INFO_PAGES ).c_str() );
318*cdf0e10cSrcweir             OutputDebugStringFormat( "CPropertyHdl::GetItemData: Pages=%S.\n", pMetaInfoReader->getTagAttribute( META_INFO_DOCUMENT_STATISTIC, META_INFO_PAGES ).c_str() );
319*cdf0e10cSrcweir             return S_OK;
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir     }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 	return S_FALSE;
324*cdf0e10cSrcweir }
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir //-----------------------------------------------------------------------------
327*cdf0e10cSrcweir //                              CClassFactory
328*cdf0e10cSrcweir //-----------------------------------------------------------------------------
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir long CClassFactory::s_ServerLocks = 0;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir //-----------------------------------------------------------------------------
333*cdf0e10cSrcweir CClassFactory::CClassFactory( const CLSID& clsid ) :
334*cdf0e10cSrcweir     m_RefCnt(1),
335*cdf0e10cSrcweir     m_Clsid(clsid)
336*cdf0e10cSrcweir {
337*cdf0e10cSrcweir     InterlockedIncrement( &g_DllRefCnt );
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir //-----------------------------------------------------------------------------
341*cdf0e10cSrcweir CClassFactory::~CClassFactory()
342*cdf0e10cSrcweir {
343*cdf0e10cSrcweir     InterlockedDecrement( &g_DllRefCnt );
344*cdf0e10cSrcweir }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir //-----------------------------------------------------------------------------
347*cdf0e10cSrcweir //                              IUnknown methods
348*cdf0e10cSrcweir //-----------------------------------------------------------------------------
349*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CClassFactory::QueryInterface( REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject )
350*cdf0e10cSrcweir {
351*cdf0e10cSrcweir     *ppvObject = 0;
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir     if ( IID_IUnknown == riid || IID_IClassFactory == riid )
354*cdf0e10cSrcweir     {
355*cdf0e10cSrcweir         IUnknown* pUnk = this;
356*cdf0e10cSrcweir         pUnk->AddRef();
357*cdf0e10cSrcweir         *ppvObject = pUnk;
358*cdf0e10cSrcweir         return S_OK;
359*cdf0e10cSrcweir     }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     return E_NOINTERFACE;
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir //-----------------------------------------------------------------------------
365*cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CClassFactory::AddRef( void )
366*cdf0e10cSrcweir {
367*cdf0e10cSrcweir     return InterlockedIncrement( &m_RefCnt );
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir //-----------------------------------------------------------------------------
371*cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CClassFactory::Release( void )
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir     long refcnt = InterlockedDecrement( &m_RefCnt );
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir     if (0 == refcnt)
376*cdf0e10cSrcweir         delete this;
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir     return refcnt;
379*cdf0e10cSrcweir }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir //-----------------------------------------------------------------------------
382*cdf0e10cSrcweir //                          IClassFactory methods
383*cdf0e10cSrcweir //-----------------------------------------------------------------------------
384*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CClassFactory::CreateInstance(
385*cdf0e10cSrcweir             IUnknown __RPC_FAR *pUnkOuter,
386*cdf0e10cSrcweir             REFIID riid,
387*cdf0e10cSrcweir             void __RPC_FAR *__RPC_FAR *ppvObject)
388*cdf0e10cSrcweir {
389*cdf0e10cSrcweir     if ( pUnkOuter != NULL )
390*cdf0e10cSrcweir         return CLASS_E_NOAGGREGATION;
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     IUnknown* pUnk = 0;
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir     if ( CLSID_PROPERTY_HANDLER == m_Clsid )
395*cdf0e10cSrcweir         pUnk = static_cast<IPropertyStore*>( new CPropertyHdl() );
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir     POST_CONDITION(pUnk != 0, "Could not create COM object");
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir     if (0 == pUnk)
400*cdf0e10cSrcweir         return E_OUTOFMEMORY;
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     HRESULT hr = pUnk->QueryInterface( riid, ppvObject );
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir     // if QueryInterface failed the component will destroy itself
405*cdf0e10cSrcweir     pUnk->Release();
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir     return hr;
408*cdf0e10cSrcweir }
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir //-----------------------------------------------------------------------------
411*cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CClassFactory::LockServer( BOOL fLock )
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir     if ( fLock )
414*cdf0e10cSrcweir         InterlockedIncrement( &s_ServerLocks );
415*cdf0e10cSrcweir     else
416*cdf0e10cSrcweir         InterlockedDecrement( &s_ServerLocks );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir     return S_OK;
419*cdf0e10cSrcweir }
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir //-----------------------------------------------------------------------------
422*cdf0e10cSrcweir bool CClassFactory::IsLocked()
423*cdf0e10cSrcweir {
424*cdf0e10cSrcweir     return ( s_ServerLocks > 0 );
425*cdf0e10cSrcweir }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir //-----------------------------------------------------------------------------
428*cdf0e10cSrcweir extern "C" STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void** ppv)
429*cdf0e10cSrcweir {
430*cdf0e10cSrcweir     OutputDebugStringFormat( "DllGetClassObject.\n" );
431*cdf0e10cSrcweir     *ppv = 0;
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir     if ( rclsid != CLSID_PROPERTY_HANDLER )
434*cdf0e10cSrcweir         return CLASS_E_CLASSNOTAVAILABLE;
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir     if ( (riid != IID_IUnknown) && (riid != IID_IClassFactory) )
437*cdf0e10cSrcweir         return E_NOINTERFACE;
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir 	IUnknown* pUnk = new CClassFactory( rclsid );
440*cdf0e10cSrcweir 	if ( 0 == pUnk )
441*cdf0e10cSrcweir 		return E_OUTOFMEMORY;
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 	*ppv = pUnk;
444*cdf0e10cSrcweir 	return S_OK;
445*cdf0e10cSrcweir }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir //-----------------------------------------------------------------------------
448*cdf0e10cSrcweir extern "C" STDAPI DllCanUnloadNow( void )
449*cdf0e10cSrcweir {
450*cdf0e10cSrcweir     OutputDebugStringFormat( "DllCanUnloadNow.\n" );
451*cdf0e10cSrcweir     if (CClassFactory::IsLocked() || g_DllRefCnt > 0)
452*cdf0e10cSrcweir         return S_FALSE;
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     return S_OK;
455*cdf0e10cSrcweir }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir //-----------------------------------------------------------------------------
458*cdf0e10cSrcweir BOOL WINAPI DllMain( HINSTANCE hInst, ULONG /*ul_reason_for_call*/, LPVOID /*lpReserved*/ )
459*cdf0e10cSrcweir {
460*cdf0e10cSrcweir     OutputDebugStringFormat( "DllMain.\n" );
461*cdf0e10cSrcweir     g_hModule = hInst;
462*cdf0e10cSrcweir     return TRUE;
463*cdf0e10cSrcweir }
464