1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: dllentry.cxx,v $
9 * last change: $Author: mav $ $Date: 2008/10/30 11:59:06 $
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 ************************************************************************/
37 #include <inprocembobj.h>
41 #include <embservconst.h>
43 static const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
44 &OID_WriterTextServer
,
45 &OID_WriterOASISTextServer
,
49 &OID_DrawingOASISServer
,
50 &OID_PresentationServer
,
51 &OID_PresentationOASISServer
,
56 static HINSTANCE g_hInstance
= NULL
;
57 static ULONG g_nObj
= 0;
58 static ULONG g_nLock
= 0;
62 void FillCharFromInt( int nValue
, char* pBuf
, int nLen
)
67 char nSign
= ( nValue
/ ( 1 << ( ( nLen
- nInd
) * 4 ) ) ) % 16;
68 if ( nSign
>= 0 && nSign
<= 9 )
69 pBuf
[nInd
] = nSign
+ '0';
70 else if ( nSign
>= 10 && nSign
<= 15 )
71 pBuf
[nInd
] = nSign
- 10 + 'a';
77 int GetStringFromClassID( const GUID
& guid
, char* pBuf
, int nLen
)
79 // is not allowed to insert
84 FillCharFromInt( guid
.Data1
, &pBuf
[1], 8 );
86 FillCharFromInt( guid
.Data2
, &pBuf
[10], 4 );
88 FillCharFromInt( guid
.Data3
, &pBuf
[15], 4 );
92 for ( nInd
= 0; nInd
< 2 ; nInd
++ )
93 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 2*nInd
], 2 );
95 for ( nInd
= 2; nInd
< 8 ; nInd
++ )
96 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 1 + 2*nInd
], 2 );
102 HRESULT
WriteLibraryToRegistry( char* pLibrary
, DWORD nLen
)
104 HRESULT hRes
= E_FAIL
;
105 if ( pLibrary
&& nLen
)
110 for ( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
112 char* pSubKey
= "Software\\Classes\\CLSID\\.....................................\\InprocHandler32";
114 int nGuidLen
= GetStringFromClassID( *guidList
[nInd
], &pSubKey
[23], 38 );
116 BOOL bLocalSuccess
= FALSE
;
117 if ( nGuidLen
&& nGuidLen
== 38 )
119 if ( ERROR_SUCCESS
== RegOpenKey( HKEY_LOCAL_MACHINE
, pSubKey
, &hKey
) )
121 if ( ERROR_SUCCESS
== RegSetValueEx( hKey
, "", 0, REG_SZ
, (const BYTE
*)pLibrary
, nLen
) )
122 bLocalSuccess
= TRUE
;
132 if ( !bLocalSuccess
)
141 // ===========================
142 // InprocEmbedProvider_Impl declaration
143 // ===========================
148 class InprocEmbedProvider_Impl
: public IClassFactory
, public InprocCountedObject_Impl
152 InprocEmbedProvider_Impl( const GUID
& guid
);
153 virtual ~InprocEmbedProvider_Impl();
155 /* IUnknown methods */
156 STDMETHOD(QueryInterface
)(REFIID riid
, LPVOID FAR
* ppvObj
);
157 STDMETHOD_(ULONG
, AddRef
)();
158 STDMETHOD_(ULONG
, Release
)();
160 /* IClassFactory methods */
161 STDMETHOD(CreateInstance
)(IUnknown FAR
* punkOuter
, REFIID riid
, void FAR
* FAR
* ppv
);
162 STDMETHOD(LockServer
)(int fLock
);
169 }; // namespace inprocserv
172 // ===========================
174 // ===========================
176 // -------------------------------------------------------------------------------
177 extern "C" BOOL WINAPI
DllMain( HINSTANCE hInstance
, DWORD dwReason
, LPVOID
/*lpReserved*/ )
179 if (dwReason
== DLL_PROCESS_ATTACH
)
181 g_hInstance
= hInstance
;
183 else if (dwReason
== DLL_PROCESS_DETACH
)
190 // -------------------------------------------------------------------------------
191 extern "C" STDAPI
DllGetClassObject( REFCLSID rclsid
, REFIID riid
, LPVOID
* ppv
)
193 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
194 if ( *guidList
[nInd
] == rclsid
)
196 if ( !IsEqualIID( riid
, IID_IUnknown
) && !IsEqualIID( riid
, IID_IClassFactory
) )
197 return E_NOINTERFACE
;
199 *ppv
= new inprocserv::InprocEmbedProvider_Impl( rclsid
);
201 return E_OUTOFMEMORY
;
203 ((LPUNKNOWN
)*ppv
)->AddRef();
210 // -------------------------------------------------------------------------------
211 extern "C" STDAPI
DllCanUnloadNow()
213 if ( !g_nObj
&& !g_nLock
)
219 // -------------------------------------------------------------------------------
220 STDAPI
DllRegisterServer( void )
223 HMODULE aCurModule
= GetModuleHandleA( "inprocserv.dll" );
226 DWORD nLen
= GetModuleFileNameA( aCurModule
, aLibPath
, 1019 );
227 if ( nLen
&& nLen
< 1019 )
229 aLibPath
[nLen
++] = 0;
230 return WriteLibraryToRegistry( aLibPath
, nLen
);
237 // -------------------------------------------------------------------------------
238 STDAPI
DllUnregisterServer( void )
240 return WriteLibraryToRegistry( "ole32.dll", 10 );
243 // ===========================
244 // End of entry points
245 // ===========================
250 // ===========================
251 // InprocCountedObject_Impl implementation
252 // ===========================
254 // -------------------------------------------------------------------------------
255 InprocCountedObject_Impl::InprocCountedObject_Impl()
260 // -------------------------------------------------------------------------------
261 InprocCountedObject_Impl::~InprocCountedObject_Impl()
266 // ===========================
267 // InprocEmbedProvider_Impl implementation
268 // ===========================
270 // -------------------------------------------------------------------------------
271 InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID
& guid
)
277 // -------------------------------------------------------------------------------
278 InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
283 // -------------------------------------------------------------------------------
284 STDMETHODIMP
InprocEmbedProvider_Impl::QueryInterface( REFIID riid
, void FAR
* FAR
* ppv
)
286 if(IsEqualIID(riid
, IID_IUnknown
))
289 *ppv
= (IUnknown
*) this;
292 else if (IsEqualIID(riid
, IID_IClassFactory
))
295 *ppv
= (IClassFactory
*) this;
300 return E_NOINTERFACE
;
303 // -------------------------------------------------------------------------------
304 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::AddRef()
309 // -------------------------------------------------------------------------------
310 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::Release()
312 sal_Int32 nCount
= --m_refCount
;
318 // -------------------------------------------------------------------------------
319 STDMETHODIMP
InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
323 // TODO/LATER: should the aggregation be supported?
324 // if ( punkOuter != NULL && riid != IID_IUnknown )
325 // return E_NOINTERFACE;
326 if ( punkOuter
!= NULL
)
327 return CLASS_E_NOAGGREGATION
;
329 InprocEmbedDocument_Impl
* pEmbedDocument
= new InprocEmbedDocument_Impl( m_guid
);
330 if ( !pEmbedDocument
)
331 return E_OUTOFMEMORY
;
333 pEmbedDocument
->AddRef();
334 HRESULT hr
= pEmbedDocument
->Init();
335 if ( SUCCEEDED( hr
) )
336 hr
= pEmbedDocument
->QueryInterface( riid
, ppv
);
337 pEmbedDocument
->Release();
339 if ( !SUCCEEDED( hr
) )
345 // -------------------------------------------------------------------------------
346 STDMETHODIMP
InprocEmbedProvider_Impl::LockServer( int fLock
)
356 }; // namespace inprocserv