xref: /aoo42x/main/extensions/source/ole/servprov.cxx (revision 2a97ec55)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 #ifdef __MINGW32__
28 #define INITGUID
29 #include <initguid.h>
30 #else
31 #include "ole2uno.hxx"
32 #include "unoconversionutilities.hxx"
33 #endif
34 #include "servprov.hxx"
35 #include "unoobjw.hxx"
36 #include "oleobjw.hxx"
37 #include <rtl/unload.h>
38 
39 #include <tools/presys.h>
40 #define _WIN32_WINNT 0x0400
41 
42 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
43 #undef _DEBUG
44 #endif
45 #include <atlbase.h>
46 extern CComModule _Module;
47 #include <atlcom.h>
48 #include <tools/postsys.h>
49 
50 
51 using namespace std;
52 using namespace cppu;
53 using namespace rtl;
54 using namespace osl;
55 using namespace com::sun::star::lang;
56 using namespace com::sun::star::uno;
57 using namespace com::sun::star::bridge;
58 using namespace com::sun::star::bridge::ModelDependent;
59 
60 
61 
62 namespace ole_adapter
63 {
64 
65 #include <initguid.h>
66 // prior 5.2 ( src569 ver m)
67 // {3ECF78F0-B149-11D2-8EBE-00105AD848AF}
68 //DEFINE_GUID(OID_ServiceManager, 0x3ECF78F0, 0xB149, 0x11d2, 0x8E, 0xBE, 0x00, 0x10, 0x5A, 0xD8, 0x48, 0xAF);
69 
70 #ifndef OWNGUID
71 // GUID used since 5.2 ( src569 m)
72 // {82154420-0FBF-11d4-8313-005004526AB4}
73 DEFINE_GUID(OID_ServiceManager, 0x82154420, 0xfbf, 0x11d4, 0x83, 0x13, 0x0, 0x50, 0x4, 0x52, 0x6a, 0xb4);
74 #else
75 // Alternative GUID
76 // {D9BB9D1D-BFA9-4357-9F11-9A2E9061F06E}
77 DEFINE_GUID(OID_ServiceManager, 0xd9bb9d1d, 0xbfa9, 0x4357, 0x9f, 0x11, 0x9a, 0x2e, 0x90, 0x61, 0xf0, 0x6e);
78 #endif
79 
80 extern	rtl_StandardModuleCount globalModuleCount;
81 
82 /*****************************************************************************
83 
84 	class implementation ProviderOleWrapper_Impl
85 
86 *****************************************************************************/
87 
ProviderOleWrapper_Impl(const Reference<XMultiServiceFactory> & smgr,const Reference<XSingleServiceFactory> & xSFact,GUID * pGuid)88 ProviderOleWrapper_Impl::ProviderOleWrapper_Impl(const Reference<XMultiServiceFactory>& smgr,
89 												 const Reference<XSingleServiceFactory>& xSFact, GUID* pGuid)
90 	: m_xSingleServiceFactory(xSFact),
91 	  m_smgr( smgr)
92 {
93 	m_guid = *pGuid;
94 
95 	Reference<XInterface> xInt = smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
96 
97 	if (xInt.is())
98 	{
99 		Any a= xInt->queryInterface( ::getCppuType( reinterpret_cast<
100 												  Reference< XBridgeSupplier2>* >(0)));
101 		a >>= m_bridgeSupplier;
102 
103 	}
104 }
105 
~ProviderOleWrapper_Impl()106 ProviderOleWrapper_Impl::~ProviderOleWrapper_Impl()
107 {
108 }
109 
registerClass()110 sal_Bool ProviderOleWrapper_Impl::registerClass()
111 {
112 	HRESULT hresult;
113 
114 	o2u_attachCurrentThread();
115 
116 	hresult = CoRegisterClassObject(
117 			m_guid,
118 			this,
119 			CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
120 			REGCLS_MULTIPLEUSE,
121 			&m_factoryHandle);
122 
123 	return (hresult == NOERROR);
124 }
125 
deregisterClass()126 sal_Bool ProviderOleWrapper_Impl::deregisterClass()
127 {
128 	HRESULT hresult = CoRevokeClassObject(m_factoryHandle);
129 
130 	return (hresult == NOERROR);
131 }
132 
QueryInterface(REFIID riid,void FAR * FAR * ppv)133 STDMETHODIMP ProviderOleWrapper_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
134 {
135     if(IsEqualIID(riid, IID_IUnknown))
136 	{
137 		AddRef();
138 		*ppv = (IUnknown*) (IClassFactory*) this;
139 		return NOERROR;
140     }
141     else if (IsEqualIID(riid, IID_IClassFactory))
142 	{
143 		AddRef();
144 		*ppv = (IClassFactory*) this;
145 		return NOERROR;
146 	}
147 
148     *ppv = NULL;
149     return ResultFromScode(E_NOINTERFACE);
150 }
151 
STDMETHODIMP_(ULONG)152 STDMETHODIMP_(ULONG) ProviderOleWrapper_Impl::AddRef()
153 {
154 	return osl_incrementInterlockedCount( &m_refCount);
155 }
156 
STDMETHODIMP_(ULONG)157 STDMETHODIMP_(ULONG) ProviderOleWrapper_Impl::Release()
158 {
159     MutexGuard aGuard( Mutex::getGlobalMutex());
160     ULONG refCount = --m_refCount;
161 	if (m_refCount == 0)
162 	{
163 		delete this;
164 	}
165 
166     return refCount;
167 }
168 
CreateInstance(IUnknown FAR * punkOuter,REFIID riid,void FAR * FAR * ppv)169 STDMETHODIMP ProviderOleWrapper_Impl::CreateInstance(IUnknown FAR* punkOuter,
170 													 REFIID riid,
171     												 void FAR* FAR* ppv)
172 {
173 	HRESULT ret = ResultFromScode(E_UNEXPECTED);
174 	punkOuter = NULL;
175 
176 	Reference<XInterface> xInstance;
177 
178 	if (m_xSingleServiceFactory.is())
179 	{
180 		xInstance = m_xSingleServiceFactory->createInstance();
181 
182 		if (xInstance.is())
183 		{
184 			Any usrAny(&xInstance, getCppuType( & xInstance));
185 
186 			sal_uInt8 arId[16];
187 			rtl_getGlobalProcessId( arId );
188 			Any oleAny = m_bridgeSupplier->createBridge(usrAny,
189 										Sequence<sal_Int8>((sal_Int8*)arId, 16),
190 										UNO,
191 										OLE);
192 
193 
194 			if (oleAny.getValueTypeClass() == getCppuType( (sal_uInt32 *)0).getTypeClass())
195 			{
196 				VARIANT* pVariant = *(VARIANT**)oleAny.getValue();
197 
198 				if (pVariant->vt == VT_DISPATCH)
199 				{
200 		    		ret = pVariant->pdispVal->QueryInterface(riid, ppv);
201 				}
202 
203 				VariantClear(pVariant);
204 				CoTaskMemFree(pVariant);
205 			}
206 		}
207 	}
208 
209 	return ret;
210 }
211 
LockServer(int)212 STDMETHODIMP ProviderOleWrapper_Impl::LockServer(int /*fLock*/)
213 {
214     return NOERROR;
215 }
216 
217 /*****************************************************************************
218 
219 	class implementation OneInstanceOleWrapper_Impl
220 
221 *****************************************************************************/
222 
OneInstanceOleWrapper_Impl(const Reference<XMultiServiceFactory> & smgr,const Reference<XInterface> & xInst,GUID * pGuid,sal_Bool bAsApplication)223 OneInstanceOleWrapper_Impl::OneInstanceOleWrapper_Impl(  const Reference<XMultiServiceFactory>& smgr,
224 														 const Reference<XInterface>& xInst,
225                                                          GUID* pGuid,
226                                                          sal_Bool bAsApplication )
227 	: m_xInst(xInst), m_refCount(0),
228 	  m_smgr( smgr),
229       m_factoryHandle( 0 ),
230       m_bAsApplication( bAsApplication ),
231       m_nApplRegHandle( 0 )
232 {
233 	m_guid = *pGuid;
234 
235 	Reference<XInterface> xInt = m_smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
236 
237 	if (xInt.is())
238 	{
239 		Any a= xInt->queryInterface( getCppuType(
240 			reinterpret_cast< Reference<XBridgeSupplier2>*>(0)));
241 		a >>= m_bridgeSupplier;
242 	}
243 }
244 
~OneInstanceOleWrapper_Impl()245 OneInstanceOleWrapper_Impl::~OneInstanceOleWrapper_Impl()
246 {
247 }
248 
registerClass()249 sal_Bool OneInstanceOleWrapper_Impl::registerClass()
250 {
251 	HRESULT hresult;
252 
253 	o2u_attachCurrentThread();
254 
255 	hresult = CoRegisterClassObject(
256 			m_guid,
257 			this,
258 			CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
259 			REGCLS_MULTIPLEUSE,
260 			&m_factoryHandle);
261 
262     if ( hresult == NOERROR && m_bAsApplication )
263         hresult = RegisterActiveObject( this, m_guid, ACTIVEOBJECT_WEAK, &m_nApplRegHandle );
264 
265 	return (hresult == NOERROR);
266 }
267 
deregisterClass()268 sal_Bool OneInstanceOleWrapper_Impl::deregisterClass()
269 {
270     HRESULT hresult1 = NOERROR;
271     if ( m_bAsApplication )
272         hresult1 = RevokeActiveObject( m_nApplRegHandle, NULL );
273 
274 	HRESULT hresult2 = CoRevokeClassObject(m_factoryHandle);
275 
276 	return (hresult1 == NOERROR && hresult2 == NOERROR);
277 }
278 
QueryInterface(REFIID riid,void FAR * FAR * ppv)279 STDMETHODIMP OneInstanceOleWrapper_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
280 {
281     if(IsEqualIID(riid, IID_IUnknown))
282 	{
283 		AddRef();
284 		*ppv = (IUnknown*) (IClassFactory*) this;
285 		return NOERROR;
286     }
287     else if (IsEqualIID(riid, IID_IClassFactory))
288 	{
289 		AddRef();
290 		*ppv = (IClassFactory*) this;
291 		return NOERROR;
292 	}
293 
294     *ppv = NULL;
295     return ResultFromScode(E_NOINTERFACE);
296 }
297 
STDMETHODIMP_(ULONG)298 STDMETHODIMP_(ULONG) OneInstanceOleWrapper_Impl::AddRef()
299 {
300 	return osl_incrementInterlockedCount( &m_refCount);
301 }
302 
STDMETHODIMP_(ULONG)303 STDMETHODIMP_(ULONG) OneInstanceOleWrapper_Impl::Release()
304 {
305     MutexGuard oGuard( Mutex::getGlobalMutex());
306 	ULONG refCount = --m_refCount;
307 	if ( m_refCount == 0)
308 	{
309 		delete this;
310 	}
311 
312     return refCount;
313 }
314 
CreateInstance(IUnknown FAR * punkOuter,REFIID riid,void FAR * FAR * ppv)315 STDMETHODIMP OneInstanceOleWrapper_Impl::CreateInstance(IUnknown FAR* punkOuter,
316 													 	REFIID riid,
317     												 	void FAR* FAR* ppv)
318 {
319 	HRESULT ret = ResultFromScode(E_UNEXPECTED);
320 	punkOuter = NULL;
321 
322 	if (m_xInst.is())
323 	{
324 		Any usrAny(&m_xInst, getCppuType( &m_xInst));
325 		sal_uInt8 arId[16];
326 		rtl_getGlobalProcessId( arId);
327 		Any oleAny = m_bridgeSupplier->createBridge(usrAny,
328 										Sequence<sal_Int8>( (sal_Int8*)arId, 16),
329 										UNO,
330 										OLE);
331 
332 
333 		if (oleAny.getValueTypeClass() == TypeClass_UNSIGNED_LONG)
334 		{
335 			VARIANT* pVariant = *(VARIANT**)oleAny.getValue();
336 
337 			if ((pVariant->vt == VT_UNKNOWN) || (pVariant->vt == VT_DISPATCH))
338 			{
339 	    		ret = pVariant->punkVal->QueryInterface(riid, ppv);
340 			}
341 
342 			VariantClear(pVariant);
343 			CoTaskMemFree(pVariant);
344 		}
345 	}
346 
347 	return ret;
348 }
349 
LockServer(int)350 STDMETHODIMP OneInstanceOleWrapper_Impl::LockServer(int /*fLock*/)
351 {
352     return NOERROR;
353 }
354 
355 
356 /*****************************************************************************
357 
358 	class implementation OleConverter_Impl2
359 
360 *****************************************************************************/
361 
OleConverter_Impl2(const Reference<XMultiServiceFactory> & smgr)362 OleConverter_Impl2::OleConverter_Impl2( const Reference<XMultiServiceFactory> &smgr):
363 	UnoConversionUtilities<OleConverter_Impl2>( smgr)
364 
365 {
366 	// library unloading support
367 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
368 }
369 
370 // The XMultiServiceFactory is later set by XInitialization
OleConverter_Impl2(const Reference<XMultiServiceFactory> & smgr,sal_uInt8 unoWrapperClass,sal_uInt8 comWrapperClass)371 OleConverter_Impl2::OleConverter_Impl2( const  Reference<XMultiServiceFactory>& smgr, sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass ):
372 	UnoConversionUtilities<OleConverter_Impl2>( smgr, unoWrapperClass, comWrapperClass  )
373 
374 {
375 	//library unloading support
376 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
377 }
378 
~OleConverter_Impl2()379 OleConverter_Impl2::~OleConverter_Impl2()
380 {
381 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
382 }
383 
384 // XBridgeSupplier --------------------------------------------------------------
createBridge(const Any & modelDepObject,const Sequence<sal_Int8> & ProcessId,sal_Int16 sourceModelType,sal_Int16 destModelType)385 Any SAL_CALL OleConverter_Impl2::createBridge(const Any& modelDepObject,
386 									   const Sequence< sal_Int8 >& ProcessId,
387 									   sal_Int16 sourceModelType,
388 									   sal_Int16 destModelType)
389 									   throw (IllegalArgumentException,
390 									   			RuntimeException )
391 {
392 	Any ret;
393 	sal_uInt8 arId[16];
394 	rtl_getGlobalProcessId( arId );
395 
396 	Sequence< sal_Int8 > seqProcessId( (sal_Int8*)arId, 16);
397 
398 	if ( seqProcessId == ProcessId)
399 	{
400 		if (sourceModelType == UNO)
401 		{
402 			if (destModelType == UNO)
403 			{
404 				// same model -> copy value only
405 				ret = modelDepObject;
406 			}
407 			else if (destModelType == OLE)
408 			{
409 				// convert UNO any into variant
410 				VARIANT* pVariant = (VARIANT*) CoTaskMemAlloc(sizeof(VARIANT));
411 				VariantInit( pVariant);
412                 try
413                 {
414                     anyToVariant( pVariant, modelDepObject);
415                 }
416                 catch(...)
417 				{
418 					CoTaskMemFree(pVariant);
419 					throw IllegalArgumentException();
420 				}
421                 ret.setValue((void*) &pVariant, getCppuType((sal_uInt32*)0));
422 			}
423 			else
424 				throw IllegalArgumentException();
425 		}
426 		else if (sourceModelType == OLE)
427 		{
428 			if (modelDepObject.getValueType() != getCppuType((sal_uInt32*)0))
429 			{
430 				throw IllegalArgumentException();
431 			}
432 			else if (destModelType == OLE)
433 			{
434 				// same model -> copy value only
435 				VARIANT* pVariant = (VARIANT*) CoTaskMemAlloc(sizeof(VARIANT));
436 
437 				if (NOERROR != VariantCopy(pVariant, *(VARIANT**)modelDepObject.getValue()))
438 				{
439 					CoTaskMemFree(pVariant);
440 					throw(IllegalArgumentException());
441 				}
442 				else
443 				{
444 					ret.setValue((void*) &pVariant, getCppuType((sal_uInt32*)0));
445 				}
446 			}
447 			else if (destModelType == UNO)
448 			{
449 				// convert variant into UNO any
450 				VARIANT* pVariant = *(VARIANT**)modelDepObject.getValue();
451 				try
452 				{
453 					variantToAny(pVariant, ret);
454 				}
455 				catch (CannotConvertException & e)
456 				{
457 					throw IllegalArgumentException(
458 						e.Message, 0, -1);
459 				}
460 			}
461 			else
462 				throw IllegalArgumentException();
463 
464 		}
465 		else
466 			throw IllegalArgumentException();
467 	}
468 
469 	return ret;
470 }
471 
472 
473 // XInitialize ------------------------------------------------------------------------------
474 // the first argument is an XMultiServiceFactory if at all
initialize(const Sequence<Any> & aArguments)475 void SAL_CALL OleConverter_Impl2::initialize( const Sequence< Any >& aArguments )
476 				throw(Exception, RuntimeException)
477 {
478 	if( aArguments.getLength() == 1 && aArguments[0].getValueTypeClass() == TypeClass_INTERFACE)
479 	{
480 		Reference < XInterface > xInt;
481 		aArguments[0] >>= xInt;
482 		Reference <XMultiServiceFactory> xMulti( xInt, UNO_QUERY);
483 		m_smgrRemote= xMulti;
484 	}
485 }
486 
487 // UnoConversionUtilities -------------------------------------------------------------------
createUnoWrapperInstance()488 Reference< XInterface > OleConverter_Impl2::createUnoWrapperInstance()
489 {
490 	if( m_nUnoWrapperClass == INTERFACE_OLE_WRAPPER_IMPL)
491 	{
492 		Reference<XWeak> xWeak= static_cast<XWeak*>( new InterfaceOleWrapper_Impl(
493 								m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
494 		return Reference<XInterface>( xWeak, UNO_QUERY);
495 	}
496 	else if( m_nUnoWrapperClass == UNO_OBJECT_WRAPPER_REMOTE_OPT)
497 	{
498 		Reference<XWeak> xWeak= static_cast<XWeak*>( new UnoObjectWrapperRemoteOpt(
499 								m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
500 		return Reference<XInterface>( xWeak, UNO_QUERY);
501 	}
502 	else
503 		return Reference<XInterface>();
504 }
505 
createComWrapperInstance()506 Reference< XInterface > OleConverter_Impl2::createComWrapperInstance()
507 {
508 	Reference<XWeak> xWeak= static_cast<XWeak*>( new IUnknownWrapper_Impl(
509 							m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
510 	return Reference<XInterface>( xWeak, UNO_QUERY);
511 }
512 
513 
514 
515 /*****************************************************************************
516 
517 	class implementation OleClient_Impl
518 
519 *****************************************************************************/
520 
OleClient_Impl(const Reference<XMultiServiceFactory> & smgr)521 OleClient_Impl::OleClient_Impl( const Reference<XMultiServiceFactory>& smgr):
522 	UnoConversionUtilities<OleClient_Impl>( smgr)
523 {
524 	// library unloading support
525 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
526 	Reference<XInterface> xInt;// = m_smgr->createInstance(L"com.sun.star.bridge.OleBridgeSupplier2");
527 
528 	if (xInt.is())
529 	{
530 		Any a= xInt->queryInterface(getCppuType(
531 			reinterpret_cast<Reference<XBridgeSupplier2>*>(0)));
532 		a >>= m_bridgeSupplier;
533 	}
534 }
535 
~OleClient_Impl()536 OleClient_Impl::~OleClient_Impl()
537 {
538 	// library unloading support
539 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
540 }
541 
getAvailableServiceNames()542 Sequence< OUString >	SAL_CALL OleClient_Impl::getAvailableServiceNames() throw( RuntimeException )
543 {
544 	Sequence< OUString > ret;
545 
546 	return ret;
547 }
548 
549 
getImplementationName()550 OUString OleClient_Impl::getImplementationName()
551 {
552 	return OUString(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.comp.ole.OleClient"));
553 }
554 
createInstance(const OUString & ServiceSpecifier)555 Reference<XInterface> SAL_CALL OleClient_Impl::createInstance(const OUString& ServiceSpecifier) throw (Exception, RuntimeException )
556 {
557 	Reference<XInterface> 	ret;
558 	HRESULT 		result;
559 	IUnknown* 		pUnknown = NULL;
560 	CLSID			classId;
561 
562 	o2u_attachCurrentThread();
563 
564 	result = CLSIDFromProgID(
565   				reinterpret_cast<LPCWSTR>(ServiceSpecifier.getStr()),  	//Pointer to the ProgID
566   				&classId);         				//Pointer to the CLSID
567 
568 
569 	if (result == NOERROR)
570 	{
571 		result = CoCreateInstance(
572 					  classId,     			//Class identifier (CLSID) of the object
573 					  NULL, 			   	//Pointer to whether object is or isn't part of an aggregate
574 					  CLSCTX_SERVER,  //Context for running executable code
575 					  IID_IUnknown,         //Reference to the identifier of the interface
576 					  (void**)&pUnknown);   //Address of output variable that receives
577 	                       				   	// the interface pointer requested in riid
578 	}
579 
580 	if (pUnknown != NULL)
581 	{
582 		Any	any;
583 		CComVariant variant;
584 
585 		V_VT(&variant) = VT_UNKNOWN;
586 		V_UNKNOWN(&variant)	= pUnknown;
587 		// AddRef for Variant
588 		pUnknown->AddRef();
589 
590 		// When the object is wrapped, then its refcount is increased
591 		variantToAny(&variant, any);
592         if (any.getValueTypeClass() == TypeClass_INTERFACE)
593         {
594             any >>= ret;
595         }
596 		pUnknown->Release(); // CoCreateInstance
597 	}
598 
599 	return ret;
600 }
601 
createInstanceWithArguments(const OUString & ServiceSpecifier,const Sequence<Any> &)602 Reference<XInterface> SAL_CALL OleClient_Impl::createInstanceWithArguments(const OUString& ServiceSpecifier, const Sequence< Any >& /*Arguments*/) throw (Exception, RuntimeException)
603 {
604 	return createInstance( ServiceSpecifier);
605 }
606 
607 // UnoConversionUtilities -----------------------------------------------------------------------------
createUnoWrapperInstance()608 Reference< XInterface > OleClient_Impl::createUnoWrapperInstance()
609 {
610 	if( m_nUnoWrapperClass == INTERFACE_OLE_WRAPPER_IMPL)
611 	{
612 		Reference<XWeak> xWeak= static_cast<XWeak*>( new InterfaceOleWrapper_Impl(
613 								m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
614 		return Reference<XInterface>( xWeak, UNO_QUERY);
615 	}
616 	else if( m_nUnoWrapperClass == UNO_OBJECT_WRAPPER_REMOTE_OPT)
617 	{
618 		Reference<XWeak> xWeak= static_cast<XWeak*>( new UnoObjectWrapperRemoteOpt(
619 								m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
620 		return Reference<XInterface>( xWeak, UNO_QUERY);
621 	}
622 	else
623 		return Reference< XInterface>();
624 }
625 // UnoConversionUtilities -----------------------------------------------------------------------------
createComWrapperInstance()626 Reference< XInterface > OleClient_Impl::createComWrapperInstance( )
627 {
628 	Reference<XWeak> xWeak= static_cast<XWeak*>( new IUnknownWrapper_Impl(
629 							m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
630 	return Reference<XInterface>( xWeak, UNO_QUERY);
631 }
632 
633 
634 
635 /*****************************************************************************
636 
637 	class implementation OleServer_Impl
638 
639 *****************************************************************************/
640 
OleServer_Impl(const Reference<XMultiServiceFactory> & smgr)641 OleServer_Impl::OleServer_Impl( const Reference<XMultiServiceFactory>& smgr):
642 	m_smgr( smgr)
643 {
644 	//library unloading support
645 	globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
646 	Reference<XInterface> xInt = m_smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
647 
648 	if (xInt.is())
649 	{
650 		Any a= xInt->queryInterface( getCppuType(
651 			reinterpret_cast< Reference<XBridgeSupplier2>*>(0)));
652 		a >>= m_bridgeSupplier;
653 	}
654 
655 #ifndef OWNGUID
656 	sal_Bool bOLERegister = sal_False;
657 #else
658 	sal_Bool bOLERegister = sal_True;
659 #endif
660 	sal_Bool ret = provideInstance( m_smgr, (GUID*)&OID_ServiceManager, bOLERegister );
661     (void)ret;
662 }
663 
~OleServer_Impl()664 OleServer_Impl::~OleServer_Impl()
665 {
666 	while (!m_wrapperList.empty())
667 	{
668 		(*m_wrapperList.begin())->deregisterClass();
669 		(*m_wrapperList.begin())->Release();
670 		m_wrapperList.pop_front();
671 	}
672 	//library unloading support
673 	globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
674 }
675 // XInterface --------------------------------------------------
queryInterface(const Type & aType)676 Any SAL_CALL OleServer_Impl::queryInterface( const Type& aType ) throw(RuntimeException)
677 {
678 	Any a= ::cppu::queryInterface( aType, static_cast<XTypeProvider*>(this));
679 	if( a == Any())
680 		return OWeakObject::queryInterface( aType);
681 	else
682 		return a;
683 }
acquire()684 void SAL_CALL OleServer_Impl::acquire(  ) throw()
685 {
686 	OWeakObject::acquire();
687 }
release()688 void SAL_CALL OleServer_Impl::release(  ) throw ()
689 {
690 	OWeakObject::release();
691 }
692 
693 
694 // XTypeProvider --------------------------------------------------
getTypes()695 Sequence< Type > SAL_CALL OleServer_Impl::getTypes( ) throw(RuntimeException)
696 {
697 	static OTypeCollection *pCollection = 0;
698 	if( ! pCollection )
699 	{
700 		MutexGuard guard( Mutex::getGlobalMutex() );
701 		if( ! pCollection )
702 		{
703 			static OTypeCollection collection(
704 				getCppuType(reinterpret_cast< Reference< XWeak>*>(0)),
705 				getCppuType(reinterpret_cast< Reference< XTypeProvider>*>(0)) );
706 			pCollection = &collection;
707 		}
708 	}
709 	return (*pCollection).getTypes();
710 }
getImplementationId()711 Sequence< sal_Int8 > SAL_CALL OleServer_Impl::getImplementationId() throw(RuntimeException)
712 {
713 	static OImplementationId *pId = 0;
714 	if( ! pId )
715 	{
716 		MutexGuard guard( Mutex::getGlobalMutex() );
717 		if( ! pId )
718 		{
719 			static OImplementationId id( sal_False );
720 			pId = &id;
721 		}
722 	}
723 	return (*pId).getImplementationId();
724 }
725 
726 
provideService(const Reference<XSingleServiceFactory> & xSFact,GUID * guid)727 sal_Bool OleServer_Impl::provideService(const Reference<XSingleServiceFactory>& xSFact, GUID* guid)
728 {
729 	IClassFactoryWrapper* pFac = new ProviderOleWrapper_Impl( m_smgr, xSFact, guid);
730 
731 	pFac->AddRef();
732 
733 	m_wrapperList.push_back(pFac);
734 
735 	return pFac->registerClass();
736 }
737 
provideInstance(const Reference<XInterface> & xInst,GUID * guid,sal_Bool bAsApplication)738 sal_Bool OleServer_Impl::provideInstance(const Reference<XInterface>& xInst, GUID* guid, sal_Bool bAsApplication )
739 {
740 	IClassFactoryWrapper* pFac = new OneInstanceOleWrapper_Impl( m_smgr, xInst, guid, bAsApplication );
741 
742 	pFac->AddRef();
743 	m_wrapperList.push_back(pFac);
744 
745 	return pFac->registerClass();
746 }
747 
748 
749 
750 } // end namespace
751