1*f78e906fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f78e906fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f78e906fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f78e906fSAndrew Rist  * distributed with this work for additional information
6*f78e906fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f78e906fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f78e906fSAndrew Rist  * "License"); you may not use this file except in compliance
9*f78e906fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f78e906fSAndrew Rist  *
11*f78e906fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f78e906fSAndrew Rist  *
13*f78e906fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f78e906fSAndrew Rist  * software distributed under the License is distributed on an
15*f78e906fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f78e906fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*f78e906fSAndrew Rist  * specific language governing permissions and limitations
18*f78e906fSAndrew Rist  * under the License.
19*f78e906fSAndrew Rist  *
20*f78e906fSAndrew Rist  *************************************************************/
21*f78e906fSAndrew Rist 
22*f78e906fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir #include <inprocembobj.h>
26cdf0e10cSrcweir #ifdef __MINGW32__
27cdf0e10cSrcweir #define INITGUID
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <embservconst.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir static const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
32cdf0e10cSrcweir 	&OID_WriterTextServer,
33cdf0e10cSrcweir 	&OID_WriterOASISTextServer,
34cdf0e10cSrcweir 	&OID_CalcServer,
35cdf0e10cSrcweir 	&OID_CalcOASISServer,
36cdf0e10cSrcweir 	&OID_DrawingServer,
37cdf0e10cSrcweir 	&OID_DrawingOASISServer,
38cdf0e10cSrcweir 	&OID_PresentationServer,
39cdf0e10cSrcweir 	&OID_PresentationOASISServer,
40cdf0e10cSrcweir 	&OID_MathServer,
41cdf0e10cSrcweir 	&OID_MathOASISServer
42cdf0e10cSrcweir };
43cdf0e10cSrcweir 
44cdf0e10cSrcweir static HINSTANCE g_hInstance = NULL;
45cdf0e10cSrcweir static ULONG g_nObj = 0;
46cdf0e10cSrcweir static ULONG g_nLock = 0;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace {
FillCharFromInt(int nValue,char * pBuf,int nLen)50cdf0e10cSrcweir     void FillCharFromInt( int nValue, char* pBuf, int nLen )
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         int nInd = 0;
53cdf0e10cSrcweir         while( nInd < nLen )
54cdf0e10cSrcweir         {
55cdf0e10cSrcweir             char nSign = ( nValue / ( 1 << ( ( nLen - nInd ) * 4 ) ) ) % 16;
56cdf0e10cSrcweir             if ( nSign >= 0 && nSign <= 9 )
57cdf0e10cSrcweir                 pBuf[nInd] = nSign + '0';
58cdf0e10cSrcweir             else if ( nSign >= 10 && nSign <= 15 )
59cdf0e10cSrcweir                 pBuf[nInd] = nSign - 10 + 'a';
60cdf0e10cSrcweir 
61cdf0e10cSrcweir             nInd++;
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
GetStringFromClassID(const GUID & guid,char * pBuf,int nLen)65cdf0e10cSrcweir     int GetStringFromClassID( const GUID& guid, char* pBuf, int nLen )
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         // is not allowed to insert
68cdf0e10cSrcweir         if ( nLen < 38 )
69cdf0e10cSrcweir             return 0;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         pBuf[0] = '{';
72cdf0e10cSrcweir         FillCharFromInt( guid.Data1, &pBuf[1], 8 );
73cdf0e10cSrcweir         pBuf[9] = '-';
74cdf0e10cSrcweir         FillCharFromInt( guid.Data2, &pBuf[10], 4 );
75cdf0e10cSrcweir         pBuf[14] = '-';
76cdf0e10cSrcweir         FillCharFromInt( guid.Data3, &pBuf[15], 4 );
77cdf0e10cSrcweir         pBuf[19] = '-';
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         int nInd = 0;
80cdf0e10cSrcweir         for ( nInd = 0; nInd < 2 ; nInd++ )
81cdf0e10cSrcweir             FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 2*nInd], 2 );
82cdf0e10cSrcweir         pBuf[24] = '-';
83cdf0e10cSrcweir         for ( nInd = 2; nInd < 8 ; nInd++ )
84cdf0e10cSrcweir             FillCharFromInt( guid.Data4[nInd], &pBuf[20 + 1 + 2*nInd], 2 );
85cdf0e10cSrcweir         pBuf[37] = '}';
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         return 38;
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 
WriteLibraryToRegistry(char * pLibrary,DWORD nLen)90cdf0e10cSrcweir     HRESULT WriteLibraryToRegistry( char* pLibrary, DWORD nLen )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         HRESULT hRes = E_FAIL;
93cdf0e10cSrcweir         if ( pLibrary && nLen )
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             HKEY hKey = NULL;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             hRes = S_OK;
98cdf0e10cSrcweir             for ( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 char* pSubKey = "Software\\Classes\\CLSID\\.....................................\\InprocHandler32";
101cdf0e10cSrcweir 
102cdf0e10cSrcweir                 int nGuidLen = GetStringFromClassID( *guidList[nInd], &pSubKey[23], 38 );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir                 BOOL bLocalSuccess = FALSE;
105cdf0e10cSrcweir                 if ( nGuidLen && nGuidLen == 38 )
106cdf0e10cSrcweir                 {
107cdf0e10cSrcweir                     if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE, pSubKey, &hKey ) )
108cdf0e10cSrcweir                     {
109cdf0e10cSrcweir                         if ( ERROR_SUCCESS == RegSetValueEx( hKey, "", 0, REG_SZ, (const BYTE*)pLibrary, nLen ) )
110cdf0e10cSrcweir                             bLocalSuccess = TRUE;
111cdf0e10cSrcweir                     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir                     if ( hKey )
114cdf0e10cSrcweir                     {
115cdf0e10cSrcweir                         RegCloseKey( hKey );
116cdf0e10cSrcweir                         hKey = NULL;
117cdf0e10cSrcweir                     }
118cdf0e10cSrcweir                 }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir                 if ( !bLocalSuccess )
121cdf0e10cSrcweir                     hRes = E_FAIL;
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         return hRes;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir };
128cdf0e10cSrcweir 
129cdf0e10cSrcweir // ===========================
130cdf0e10cSrcweir // InprocEmbedProvider_Impl declaration
131cdf0e10cSrcweir // ===========================
132cdf0e10cSrcweir 
133cdf0e10cSrcweir namespace inprocserv
134cdf0e10cSrcweir {
135cdf0e10cSrcweir 
136cdf0e10cSrcweir class InprocEmbedProvider_Impl : public IClassFactory, public InprocCountedObject_Impl
137cdf0e10cSrcweir {
138cdf0e10cSrcweir public:
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	InprocEmbedProvider_Impl( const GUID& guid );
141cdf0e10cSrcweir 	virtual ~InprocEmbedProvider_Impl();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	/* IUnknown methods */
144cdf0e10cSrcweir 	STDMETHOD(QueryInterface)(REFIID riid, LPVOID FAR * ppvObj);
145cdf0e10cSrcweir 	STDMETHOD_(ULONG, AddRef)();
146cdf0e10cSrcweir 	STDMETHOD_(ULONG, Release)();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	/* IClassFactory methods */
149cdf0e10cSrcweir 	STDMETHOD(CreateInstance)(IUnknown FAR* punkOuter, REFIID riid, void FAR* FAR* ppv);
150cdf0e10cSrcweir 	STDMETHOD(LockServer)(int fLock);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir protected:
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	ULONG               m_refCount;
155cdf0e10cSrcweir 	GUID				m_guid;
156cdf0e10cSrcweir };
157cdf0e10cSrcweir }; // namespace inprocserv
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // ===========================
161cdf0e10cSrcweir // Entry points
162cdf0e10cSrcweir // ===========================
163cdf0e10cSrcweir 
164cdf0e10cSrcweir // -------------------------------------------------------------------------------
DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID)165cdf0e10cSrcweir extern "C" BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/ )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     if (dwReason == DLL_PROCESS_ATTACH)
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         g_hInstance = hInstance;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     else if (dwReason == DLL_PROCESS_DETACH)
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     return TRUE;    // ok
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // -------------------------------------------------------------------------------
DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID * ppv)179cdf0e10cSrcweir extern "C" STDAPI DllGetClassObject( REFCLSID rclsid, REFIID riid, LPVOID* ppv )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
182cdf0e10cSrcweir 		 if ( *guidList[nInd] == rclsid )
183cdf0e10cSrcweir          {
184cdf0e10cSrcweir             if ( !IsEqualIID( riid, IID_IUnknown ) && !IsEqualIID( riid, IID_IClassFactory ) )
185cdf0e10cSrcweir                 return E_NOINTERFACE;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir             *ppv = new inprocserv::InprocEmbedProvider_Impl( rclsid );
188cdf0e10cSrcweir             if ( *ppv == NULL )
189cdf0e10cSrcweir                 return E_OUTOFMEMORY;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             ((LPUNKNOWN)*ppv)->AddRef();
192cdf0e10cSrcweir             return S_OK;
193cdf0e10cSrcweir          }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     return E_FAIL;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir // -------------------------------------------------------------------------------
DllCanUnloadNow()199cdf0e10cSrcweir extern "C" STDAPI DllCanUnloadNow()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     if ( !g_nObj && !g_nLock )
202cdf0e10cSrcweir         return S_OK;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     return S_FALSE;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir // -------------------------------------------------------------------------------
DllRegisterServer(void)208cdf0e10cSrcweir STDAPI DllRegisterServer( void )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	char aLibPath[1024];
211cdf0e10cSrcweir 	HMODULE aCurModule = GetModuleHandleA( "inprocserv.dll" );
212cdf0e10cSrcweir 	if( aCurModule )
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         DWORD nLen = GetModuleFileNameA( aCurModule, aLibPath, 1019 );
215cdf0e10cSrcweir         if ( nLen && nLen < 1019 )
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             aLibPath[nLen++] = 0;
218cdf0e10cSrcweir             return WriteLibraryToRegistry( aLibPath, nLen );
219cdf0e10cSrcweir         }
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     return E_FAIL;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir // -------------------------------------------------------------------------------
DllUnregisterServer(void)226cdf0e10cSrcweir STDAPI DllUnregisterServer( void )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     return WriteLibraryToRegistry( "ole32.dll", 10 );
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir // ===========================
232cdf0e10cSrcweir // End of entry points
233cdf0e10cSrcweir // ===========================
234cdf0e10cSrcweir 
235cdf0e10cSrcweir namespace inprocserv
236cdf0e10cSrcweir {
237cdf0e10cSrcweir 
238cdf0e10cSrcweir // ===========================
239cdf0e10cSrcweir // InprocCountedObject_Impl implementation
240cdf0e10cSrcweir // ===========================
241cdf0e10cSrcweir 
242cdf0e10cSrcweir // -------------------------------------------------------------------------------
InprocCountedObject_Impl()243cdf0e10cSrcweir InprocCountedObject_Impl::InprocCountedObject_Impl()
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     g_nObj++;
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir // -------------------------------------------------------------------------------
~InprocCountedObject_Impl()249cdf0e10cSrcweir InprocCountedObject_Impl::~InprocCountedObject_Impl()
250cdf0e10cSrcweir {
251cdf0e10cSrcweir     g_nObj--;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir // ===========================
255cdf0e10cSrcweir // InprocEmbedProvider_Impl implementation
256cdf0e10cSrcweir // ===========================
257cdf0e10cSrcweir 
258cdf0e10cSrcweir // -------------------------------------------------------------------------------
InprocEmbedProvider_Impl(const GUID & guid)259cdf0e10cSrcweir InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID& guid )
260cdf0e10cSrcweir : m_refCount( 0 )
261cdf0e10cSrcweir , m_guid( guid )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir // -------------------------------------------------------------------------------
~InprocEmbedProvider_Impl()266cdf0e10cSrcweir InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
267cdf0e10cSrcweir {
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir // IUnknown
271cdf0e10cSrcweir // -------------------------------------------------------------------------------
QueryInterface(REFIID riid,void FAR * FAR * ppv)272cdf0e10cSrcweir STDMETHODIMP InprocEmbedProvider_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     if(IsEqualIID(riid, IID_IUnknown))
275cdf0e10cSrcweir 	{
276cdf0e10cSrcweir 		AddRef();
277cdf0e10cSrcweir 		*ppv = (IUnknown*) this;
278cdf0e10cSrcweir 		return S_OK;
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     else if (IsEqualIID(riid, IID_IClassFactory))
281cdf0e10cSrcweir 	{
282cdf0e10cSrcweir 		AddRef();
283cdf0e10cSrcweir 		*ppv = (IClassFactory*) this;
284cdf0e10cSrcweir 		return S_OK;
285cdf0e10cSrcweir 	}
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     *ppv = NULL;
288cdf0e10cSrcweir     return E_NOINTERFACE;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir // -------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)292cdf0e10cSrcweir STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::AddRef()
293cdf0e10cSrcweir {
294cdf0e10cSrcweir 	return ++m_refCount;
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir // -------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)298cdf0e10cSrcweir STDMETHODIMP_(ULONG) InprocEmbedProvider_Impl::Release()
299cdf0e10cSrcweir {
300cdf0e10cSrcweir 	sal_Int32 nCount = --m_refCount;
301cdf0e10cSrcweir 	if ( nCount == 0 )
302cdf0e10cSrcweir 		delete this;
303cdf0e10cSrcweir     return nCount;
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir // -------------------------------------------------------------------------------
CreateInstance(IUnknown FAR * punkOuter,REFIID riid,void FAR * FAR * ppv)307cdf0e10cSrcweir STDMETHODIMP InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR* punkOuter,
308cdf0e10cSrcweir                                                        REFIID riid,
309cdf0e10cSrcweir                                                        void FAR* FAR* ppv)
310cdf0e10cSrcweir {
311cdf0e10cSrcweir     // TODO/LATER: should the aggregation be supported?
312cdf0e10cSrcweir     // if ( punkOuter != NULL && riid != IID_IUnknown )
313cdf0e10cSrcweir     //     return E_NOINTERFACE;
314cdf0e10cSrcweir     if ( punkOuter != NULL )
315cdf0e10cSrcweir         return CLASS_E_NOAGGREGATION;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     InprocEmbedDocument_Impl* pEmbedDocument = new InprocEmbedDocument_Impl( m_guid );
318cdf0e10cSrcweir     if ( !pEmbedDocument )
319cdf0e10cSrcweir         return E_OUTOFMEMORY;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     pEmbedDocument->AddRef();
322cdf0e10cSrcweir     HRESULT hr = pEmbedDocument->Init();
323cdf0e10cSrcweir     if ( SUCCEEDED( hr ) )
324cdf0e10cSrcweir         hr = pEmbedDocument->QueryInterface( riid, ppv );
325cdf0e10cSrcweir 	pEmbedDocument->Release();
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 	if ( !SUCCEEDED( hr ) )
328cdf0e10cSrcweir         *ppv = NULL;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     return hr;
331cdf0e10cSrcweir }
332cdf0e10cSrcweir 
333cdf0e10cSrcweir // -------------------------------------------------------------------------------
LockServer(int fLock)334cdf0e10cSrcweir STDMETHODIMP InprocEmbedProvider_Impl::LockServer( int fLock )
335cdf0e10cSrcweir {
336cdf0e10cSrcweir     if ( fLock )
337cdf0e10cSrcweir         g_nLock++;
338cdf0e10cSrcweir     else
339cdf0e10cSrcweir         g_nLock--;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     return S_OK;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir 
344cdf0e10cSrcweir }; // namespace inprocserv
345cdf0e10cSrcweir 
346