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 // Use UNICODE Windows and C API.
25 #pragma warning(push, 1)
28 #include "uno/environment.hxx"
35 #include "native_share.h"
37 #include "rtl/bootstrap.hxx"
38 #include "com/sun/star/uno/XComponentContext.hpp"
39 #include "cppuhelper/bootstrap.hxx"
43 using namespace ::rtl
;
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::uno
;
48 WCHAR
* resolveLink(WCHAR
* path
);
51 #define INSTALL_PATH L"Software\\LibreOffice\\UNO\\InstallPath"
52 #define URE_LINK L"\\ure-link"
53 #define URE_BIN L"\\bin"
54 #define UNO_PATH L"UNO_PATH"
60 * Gets the installation path from the Windows Registry for the specified
63 * @param hroot open handle to predefined root registry key
64 * @param subKeyName name of the subkey to open
66 * @return the installation path or NULL, if no installation was found or
67 * if an error occurred
69 WCHAR
* getPathFromRegistryKey( HKEY hroot
, LPCWSTR subKeyName
)
76 /* open the specified registry key */
77 if ( RegOpenKeyEx( hroot
, subKeyName
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
82 /* find the type and size of the default value */
83 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, NULL
, &size
) != ERROR_SUCCESS
)
89 /* get memory to hold the default value */
90 data
= new WCHAR
[size
];
92 /* read the default value */
93 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, (LPBYTE
) data
, &size
) != ERROR_SUCCESS
)
99 /* release registry key handle */
105 /* If the path does not end with '\' the las segment will be removed.
109 in/out parameter. The string is not reallocated. Simply a '\0'
110 will be inserted to shorten the string.
112 void oneDirUp(LPTSTR io_path
)
114 WCHAR
* pEnd
= io_path
+ lstrlen(io_path
) - 1;
115 while (pEnd
> io_path
//prevent crashing if provided string does not contain a backslash
122 /* Returns the path to the program folder of the brand layer,
123 for example c:/LibreOffice 3/program
124 This path is either obtained from the environment variable UNO_PATH
126 "Software\\LibreOffice\\UNO\\InstallPath"
127 either in HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE
128 The return value must be freed with delete[]
130 WCHAR
* getInstallPath()
132 WCHAR
* szInstallPath
= NULL
;
134 DWORD cChars
= GetEnvironmentVariable(UNO_PATH
, NULL
, 0);
137 szInstallPath
= new WCHAR
[cChars
];
138 cChars
= GetEnvironmentVariable(UNO_PATH
, szInstallPath
, cChars
);
139 //If PATH is not set then it is no error
142 delete[] szInstallPath
;
149 szInstallPath
= getPathFromRegistryKey( HKEY_CURRENT_USER
, INSTALL_PATH
);
150 if ( szInstallPath
== NULL
)
152 /* read the key's default value from HKEY_LOCAL_MACHINE */
153 szInstallPath
= getPathFromRegistryKey( HKEY_LOCAL_MACHINE
, INSTALL_PATH
);
156 return szInstallPath
;
159 /* Returns the path to the URE/bin path, where cppuhelper lib resides.
160 The returned string must be freed with delete[]
164 WCHAR
* szLinkPath
= NULL
;
165 WCHAR
* szUrePath
= NULL
;
166 WCHAR
* szUreBin
= NULL
; //the return value
168 WCHAR
* szInstallPath
= getInstallPath();
171 oneDirUp(szInstallPath
);
173 //build the path to the ure-link file
174 szUrePath
= new WCHAR
[MAX_PATH
];
175 szUrePath
[0] = L
'\0';
176 lstrcat(szUrePath
, szInstallPath
);
177 lstrcat(szUrePath
, URE_LINK
);
179 //get the path to the actual Ure folder
180 if (cli_ure::resolveLink(szUrePath
))
182 //build the path to the URE/bin directory
183 szUreBin
= new WCHAR
[lstrlen(szUrePath
) + lstrlen(URE_BIN
) + 1];
185 lstrcat(szUreBin
, szUrePath
);
186 lstrcat(szUreBin
, URE_BIN
);
189 #if OSL_DEBUG_LEVEL >=2
192 fwprintf(stdout
,L
"[cli_cppuhelper]: Path to URE libraries:\n %s \n", szUreBin
);
196 fwprintf(stdout
,L
"[cli_cppuhelper]: Failed to determine location of URE.\n");
199 delete[] szInstallPath
;
206 /*We extend the path to contain the Ure/bin folder,
207 so that components can use osl_loadModule with arguments, such as
208 "reg3.dll". That is, the arguments are only the library names.
210 void extendPath(LPCWSTR szUreBinPath
)
215 WCHAR
* sEnvPath
= NULL
;
216 DWORD cChars
= GetEnvironmentVariable(L
"PATH", sEnvPath
, 0);
219 sEnvPath
= new WCHAR
[cChars
];
220 cChars
= GetEnvironmentVariable(L
"PATH", sEnvPath
, cChars
);
221 //If PATH is not set then it is no error
222 if (cChars
== 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND
)
228 //prepare the new PATH. Add the Ure/bin directory at the front.
229 //note also adding ';'
230 WCHAR
* sNewPath
= new WCHAR
[lstrlen(sEnvPath
) + lstrlen(szUreBinPath
) + 2];
232 lstrcat(sNewPath
, szUreBinPath
);
233 if (lstrlen(sEnvPath
))
235 lstrcat(sNewPath
, L
";");
236 lstrcat(sNewPath
, sEnvPath
);
238 BOOL bSet
= SetEnvironmentVariable(L
"PATH", sNewPath
);
245 HMODULE
loadFromPath(LPCWSTR sLibName
)
247 if (sLibName
== NULL
)
250 WCHAR
* szUreBinPath
= getUnoPath();
254 extendPath(szUreBinPath
);
256 WCHAR
* szFullPath
= new WCHAR
[lstrlen(sLibName
) + lstrlen(szUreBinPath
) + 2];
257 szFullPath
[0] = L
'\0';
258 lstrcat(szFullPath
, szUreBinPath
);
259 lstrcat(szFullPath
, L
"\\");
260 lstrcat(szFullPath
, sLibName
);
261 HMODULE handle
= LoadLibraryEx(szFullPath
, NULL
,
262 LOAD_WITH_ALTERED_SEARCH_PATH
);
265 delete[] szUreBinPath
;
269 /*Hook for delayed loading of libraries which this library is linked with.
270 This is a failure hook. That is, it is only called when the loading of
271 a library failed. It will be called when loading of cppuhelper failed.
272 Because we extend the PATH to the URE/bin folder while this function is
273 executed (see extendPath), all other libraries are found.
275 extern "C" FARPROC WINAPI
delayLoadHook(
280 if (dliNotify
== dliFailLoadLib
)
282 LPWSTR szLibName
= NULL
;
283 //Convert the ansi file name to wchar_t*
284 int size
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, pdli
->szDll
, -1, NULL
, 0);
287 szLibName
= new WCHAR
[size
];
288 if (! MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, pdli
->szDll
, -1, szLibName
, size
))
293 HANDLE h
= loadFromPath(szLibName
);
302 PfnDliHook __pfnDliFailureHook2
= delayLoadHook
;
309 /** Bootstrapping native UNO.
311 Bootstrapping requires the existence of many libraries which are contained
312 in an URE installation. To find and load these libraries the Windows
313 registry keys HKEY_CURRENT_USER\Software\LibreOffice\Layer\URE\1
314 and HKEY_LOCAL_MACHINE\Software\LibreOffice\Layer\URE\1 are examined.
315 These contain a named value UREINSTALLLOCATION which holds a path to the URE
318 public ref
class Bootstrap sealed
320 inline Bootstrap() {}
324 /** Bootstraps the initial component context from a native UNO installation.
326 @see cppuhelper/bootstrap.hxx:defaultBootstrap_InitialComponentContext()
328 static ::unoidl::com::sun::star::uno::XComponentContext
^
329 defaultBootstrap_InitialComponentContext();
331 /** Bootstraps the initial component context from a native UNO installation.
334 a file URL of an ini file, e.g. uno.ini/unorc. (The ini file must
335 reside next to the cppuhelper library)
336 @param bootstrap_parameters
337 bootstrap parameters (maybe null)
339 @see cppuhelper/bootstrap.hxx:defaultBootstrap_InitialComponentContext()
341 static ::unoidl::com::sun::star::uno::XComponentContext
^
342 defaultBootstrap_InitialComponentContext(
343 ::System::String
^ ini_file
,
344 ::System::Collections::IDictionaryEnumerator
^
345 bootstrap_parameters
);
347 /** Bootstraps the initial component context from a native UNO installation.
349 @see cppuhelper/bootstrap.hxx:bootstrap()
351 static ::unoidl::com::sun::star::uno::XComponentContext
^
355 //______________________________________________________________________________
356 ::unoidl::com::sun::star::uno::XComponentContext
^
357 Bootstrap::defaultBootstrap_InitialComponentContext(
358 ::System::String
^ ini_file
,
359 ::System::Collections::IDictionaryEnumerator
^ bootstrap_parameters
)
361 if (nullptr != bootstrap_parameters
)
363 bootstrap_parameters
->Reset();
364 while (bootstrap_parameters
->MoveNext())
367 String_to_ustring( safe_cast
< ::System::String
^ >(
368 bootstrap_parameters
->Key
) ) );
370 String_to_ustring( safe_cast
< ::System::String
^ >(
371 bootstrap_parameters
->Value
) ) );
373 ::rtl::Bootstrap::set( key
, value
);
377 // bootstrap native uno
378 Reference
< XComponentContext
> xContext
;
379 if (nullptr == ini_file
)
381 xContext
= ::cppu::defaultBootstrap_InitialComponentContext();
385 xContext
= ::cppu::defaultBootstrap_InitialComponentContext(
386 String_to_ustring( safe_cast
< ::System::String
^ >( ini_file
) ) );
389 return safe_cast
< ::unoidl::com::sun::star::uno::XComponentContext
^ >(
390 to_cli( xContext
) );
393 //______________________________________________________________________________
394 ::unoidl::com::sun::star::uno::XComponentContext
^
395 Bootstrap::defaultBootstrap_InitialComponentContext()
397 return defaultBootstrap_InitialComponentContext( nullptr, nullptr );
400 ::unoidl::com::sun::star::uno::XComponentContext
^ Bootstrap::bootstrap()
402 Reference
<XComponentContext
> xContext
= ::cppu::bootstrap();
403 return safe_cast
< ::unoidl::com::sun::star::uno::XComponentContext
^ >(
404 to_cli( xContext
) );
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */