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