1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/types.h"
23 #include <inprocembobj.h>
26 #define INPROC_DLLPUBLIC SAL_DLLPUBLIC_EXPORT
28 #define INPROC_DLLPUBLIC
30 #include <embservconst.h>
32 static const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
33 &OID_WriterTextServer
,
34 &OID_WriterOASISTextServer
,
38 &OID_DrawingOASISServer
,
39 &OID_PresentationServer
,
40 &OID_PresentationOASISServer
,
45 static HINSTANCE g_hInstance
= NULL
;
46 static ULONG g_nObj
= 0;
47 static ULONG g_nLock
= 0;
51 void FillCharFromInt( int nValue
, char* pBuf
, int nLen
)
56 char nSign
= ( nValue
/ ( 1 << ( ( nLen
- nInd
- 1 ) * 4 ) ) ) % 16;
57 if ( nSign
>= 0 && nSign
<= 9 )
58 pBuf
[nInd
] = nSign
+ '0';
59 else if ( nSign
>= 10 && nSign
<= 15 )
60 pBuf
[nInd
] = nSign
- 10 + 'a';
66 int GetStringFromClassID( const GUID
& guid
, char* pBuf
, int nLen
)
68 // is not allowed to insert
73 FillCharFromInt( guid
.Data1
, &pBuf
[1], 8 );
75 FillCharFromInt( guid
.Data2
, &pBuf
[10], 4 );
77 FillCharFromInt( guid
.Data3
, &pBuf
[15], 4 );
81 for ( nInd
= 0; nInd
< 2 ; nInd
++ )
82 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 2*nInd
], 2 );
84 for ( nInd
= 2; nInd
< 8 ; nInd
++ )
85 FillCharFromInt( guid
.Data4
[nInd
], &pBuf
[20 + 1 + 2*nInd
], 2 );
91 HRESULT
WriteLibraryToRegistry( const char* pLibrary
, DWORD nLen
)
93 HRESULT hRes
= E_FAIL
;
94 if ( pLibrary
&& nLen
)
99 for ( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
101 const char pSubKeyTemplate
[] = "Software\\Classes\\CLSID\\.....................................\\InprocHandler32";
102 char pSubKey
[SAL_N_ELEMENTS(pSubKeyTemplate
)];
103 strncpy(pSubKey
, pSubKeyTemplate
, SAL_N_ELEMENTS(pSubKeyTemplate
));
105 int nGuidLen
= GetStringFromClassID( *guidList
[nInd
], &pSubKey
[23], 38 );
107 BOOL bLocalSuccess
= FALSE
;
108 if ( nGuidLen
&& nGuidLen
== 38 )
110 if ( ERROR_SUCCESS
== RegOpenKey( HKEY_LOCAL_MACHINE
, pSubKey
, &hKey
) )
112 if ( ERROR_SUCCESS
== RegSetValueEx( hKey
, "", 0, REG_SZ
, (const BYTE
*)pLibrary
, nLen
) )
113 bLocalSuccess
= TRUE
;
123 if ( !bLocalSuccess
)
133 // InprocEmbedProvider_Impl declaration
139 class InprocEmbedProvider_Impl
: public IClassFactory
, public InprocCountedObject_Impl
143 InprocEmbedProvider_Impl( const GUID
& guid
);
144 virtual ~InprocEmbedProvider_Impl();
146 /* IUnknown methods */
147 STDMETHOD(QueryInterface
)(REFIID riid
, LPVOID FAR
* ppvObj
);
148 STDMETHOD_(ULONG
, AddRef
)();
149 STDMETHOD_(ULONG
, Release
)();
151 /* IClassFactory methods */
152 STDMETHOD(CreateInstance
)(IUnknown FAR
* punkOuter
, REFIID riid
, void FAR
* FAR
* ppv
);
153 STDMETHOD(LockServer
)(int fLock
);
160 }; // namespace inprocserv
168 extern "C" INPROC_DLLPUBLIC BOOL WINAPI
DllMain( HINSTANCE hInstance
, DWORD dwReason
, LPVOID
/*lpReserved*/ )
170 if (dwReason
== DLL_PROCESS_ATTACH
)
172 g_hInstance
= hInstance
;
174 else if (dwReason
== DLL_PROCESS_DETACH
)
182 extern "C" STDAPI INPROC_DLLPUBLIC
DllGetClassObject( REFCLSID rclsid
, REFIID riid
, LPVOID
* ppv
)
184 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
185 if ( *guidList
[nInd
] == rclsid
)
187 if ( !IsEqualIID( riid
, IID_IUnknown
) && !IsEqualIID( riid
, IID_IClassFactory
) )
188 return E_NOINTERFACE
;
190 *ppv
= new inprocserv::InprocEmbedProvider_Impl( rclsid
);
191 ((LPUNKNOWN
)*ppv
)->AddRef();
199 extern "C" STDAPI INPROC_DLLPUBLIC
DllCanUnloadNow()
201 if ( !g_nObj
&& !g_nLock
)
208 STDAPI INPROC_DLLPUBLIC
DllRegisterServer()
210 HMODULE aCurModule
= GetModuleHandleA( "inprocserv.dll" );
214 DWORD nLen
= GetModuleFileNameA( aCurModule
, aLibPath
, 1019 );
215 if ( nLen
&& nLen
< 1019 )
217 aLibPath
[nLen
++] = 0;
218 return WriteLibraryToRegistry( aLibPath
, nLen
);
226 STDAPI INPROC_DLLPUBLIC
DllUnregisterServer()
228 return WriteLibraryToRegistry( "ole32.dll", 10 );
232 // End of entry points
239 // InprocCountedObject_Impl implementation
243 InprocCountedObject_Impl::InprocCountedObject_Impl()
249 InprocCountedObject_Impl::~InprocCountedObject_Impl()
255 // InprocEmbedProvider_Impl implementation
259 InprocEmbedProvider_Impl::InprocEmbedProvider_Impl( const GUID
& guid
)
266 InprocEmbedProvider_Impl::~InprocEmbedProvider_Impl()
272 STDMETHODIMP
InprocEmbedProvider_Impl::QueryInterface( REFIID riid
, void FAR
* FAR
* ppv
)
274 if(IsEqualIID(riid
, IID_IUnknown
))
277 *ppv
= (IUnknown
*) this;
280 else if (IsEqualIID(riid
, IID_IClassFactory
))
283 *ppv
= (IClassFactory
*) this;
288 return E_NOINTERFACE
;
292 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::AddRef()
298 STDMETHODIMP_(ULONG
) InprocEmbedProvider_Impl::Release()
300 sal_Int32 nCount
= --m_refCount
;
307 STDMETHODIMP
InprocEmbedProvider_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
311 // TODO/LATER: should the aggregation be supported?
312 // if ( punkOuter != NULL && riid != IID_IUnknown )
313 // return E_NOINTERFACE;
314 if ( punkOuter
!= NULL
)
315 return CLASS_E_NOAGGREGATION
;
317 InprocEmbedDocument_Impl
* pEmbedDocument
= new InprocEmbedDocument_Impl( m_guid
);
318 pEmbedDocument
->AddRef();
319 HRESULT hr
= pEmbedDocument
->Init();
320 if ( SUCCEEDED( hr
) )
321 hr
= pEmbedDocument
->QueryInterface( riid
, ppv
);
322 pEmbedDocument
->Release();
324 if ( !SUCCEEDED( hr
) )
331 STDMETHODIMP
InprocEmbedProvider_Impl::LockServer( int fLock
)
341 }; // namespace inprocserv
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */