GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / cli_ure / source / native / native_bootstrap.cxx
blob0a22d7bb5e63c13214359f4d9d237117ed4c506b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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.
21 #define _UNICODE
22 #define UNICODE
24 #ifdef _MSC_VER
25 #pragma warning(push, 1)
26 #endif
27 #include <windows.h>
28 #include "uno/environment.hxx"
29 #ifdef _MSC_VER
30 #pragma warning(pop)
31 #endif
33 #include <tchar.h>
35 #include "native_share.h"
37 #include "rtl/bootstrap.hxx"
38 #include "com/sun/star/uno/XComponentContext.hpp"
39 #include "cppuhelper/bootstrap.hxx"
40 #include <delayimp.h>
41 #include <stdio.h>
43 using namespace ::rtl;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
47 namespace cli_ure {
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"
56 namespace
60 * Gets the installation path from the Windows Registry for the specified
61 * registry key.
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 )
71 HKEY hkey;
72 DWORD type;
73 TCHAR* data = NULL;
74 DWORD size;
76 /* open the specified registry key */
77 if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
79 return NULL;
82 /* find the type and size of the default value */
83 if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
85 RegCloseKey( hkey );
86 return NULL;
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 )
95 RegCloseKey( hkey );
96 return NULL;
99 /* release registry key handle */
100 RegCloseKey( hkey );
102 return data;
105 /* If the path does not end with '\' the las segment will be removed.
106 path: C:\a\b
107 -> C:\a
108 @param io_path
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
116 && *pEnd != L'\\')
117 pEnd --;
118 *pEnd = L'\0';
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
125 or the registry item
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);
135 if (cChars > 0)
137 szInstallPath = new WCHAR[cChars];
138 cChars = GetEnvironmentVariable(UNO_PATH, szInstallPath, cChars);
139 //If PATH is not set then it is no error
140 if (cChars == 0)
142 delete[] szInstallPath;
143 return NULL;
147 if (! 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[]
162 WCHAR* getUnoPath()
164 WCHAR * szLinkPath = NULL;
165 WCHAR * szUrePath = NULL;
166 WCHAR * szUreBin = NULL; //the return value
168 WCHAR * szInstallPath = getInstallPath();
169 if (szInstallPath)
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];
184 szUreBin[0] = L'\0';
185 lstrcat(szUreBin, szUrePath);
186 lstrcat(szUreBin, URE_BIN);
189 #if OSL_DEBUG_LEVEL >=2
190 if (szUreBin)
192 fwprintf(stdout,L"[cli_cppuhelper]: Path to URE libraries:\n %s \n", szUreBin);
194 else
196 fwprintf(stdout,L"[cli_cppuhelper]: Failed to determine location of URE.\n");
198 #endif
199 delete[] szInstallPath;
200 delete[] szLinkPath;
201 delete[] szUrePath;
202 return szUreBin;
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)
212 if (!szUreBinPath)
213 return;
215 WCHAR * sEnvPath = NULL;
216 DWORD cChars = GetEnvironmentVariable(L"PATH", sEnvPath, 0);
217 if (cChars > 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)
224 delete[] sEnvPath;
225 return;
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];
231 sNewPath[0] = L'\0';
232 lstrcat(sNewPath, szUreBinPath);
233 if (lstrlen(sEnvPath))
235 lstrcat(sNewPath, L";");
236 lstrcat(sNewPath, sEnvPath);
238 BOOL bSet = SetEnvironmentVariable(L"PATH", sNewPath);
240 delete[] sEnvPath;
241 delete[] sNewPath;
245 HMODULE loadFromPath(LPCWSTR sLibName)
247 if (sLibName == NULL)
248 return NULL;
250 WCHAR * szUreBinPath = getUnoPath();
251 if (!szUreBinPath)
252 return NULL;
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);
264 delete[] szFullPath;
265 delete[] szUreBinPath;
266 return handle;
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(
276 unsigned dliNotify,
277 PDelayLoadInfo pdli
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);
285 if (size > 0)
287 szLibName = new WCHAR[size];
288 if (! MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pdli->szDll, -1, szLibName, size))
290 return 0;
293 HANDLE h = loadFromPath(szLibName);
294 delete[] szLibName;
295 return (FARPROC) h;
297 return 0;
301 ExternC
302 PfnDliHook __pfnDliFailureHook2 = delayLoadHook;
304 namespace uno
306 namespace util
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
316 installation folder.
318 public ref class Bootstrap sealed
320 inline Bootstrap() {}
322 public:
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.
333 @param ini_file
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 ^
352 bootstrap();
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())
366 OUString key(
367 String_to_ustring( safe_cast< ::System::String ^ >(
368 bootstrap_parameters->Key ) ) );
369 OUString value(
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();
383 else
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: */