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>
38 #include <embservconst.h>
40 static const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
41 &OID_WriterTextServer
,
42 &OID_WriterOASISTextServer
,
46 &OID_DrawingOASISServer
,
47 &OID_PresentationServer
,
48 &OID_PresentationOASISServer
,
53 static HINSTANCE g_hInstance
= NULL
;
54 static ULONG g_nObj
= 0;
55 static ULONG g_nLock
= 0;
59 void FillCharFromInt( int nValue
, char* pBuf
, int nLen
)
64 char nSign
= ( nValue
/ ( 1 << ( ( nLen
- nInd
) * 4 ) ) ) % 16;
65 if ( nSign
>= 0 && nSign
<= 9 )
66 pBuf
[nInd
] = nSign
+ '0';
67 else if ( nSign
>= 10 && nSign
<= 15 )
68 pBuf
[nInd
] = nSign
- 10 + 'a';
74 int GetStringFromClassID( const GUID
& guid
, char* pBuf
, int nLen
)
76 // is not allowed to insert
81 FillCharFromInt( guid
.Data1
, &pBuf
[1], 8 );
83 FillCharFromInt( guid
.Data2
, &pBuf
[10], 4 );
85 FillCharFromInt( guid
.Data3
, &pBuf
[15], 4 );
89 for ( nInd
= 0; nInd
< 2 ; nInd
++ )
90 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 2*nInd
], 2 );
92 for ( nInd
= 2; nInd
< 8 ; nInd
++ )
93 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 1 + 2*nInd
], 2 );
99 HRESULT
WriteLibraryToRegistry( char* pLibrary
, DWORD nLen
)
101 HRESULT hRes
= E_FAIL
;
102 if ( pLibrary
&& nLen
)
107 for ( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
109 char* pSubKey
= "Software\\Classes\\CLSID\\.....................................\\InprocHandler32";
111 int nGuidLen
= GetStringFromClassID( *guidList
[nInd
], &pSubKey
[23], 38 );
113 BOOL bLocalSuccess
= FALSE
;
114 if ( nGuidLen
&& nGuidLen
== 38 )
116 if ( ERROR_SUCCESS
== RegOpenKey( HKEY_LOCAL_MACHINE
, pSubKey
, &hKey
) )
118 if ( ERROR_SUCCESS
== RegSetValueEx( hKey
, "", 0, REG_SZ
, (const BYTE
*)pLibrary
, nLen
) )
119 bLocalSuccess
= TRUE
;
129 if ( !bLocalSuccess
)
138 // ===========================
139 // InprocEmbedProvider_Impl declaration
140 // ===========================
145 class InprocEmbedProvider_Impl
: public IClassFactory
, public InprocCountedObject_Impl
149 InprocEmbedProvider_Impl( const GUID
& guid
);
150 virtual ~InprocEmbedProvider_Impl();
152 /* IUnknown methods */
153 STDMETHOD(QueryInterface
)(REFIID riid
, LPVOID FAR
* ppvObj
);
154 STDMETHOD_(ULONG
, AddRef
)();
155 STDMETHOD_(ULONG
, Release
)();
157 /* IClassFactory methods */
158 STDMETHOD(CreateInstance
)(IUnknown FAR
* punkOuter
, REFIID riid
, void FAR
* FAR
* ppv
);
159 STDMETHOD(LockServer
)(int fLock
);
166 }; // namespace inprocserv
169 // ===========================
171 // ===========================
173 // -------------------------------------------------------------------------------
174 extern "C" BOOL WINAPI
DllMain( HINSTANCE hInstance
, DWORD dwReason
, LPVOID
/*lpReserved*/ )
176 if (dwReason
== DLL_PROCESS_ATTACH
)
178 g_hInstance
= hInstance
;
180 else if (dwReason
== DLL_PROCESS_DETACH
)
187 // -------------------------------------------------------------------------------
188 extern "C" STDAPI
DllGetClassObject( REFCLSID rclsid
, REFIID riid
, LPVOID
* ppv
)
190 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
191 if ( *guidList
[nInd
] == rclsid
)
193 if ( !IsEqualIID( riid
, IID_IUnknown
) && !IsEqualIID( riid
, IID_IClassFactory
) )
194 return E_NOINTERFACE
;
196 *ppv
= new inprocserv::InprocEmbedProvider_Impl( rclsid
);
198 return E_OUTOFMEMORY
;
200 ((LPUNKNOWN
)*ppv
)->AddRef();
207 // -------------------------------------------------------------------------------
208 extern "C" STDAPI
DllCanUnloadNow()
210 if ( !g_nObj
&& !g_nLock
)
216 // -------------------------------------------------------------------------------
217 STDAPI
DllRegisterServer( void )
220 HMODULE aCurModule
= GetModuleHandleA( "inprocserv.dll" );
223 DWORD nLen
= GetModuleFileNameA( aCurModule
, aLibPath
, 1019 );
224 if ( nLen
&& nLen
< 1019 )
226 aLibPath
[nLen
++] = 0;
227 return WriteLibraryToRegistry( aLibPath
, nLen
);
234 // -------------------------------------------------------------------------------
235 STDAPI
DllUnregisterServer( void )
237 return WriteLibraryToRegistry( "ole32.dll", 10 );
240 // ===========================
241 // End of entry points
242 // ===========================
247 // ===========================
248 // InprocCountedObject_Impl implementation
249 // ===========================
251 // -------------------------------------------------------------------------------
252 InprocCountedObject_Impl::InprocCountedObject_Impl()
257 // -------------------------------------------------------------------------------
258 InprocCountedObject_Impl::~InprocCountedObject_Impl()
263 // ===========================
264 // InprocEmbedProvider_Impl implementation
265 // ===========================
267 // -------------------------------------------------------------------------------
268 InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID
& guid
)
274 // -------------------------------------------------------------------------------
275 InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
280 // -------------------------------------------------------------------------------
281 STDMETHODIMP
InprocEmbedProvider_Impl::QueryInterface( REFIID riid
, void FAR
* FAR
* ppv
)
283 if(IsEqualIID(riid
, IID_IUnknown
))
286 *ppv
= (IUnknown
*) this;
289 else if (IsEqualIID(riid
, IID_IClassFactory
))
292 *ppv
= (IClassFactory
*) this;
297 return E_NOINTERFACE
;
300 // -------------------------------------------------------------------------------
301 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::AddRef()
306 // -------------------------------------------------------------------------------
307 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::Release()
309 sal_Int32 nCount
= --m_refCount
;
315 // -------------------------------------------------------------------------------
316 STDMETHODIMP
InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
320 // TODO/LATER: should the aggregation be supported?
321 // if ( punkOuter != NULL && riid != IID_IUnknown )
322 // return E_NOINTERFACE;
323 if ( punkOuter
!= NULL
)
324 return CLASS_E_NOAGGREGATION
;
326 InprocEmbedDocument_Impl
* pEmbedDocument
= new InprocEmbedDocument_Impl( m_guid
);
327 if ( !pEmbedDocument
)
328 return E_OUTOFMEMORY
;
330 pEmbedDocument
->AddRef();
331 HRESULT hr
= pEmbedDocument
->Init();
332 if ( SUCCEEDED( hr
) )
333 hr
= pEmbedDocument
->QueryInterface( riid
, ppv
);
334 pEmbedDocument
->Release();
336 if ( !SUCCEEDED( hr
) )
342 // -------------------------------------------------------------------------------
343 STDMETHODIMP
InprocEmbedProvider_Impl::LockServer( int fLock
)
353 }; // namespace inprocserv