Update to m13
[ooovba.git] / embedserv / source / inprocserv / dllentry.cxx
blobdae96955d25ff7923aaac54b6c8465167d700f3d
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: dllentry.cxx,v $
7 * $Revision: 1.1.8.2 $
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,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 #include <stdio.h>
37 #include <inprocembobj.h>
38 #include <embservconst.h>
40 static const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
41 &OID_WriterTextServer,
42 &OID_WriterOASISTextServer,
43 &OID_CalcServer,
44 &OID_CalcOASISServer,
45 &OID_DrawingServer,
46 &OID_DrawingOASISServer,
47 &OID_PresentationServer,
48 &OID_PresentationOASISServer,
49 &OID_MathServer,
50 &OID_MathOASISServer
53 static HINSTANCE g_hInstance = NULL;
54 static ULONG g_nObj = 0;
55 static ULONG g_nLock = 0;
58 namespace {
59 void FillCharFromInt( int nValue, char* pBuf, int nLen )
61 int nInd = 0;
62 while( nInd < 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';
70 nInd++;
74 int GetStringFromClassID( const GUID& guid, char* pBuf, int nLen )
76 // is not allowed to insert
77 if ( nLen < 38 )
78 return 0;
80 pBuf[0] = '{';
81 FillCharFromInt( guid.Data1, &pBuf[1], 8 );
82 pBuf[9] = '-';
83 FillCharFromInt( guid.Data2, &pBuf[10], 4 );
84 pBuf[14] = '-';
85 FillCharFromInt( guid.Data3, &pBuf[15], 4 );
86 pBuf[19] = '-';
88 int nInd = 0;
89 for ( nInd = 0; nInd < 2 ; nInd++ )
90 FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 2*nInd], 2 );
91 pBuf[24] = '-';
92 for ( nInd = 2; nInd < 8 ; nInd++ )
93 FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 1 + 2*nInd], 2 );
94 pBuf[37] = '}';
96 return 38;
99 HRESULT WriteLibraryToRegistry( char* pLibrary, DWORD nLen )
101 HRESULT hRes = E_FAIL;
102 if ( pLibrary && nLen )
104 HKEY hKey = NULL;
106 hRes = S_OK;
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;
122 if ( hKey )
124 RegCloseKey( hKey );
125 hKey = NULL;
129 if ( !bLocalSuccess )
130 hRes = E_FAIL;
134 return hRes;
138 // ===========================
139 // InprocEmbedProvider_Impl declaration
140 // ===========================
142 namespace inprocserv
145 class InprocEmbedProvider_Impl : public IClassFactory, public InprocCountedObject_Impl
147 public:
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);
161 protected:
163 ULONG m_refCount;
164 GUID m_guid;
166 }; // namespace inprocserv
169 // ===========================
170 // Entry points
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)
184 return TRUE; // ok
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 );
197 if ( *ppv == NULL )
198 return E_OUTOFMEMORY;
200 ((LPUNKNOWN)*ppv)->AddRef();
201 return S_OK;
204 return E_FAIL;
207 // -------------------------------------------------------------------------------
208 extern "C" STDAPI DllCanUnloadNow()
210 if ( !g_nObj && !g_nLock )
211 return S_OK;
213 return S_FALSE;
216 // -------------------------------------------------------------------------------
217 STDAPI DllRegisterServer( void )
219 char aLibPath[1024];
220 HMODULE aCurModule = GetModuleHandleA( "inprocserv.dll" );
221 if( aCurModule )
223 DWORD nLen = GetModuleFileNameA( aCurModule, aLibPath, 1019 );
224 if ( nLen && nLen < 1019 )
226 aLibPath[nLen++] = 0;
227 return WriteLibraryToRegistry( aLibPath, nLen );
231 return E_FAIL;
234 // -------------------------------------------------------------------------------
235 STDAPI DllUnregisterServer( void )
237 return WriteLibraryToRegistry( "ole32.dll", 10 );
240 // ===========================
241 // End of entry points
242 // ===========================
244 namespace inprocserv
247 // ===========================
248 // InprocCountedObject_Impl implementation
249 // ===========================
251 // -------------------------------------------------------------------------------
252 InprocCountedObject_Impl::InprocCountedObject_Impl()
254 g_nObj++;
257 // -------------------------------------------------------------------------------
258 InprocCountedObject_Impl::~InprocCountedObject_Impl()
260 g_nObj--;
263 // ===========================
264 // InprocEmbedProvider_Impl implementation
265 // ===========================
267 // -------------------------------------------------------------------------------
268 InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID& guid )
269 : m_refCount( 0 )
270 , m_guid( guid )
274 // -------------------------------------------------------------------------------
275 InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
279 // IUnknown
280 // -------------------------------------------------------------------------------
281 STDMETHODIMP InprocEmbedProvider_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
283 if(IsEqualIID(riid, IID_IUnknown))
285 AddRef();
286 *ppv = (IUnknown*) this;
287 return S_OK;
289 else if (IsEqualIID(riid, IID_IClassFactory))
291 AddRef();
292 *ppv = (IClassFactory*) this;
293 return S_OK;
296 *ppv = NULL;
297 return E_NOINTERFACE;
300 // -------------------------------------------------------------------------------
301 STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::AddRef()
303 return ++m_refCount;
306 // -------------------------------------------------------------------------------
307 STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::Release()
309 sal_Int32 nCount = --m_refCount;
310 if ( nCount == 0 )
311 delete this;
312 return nCount;
315 // -------------------------------------------------------------------------------
316 STDMETHODIMP InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR* punkOuter,
317 REFIID riid,
318 void FAR* FAR* ppv)
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 ) )
337 *ppv = NULL;
339 return hr;
342 // -------------------------------------------------------------------------------
343 STDMETHODIMP InprocEmbedProvider_Impl::LockServer( int fLock )
345 if ( fLock )
346 g_nLock++;
347 else
348 g_nLock--;
350 return S_OK;
353 }; // namespace inprocserv