masterfix DEV300: #i10000# build fix
[LibreOffice.git] / extensions / source / ole / servprov.cxx
blob18f3bc26f9b7d044e588c7124c116dc6c8a3ebd9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
31 #ifdef __MINGW32__
32 #define INITGUID
33 #include <initguid.h>
34 #else
35 #include "ole2uno.hxx"
36 #include "unoconversionutilities.hxx"
37 #endif
38 #include "servprov.hxx"
39 #include "unoobjw.hxx"
40 #include "oleobjw.hxx"
41 #include <rtl/unload.h>
43 #include <tools/presys.h>
44 #define _WIN32_WINNT 0x0400
46 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
47 #undef _DEBUG
48 #endif
49 #include <atlbase.h>
50 extern CComModule _Module;
51 #include <atlcom.h>
52 #include <tools/postsys.h>
55 using namespace std;
56 using namespace cppu;
57 using namespace rtl;
58 using namespace osl;
59 using namespace com::sun::star::lang;
60 using namespace com::sun::star::uno;
61 using namespace com::sun::star::bridge;
62 using namespace com::sun::star::bridge::ModelDependent;
66 namespace ole_adapter
69 #include <initguid.h>
70 // prior 5.2 ( src569 ver m)
71 // {3ECF78F0-B149-11D2-8EBE-00105AD848AF}
72 //DEFINE_GUID(OID_ServiceManager, 0x3ECF78F0, 0xB149, 0x11d2, 0x8E, 0xBE, 0x00, 0x10, 0x5A, 0xD8, 0x48, 0xAF);
74 #ifndef OWNGUID
75 // GUID used since 5.2 ( src569 m)
76 // {82154420-0FBF-11d4-8313-005004526AB4}
77 DEFINE_GUID(OID_ServiceManager, 0x82154420, 0xfbf, 0x11d4, 0x83, 0x13, 0x0, 0x50, 0x4, 0x52, 0x6a, 0xb4);
78 #else
79 // Alternative GUID
80 // {D9BB9D1D-BFA9-4357-9F11-9A2E9061F06E}
81 DEFINE_GUID(OID_ServiceManager, 0xd9bb9d1d, 0xbfa9, 0x4357, 0x9f, 0x11, 0x9a, 0x2e, 0x90, 0x61, 0xf0, 0x6e);
82 #endif
84 extern rtl_StandardModuleCount globalModuleCount;
86 /*****************************************************************************
88 class implementation ProviderOleWrapper_Impl
90 *****************************************************************************/
92 ProviderOleWrapper_Impl::ProviderOleWrapper_Impl(const Reference<XMultiServiceFactory>& smgr,
93 const Reference<XSingleServiceFactory>& xSFact, GUID* pGuid)
94 : m_xSingleServiceFactory(xSFact),
95 m_smgr( smgr)
97 m_guid = *pGuid;
99 Reference<XInterface> xInt = smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
101 if (xInt.is())
103 Any a= xInt->queryInterface( ::getCppuType( reinterpret_cast<
104 Reference< XBridgeSupplier2>* >(0)));
105 a >>= m_bridgeSupplier;
110 ProviderOleWrapper_Impl::~ProviderOleWrapper_Impl()
114 sal_Bool ProviderOleWrapper_Impl::registerClass()
116 HRESULT hresult;
118 o2u_attachCurrentThread();
120 hresult = CoRegisterClassObject(
121 m_guid,
122 this,
123 CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
124 REGCLS_MULTIPLEUSE,
125 &m_factoryHandle);
127 return (hresult == NOERROR);
130 sal_Bool ProviderOleWrapper_Impl::deregisterClass()
132 HRESULT hresult = CoRevokeClassObject(m_factoryHandle);
134 return (hresult == NOERROR);
137 STDMETHODIMP ProviderOleWrapper_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
139 if(IsEqualIID(riid, IID_IUnknown))
141 AddRef();
142 *ppv = (IUnknown*) (IClassFactory*) this;
143 return NOERROR;
145 else if (IsEqualIID(riid, IID_IClassFactory))
147 AddRef();
148 *ppv = (IClassFactory*) this;
149 return NOERROR;
152 *ppv = NULL;
153 return ResultFromScode(E_NOINTERFACE);
156 STDMETHODIMP_(ULONG) ProviderOleWrapper_Impl::AddRef()
158 return osl_incrementInterlockedCount( &m_refCount);
161 STDMETHODIMP_(ULONG) ProviderOleWrapper_Impl::Release()
163 MutexGuard aGuard( Mutex::getGlobalMutex());
164 ULONG refCount = --m_refCount;
165 if (m_refCount == 0)
167 delete this;
170 return refCount;
173 STDMETHODIMP ProviderOleWrapper_Impl::CreateInstance(IUnknown FAR* punkOuter,
174 REFIID riid,
175 void FAR* FAR* ppv)
177 HRESULT ret = ResultFromScode(E_UNEXPECTED);
178 punkOuter = NULL;
180 Reference<XInterface> xInstance;
182 if (m_xSingleServiceFactory.is())
184 xInstance = m_xSingleServiceFactory->createInstance();
186 if (xInstance.is())
188 Any usrAny(&xInstance, getCppuType( & xInstance));
190 sal_uInt8 arId[16];
191 rtl_getGlobalProcessId( arId );
192 Any oleAny = m_bridgeSupplier->createBridge(usrAny,
193 Sequence<sal_Int8>((sal_Int8*)arId, 16),
194 UNO,
195 OLE);
198 if (oleAny.getValueTypeClass() == getCppuType( (sal_uInt32 *)0).getTypeClass())
200 VARIANT* pVariant = *(VARIANT**)oleAny.getValue();
202 if (pVariant->vt == VT_DISPATCH)
204 ret = pVariant->pdispVal->QueryInterface(riid, ppv);
207 VariantClear(pVariant);
208 CoTaskMemFree(pVariant);
213 return ret;
216 STDMETHODIMP ProviderOleWrapper_Impl::LockServer(int /*fLock*/)
218 return NOERROR;
221 /*****************************************************************************
223 class implementation OneInstanceOleWrapper_Impl
225 *****************************************************************************/
227 OneInstanceOleWrapper_Impl::OneInstanceOleWrapper_Impl( const Reference<XMultiServiceFactory>& smgr,
228 const Reference<XInterface>& xInst,
229 GUID* pGuid,
230 sal_Bool bAsApplication )
231 : m_xInst(xInst), m_refCount(0),
232 m_smgr( smgr),
233 m_factoryHandle( 0 ),
234 m_bAsApplication( bAsApplication ),
235 m_nApplRegHandle( 0 )
237 m_guid = *pGuid;
239 Reference<XInterface> xInt = m_smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
241 if (xInt.is())
243 Any a= xInt->queryInterface( getCppuType(
244 reinterpret_cast< Reference<XBridgeSupplier2>*>(0)));
245 a >>= m_bridgeSupplier;
249 OneInstanceOleWrapper_Impl::~OneInstanceOleWrapper_Impl()
253 sal_Bool OneInstanceOleWrapper_Impl::registerClass()
255 HRESULT hresult;
257 o2u_attachCurrentThread();
259 hresult = CoRegisterClassObject(
260 m_guid,
261 this,
262 CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
263 REGCLS_MULTIPLEUSE,
264 &m_factoryHandle);
266 if ( hresult == NOERROR && m_bAsApplication )
267 hresult = RegisterActiveObject( this, m_guid, ACTIVEOBJECT_WEAK, &m_nApplRegHandle );
269 return (hresult == NOERROR);
272 sal_Bool OneInstanceOleWrapper_Impl::deregisterClass()
274 HRESULT hresult1 = NOERROR;
275 if ( m_bAsApplication )
276 hresult1 = RevokeActiveObject( m_nApplRegHandle, NULL );
278 HRESULT hresult2 = CoRevokeClassObject(m_factoryHandle);
280 return (hresult1 == NOERROR && hresult2 == NOERROR);
283 STDMETHODIMP OneInstanceOleWrapper_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
285 if(IsEqualIID(riid, IID_IUnknown))
287 AddRef();
288 *ppv = (IUnknown*) (IClassFactory*) this;
289 return NOERROR;
291 else if (IsEqualIID(riid, IID_IClassFactory))
293 AddRef();
294 *ppv = (IClassFactory*) this;
295 return NOERROR;
298 *ppv = NULL;
299 return ResultFromScode(E_NOINTERFACE);
302 STDMETHODIMP_(ULONG) OneInstanceOleWrapper_Impl::AddRef()
304 return osl_incrementInterlockedCount( &m_refCount);
307 STDMETHODIMP_(ULONG) OneInstanceOleWrapper_Impl::Release()
309 MutexGuard oGuard( Mutex::getGlobalMutex());
310 ULONG refCount = --m_refCount;
311 if ( m_refCount == 0)
313 delete this;
316 return refCount;
319 STDMETHODIMP OneInstanceOleWrapper_Impl::CreateInstance(IUnknown FAR* punkOuter,
320 REFIID riid,
321 void FAR* FAR* ppv)
323 HRESULT ret = ResultFromScode(E_UNEXPECTED);
324 punkOuter = NULL;
326 if (m_xInst.is())
328 Any usrAny(&m_xInst, getCppuType( &m_xInst));
329 sal_uInt8 arId[16];
330 rtl_getGlobalProcessId( arId);
331 Any oleAny = m_bridgeSupplier->createBridge(usrAny,
332 Sequence<sal_Int8>( (sal_Int8*)arId, 16),
333 UNO,
334 OLE);
337 if (oleAny.getValueTypeClass() == TypeClass_UNSIGNED_LONG)
339 VARIANT* pVariant = *(VARIANT**)oleAny.getValue();
341 if ((pVariant->vt == VT_UNKNOWN) || (pVariant->vt == VT_DISPATCH))
343 ret = pVariant->punkVal->QueryInterface(riid, ppv);
346 VariantClear(pVariant);
347 CoTaskMemFree(pVariant);
351 return ret;
354 STDMETHODIMP OneInstanceOleWrapper_Impl::LockServer(int /*fLock*/)
356 return NOERROR;
360 /*****************************************************************************
362 class implementation OleConverter_Impl2
364 *****************************************************************************/
366 OleConverter_Impl2::OleConverter_Impl2( const Reference<XMultiServiceFactory> &smgr):
367 UnoConversionUtilities<OleConverter_Impl2>( smgr)
370 // library unloading support
371 globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
374 // The XMultiServiceFactory is later set by XInitialization
375 OleConverter_Impl2::OleConverter_Impl2( const Reference<XMultiServiceFactory>& smgr, sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass ):
376 UnoConversionUtilities<OleConverter_Impl2>( smgr, unoWrapperClass, comWrapperClass )
379 //library unloading support
380 globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
383 OleConverter_Impl2::~OleConverter_Impl2()
385 globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
388 // XBridgeSupplier --------------------------------------------------------------
389 Any SAL_CALL OleConverter_Impl2::createBridge(const Any& modelDepObject,
390 const Sequence< sal_Int8 >& ProcessId,
391 sal_Int16 sourceModelType,
392 sal_Int16 destModelType)
393 throw (IllegalArgumentException,
394 RuntimeException )
396 Any ret;
397 sal_uInt8 arId[16];
398 rtl_getGlobalProcessId( arId );
400 Sequence< sal_Int8 > seqProcessId( (sal_Int8*)arId, 16);
402 if ( seqProcessId == ProcessId)
404 if (sourceModelType == UNO)
406 if (destModelType == UNO)
408 // same model -> copy value only
409 ret = modelDepObject;
411 else if (destModelType == OLE)
413 // convert UNO any into variant
414 VARIANT* pVariant = (VARIANT*) CoTaskMemAlloc(sizeof(VARIANT));
415 VariantInit( pVariant);
418 anyToVariant( pVariant, modelDepObject);
420 catch(...)
422 CoTaskMemFree(pVariant);
423 throw IllegalArgumentException();
425 ret.setValue((void*) &pVariant, getCppuType((sal_uInt32*)0));
427 else
428 throw IllegalArgumentException();
430 else if (sourceModelType == OLE)
432 if (modelDepObject.getValueType() != getCppuType((sal_uInt32*)0))
434 throw IllegalArgumentException();
436 else if (destModelType == OLE)
438 // same model -> copy value only
439 VARIANT* pVariant = (VARIANT*) CoTaskMemAlloc(sizeof(VARIANT));
441 if (NOERROR != VariantCopy(pVariant, *(VARIANT**)modelDepObject.getValue()))
443 CoTaskMemFree(pVariant);
444 throw(IllegalArgumentException());
446 else
448 ret.setValue((void*) &pVariant, getCppuType((sal_uInt32*)0));
451 else if (destModelType == UNO)
453 // convert variant into UNO any
454 VARIANT* pVariant = *(VARIANT**)modelDepObject.getValue();
457 variantToAny(pVariant, ret);
459 catch (CannotConvertException & e)
461 throw IllegalArgumentException(
462 e.Message, 0, -1);
465 else
466 throw IllegalArgumentException();
469 else
470 throw IllegalArgumentException();
473 return ret;
477 // XInitialize ------------------------------------------------------------------------------
478 // the first argument is an XMultiServiceFactory if at all
479 void SAL_CALL OleConverter_Impl2::initialize( const Sequence< Any >& aArguments )
480 throw(Exception, RuntimeException)
482 if( aArguments.getLength() == 1 && aArguments[0].getValueTypeClass() == TypeClass_INTERFACE)
484 Reference < XInterface > xInt;
485 aArguments[0] >>= xInt;
486 Reference <XMultiServiceFactory> xMulti( xInt, UNO_QUERY);
487 m_smgrRemote= xMulti;
491 // UnoConversionUtilities -------------------------------------------------------------------
492 Reference< XInterface > OleConverter_Impl2::createUnoWrapperInstance()
494 if( m_nUnoWrapperClass == INTERFACE_OLE_WRAPPER_IMPL)
496 Reference<XWeak> xWeak= static_cast<XWeak*>( new InterfaceOleWrapper_Impl(
497 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
498 return Reference<XInterface>( xWeak, UNO_QUERY);
500 else if( m_nUnoWrapperClass == UNO_OBJECT_WRAPPER_REMOTE_OPT)
502 Reference<XWeak> xWeak= static_cast<XWeak*>( new UnoObjectWrapperRemoteOpt(
503 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
504 return Reference<XInterface>( xWeak, UNO_QUERY);
506 else
507 return Reference<XInterface>();
510 Reference< XInterface > OleConverter_Impl2::createComWrapperInstance()
512 Reference<XWeak> xWeak= static_cast<XWeak*>( new IUnknownWrapper_Impl(
513 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
514 return Reference<XInterface>( xWeak, UNO_QUERY);
519 /*****************************************************************************
521 class implementation OleClient_Impl
523 *****************************************************************************/
525 OleClient_Impl::OleClient_Impl( const Reference<XMultiServiceFactory>& smgr):
526 UnoConversionUtilities<OleClient_Impl>( smgr)
528 // library unloading support
529 globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
530 Reference<XInterface> xInt;// = m_smgr->createInstance(L"com.sun.star.bridge.OleBridgeSupplier2");
532 if (xInt.is())
534 Any a= xInt->queryInterface(getCppuType(
535 reinterpret_cast<Reference<XBridgeSupplier2>*>(0)));
536 a >>= m_bridgeSupplier;
540 OleClient_Impl::~OleClient_Impl()
542 // library unloading support
543 globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
546 Sequence< OUString > SAL_CALL OleClient_Impl::getAvailableServiceNames() throw( RuntimeException )
548 Sequence< OUString > ret;
550 return ret;
554 OUString OleClient_Impl::getImplementationName()
556 return OUString(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.comp.ole.OleClient"));
559 Reference<XInterface> SAL_CALL OleClient_Impl::createInstance(const OUString& ServiceSpecifier) throw (Exception, RuntimeException )
561 Reference<XInterface> ret;
562 HRESULT result;
563 IUnknown* pUnknown = NULL;
564 CLSID classId;
566 o2u_attachCurrentThread();
568 result = CLSIDFromProgID(
569 reinterpret_cast<LPCWSTR>(ServiceSpecifier.getStr()), //Pointer to the ProgID
570 &classId); //Pointer to the CLSID
573 if (result == NOERROR)
575 result = CoCreateInstance(
576 classId, //Class identifier (CLSID) of the object
577 NULL, //Pointer to whether object is or isn't part of an aggregate
578 CLSCTX_SERVER, //Context for running executable code
579 IID_IUnknown, //Reference to the identifier of the interface
580 (void**)&pUnknown); //Address of output variable that receives
581 // the interface pointer requested in riid
584 if (pUnknown != NULL)
586 Any any;
587 CComVariant variant;
589 V_VT(&variant) = VT_UNKNOWN;
590 V_UNKNOWN(&variant) = pUnknown;
591 // AddRef for Variant
592 pUnknown->AddRef();
594 // When the object is wrapped, then its refcount is increased
595 variantToAny(&variant, any);
596 if (any.getValueTypeClass() == TypeClass_INTERFACE)
598 any >>= ret;
600 pUnknown->Release(); // CoCreateInstance
603 return ret;
606 Reference<XInterface> SAL_CALL OleClient_Impl::createInstanceWithArguments(const OUString& ServiceSpecifier, const Sequence< Any >& /*Arguments*/) throw (Exception, RuntimeException)
608 return createInstance( ServiceSpecifier);
611 // UnoConversionUtilities -----------------------------------------------------------------------------
612 Reference< XInterface > OleClient_Impl::createUnoWrapperInstance()
614 if( m_nUnoWrapperClass == INTERFACE_OLE_WRAPPER_IMPL)
616 Reference<XWeak> xWeak= static_cast<XWeak*>( new InterfaceOleWrapper_Impl(
617 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
618 return Reference<XInterface>( xWeak, UNO_QUERY);
620 else if( m_nUnoWrapperClass == UNO_OBJECT_WRAPPER_REMOTE_OPT)
622 Reference<XWeak> xWeak= static_cast<XWeak*>( new UnoObjectWrapperRemoteOpt(
623 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
624 return Reference<XInterface>( xWeak, UNO_QUERY);
626 else
627 return Reference< XInterface>();
629 // UnoConversionUtilities -----------------------------------------------------------------------------
630 Reference< XInterface > OleClient_Impl::createComWrapperInstance( )
632 Reference<XWeak> xWeak= static_cast<XWeak*>( new IUnknownWrapper_Impl(
633 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
634 return Reference<XInterface>( xWeak, UNO_QUERY);
639 /*****************************************************************************
641 class implementation OleServer_Impl
643 *****************************************************************************/
645 OleServer_Impl::OleServer_Impl( const Reference<XMultiServiceFactory>& smgr):
646 m_smgr( smgr)
648 //library unloading support
649 globalModuleCount.modCnt.acquire( &globalModuleCount.modCnt);
650 Reference<XInterface> xInt = m_smgr->createInstance(reinterpret_cast<const sal_Unicode*>(L"com.sun.star.bridge.oleautomation.BridgeSupplier"));
652 if (xInt.is())
654 Any a= xInt->queryInterface( getCppuType(
655 reinterpret_cast< Reference<XBridgeSupplier2>*>(0)));
656 a >>= m_bridgeSupplier;
659 #ifndef OWNGUID
660 sal_Bool bOLERegister = sal_False;
661 #else
662 sal_Bool bOLERegister = sal_True;
663 #endif
664 sal_Bool ret = provideInstance( m_smgr, (GUID*)&OID_ServiceManager, bOLERegister );
665 (void)ret;
668 OleServer_Impl::~OleServer_Impl()
670 while (!m_wrapperList.empty())
672 (*m_wrapperList.begin())->deregisterClass();
673 (*m_wrapperList.begin())->Release();
674 m_wrapperList.pop_front();
676 //library unloading support
677 globalModuleCount.modCnt.release( &globalModuleCount.modCnt);
679 // XInterface --------------------------------------------------
680 Any SAL_CALL OleServer_Impl::queryInterface( const Type& aType ) throw(RuntimeException)
682 Any a= ::cppu::queryInterface( aType, static_cast<XTypeProvider*>(this));
683 if( a == Any())
684 return OWeakObject::queryInterface( aType);
685 else
686 return a;
688 void SAL_CALL OleServer_Impl::acquire( ) throw()
690 OWeakObject::acquire();
692 void SAL_CALL OleServer_Impl::release( ) throw ()
694 OWeakObject::release();
698 // XTypeProvider --------------------------------------------------
699 Sequence< Type > SAL_CALL OleServer_Impl::getTypes( ) throw(RuntimeException)
701 static OTypeCollection *pCollection = 0;
702 if( ! pCollection )
704 MutexGuard guard( Mutex::getGlobalMutex() );
705 if( ! pCollection )
707 static OTypeCollection collection(
708 getCppuType(reinterpret_cast< Reference< XWeak>*>(0)),
709 getCppuType(reinterpret_cast< Reference< XTypeProvider>*>(0)) );
710 pCollection = &collection;
713 return (*pCollection).getTypes();
715 Sequence< sal_Int8 > SAL_CALL OleServer_Impl::getImplementationId() throw(RuntimeException)
717 static OImplementationId *pId = 0;
718 if( ! pId )
720 MutexGuard guard( Mutex::getGlobalMutex() );
721 if( ! pId )
723 static OImplementationId id( sal_False );
724 pId = &id;
727 return (*pId).getImplementationId();
731 sal_Bool OleServer_Impl::provideService(const Reference<XSingleServiceFactory>& xSFact, GUID* guid)
733 IClassFactoryWrapper* pFac = new ProviderOleWrapper_Impl( m_smgr, xSFact, guid);
735 pFac->AddRef();
737 m_wrapperList.push_back(pFac);
739 return pFac->registerClass();
742 sal_Bool OleServer_Impl::provideInstance(const Reference<XInterface>& xInst, GUID* guid, sal_Bool bAsApplication )
744 IClassFactoryWrapper* pFac = new OneInstanceOleWrapper_Impl( m_smgr, xInst, guid, bAsApplication );
746 pFac->AddRef();
747 m_wrapperList.push_back(pFac);
749 return pFac->registerClass();
754 } // end namespace