update dev300-m58
[ooovba.git] / embedserv / source / inprocserv / dllentry.cxx
blobf8630a82623003427b0c678a30bd6657e36bddfb
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 #ifdef __MINGW32__
39 #define INITGUID
40 #endif
41 #include <embservconst.h>
43 static const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
44 &OID_WriterTextServer,
45 &OID_WriterOASISTextServer,
46 &OID_CalcServer,
47 &OID_CalcOASISServer,
48 &OID_DrawingServer,
49 &OID_DrawingOASISServer,
50 &OID_PresentationServer,
51 &OID_PresentationOASISServer,
52 &OID_MathServer,
53 &OID_MathOASISServer
56 static HINSTANCE g_hInstance = NULL;
57 static ULONG g_nObj = 0;
58 static ULONG g_nLock = 0;
61 namespace {
62 void FillCharFromInt( int nValue, char* pBuf, int nLen )
64 int nInd = 0;
65 while( nInd < 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';
73 nInd++;
77 int GetStringFromClassID( const GUID& guid, char* pBuf, int nLen )
79 // is not allowed to insert
80 if ( nLen < 38 )
81 return 0;
83 pBuf[0] = '{';
84 FillCharFromInt( guid.Data1, &pBuf[1], 8 );
85 pBuf[9] = '-';
86 FillCharFromInt( guid.Data2, &pBuf[10], 4 );
87 pBuf[14] = '-';
88 FillCharFromInt( guid.Data3, &pBuf[15], 4 );
89 pBuf[19] = '-';
91 int nInd = 0;
92 for ( nInd = 0; nInd < 2 ; nInd++ )
93 FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 2*nInd], 2 );
94 pBuf[24] = '-';
95 for ( nInd = 2; nInd < 8 ; nInd++ )
96 FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 1 + 2*nInd], 2 );
97 pBuf[37] = '}';
99 return 38;
102 HRESULT WriteLibraryToRegistry( char* pLibrary, DWORD nLen )
104 HRESULT hRes = E_FAIL;
105 if ( pLibrary && nLen )
107 HKEY hKey = NULL;
109 hRes = S_OK;
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;
125 if ( hKey )
127 RegCloseKey( hKey );
128 hKey = NULL;
132 if ( !bLocalSuccess )
133 hRes = E_FAIL;
137 return hRes;
141 // ===========================
142 // InprocEmbedProvider_Impl declaration
143 // ===========================
145 namespace inprocserv
148 class InprocEmbedProvider_Impl : public IClassFactory, public InprocCountedObject_Impl
150 public:
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);
164 protected:
166 ULONG m_refCount;
167 GUID m_guid;
169 }; // namespace inprocserv
172 // ===========================
173 // Entry points
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)
187 return TRUE; // ok
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 );
200 if ( *ppv == NULL )
201 return E_OUTOFMEMORY;
203 ((LPUNKNOWN)*ppv)->AddRef();
204 return S_OK;
207 return E_FAIL;
210 // -------------------------------------------------------------------------------
211 extern "C" STDAPI DllCanUnloadNow()
213 if ( !g_nObj && !g_nLock )
214 return S_OK;
216 return S_FALSE;
219 // -------------------------------------------------------------------------------
220 STDAPI DllRegisterServer( void )
222 char aLibPath[1024];
223 HMODULE aCurModule = GetModuleHandleA( "inprocserv.dll" );
224 if( aCurModule )
226 DWORD nLen = GetModuleFileNameA( aCurModule, aLibPath, 1019 );
227 if ( nLen && nLen < 1019 )
229 aLibPath[nLen++] = 0;
230 return WriteLibraryToRegistry( aLibPath, nLen );
234 return E_FAIL;
237 // -------------------------------------------------------------------------------
238 STDAPI DllUnregisterServer( void )
240 return WriteLibraryToRegistry( "ole32.dll", 10 );
243 // ===========================
244 // End of entry points
245 // ===========================
247 namespace inprocserv
250 // ===========================
251 // InprocCountedObject_Impl implementation
252 // ===========================
254 // -------------------------------------------------------------------------------
255 InprocCountedObject_Impl::InprocCountedObject_Impl()
257 g_nObj++;
260 // -------------------------------------------------------------------------------
261 InprocCountedObject_Impl::~InprocCountedObject_Impl()
263 g_nObj--;
266 // ===========================
267 // InprocEmbedProvider_Impl implementation
268 // ===========================
270 // -------------------------------------------------------------------------------
271 InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID& guid )
272 : m_refCount( 0 )
273 , m_guid( guid )
277 // -------------------------------------------------------------------------------
278 InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
282 // IUnknown
283 // -------------------------------------------------------------------------------
284 STDMETHODIMP InprocEmbedProvider_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
286 if(IsEqualIID(riid, IID_IUnknown))
288 AddRef();
289 *ppv = (IUnknown*) this;
290 return S_OK;
292 else if (IsEqualIID(riid, IID_IClassFactory))
294 AddRef();
295 *ppv = (IClassFactory*) this;
296 return S_OK;
299 *ppv = NULL;
300 return E_NOINTERFACE;
303 // -------------------------------------------------------------------------------
304 STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::AddRef()
306 return ++m_refCount;
309 // -------------------------------------------------------------------------------
310 STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::Release()
312 sal_Int32 nCount = --m_refCount;
313 if ( nCount == 0 )
314 delete this;
315 return nCount;
318 // -------------------------------------------------------------------------------
319 STDMETHODIMP InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR* punkOuter,
320 REFIID riid,
321 void FAR* FAR* ppv)
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 ) )
340 *ppv = NULL;
342 return hr;
345 // -------------------------------------------------------------------------------
346 STDMETHODIMP InprocEmbedProvider_Impl::LockServer( int fLock )
348 if ( fLock )
349 g_nLock++;
350 else
351 g_nLock--;
353 return S_OK;
356 }; // namespace inprocserv