Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / w32 / module.cxx
blob8379e14b2fd519f53f5d70ce18aaf9d31f188348
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 #include "system.h"
22 #include "file_url.hxx"
23 #include "path_helper.hxx"
25 #include <osl/module.h>
26 #include <osl/diagnose.h>
27 #include <osl/thread.h>
28 #include <osl/file.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/log.hxx>
31 #include <o3tl/char16_t2wchar_t.hxx>
32 #include <vector>
35 under WIN32, we use the void* oslModule
36 as a WIN32 HANDLE (which is also a 32-bit value)
39 oslModule SAL_CALL osl_loadModule(rtl_uString *strModuleName, sal_Int32 /*nRtldMode*/ )
41 HMODULE h;
42 #if OSL_DEBUG_LEVEL < 2
43 UINT errorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
44 #endif
45 rtl_uString* Module = nullptr;
46 oslModule ret = nullptr;
47 oslFileError nError;
49 SAL_INFO( "sal.osl", "osl_loadModule: " << OUString(strModuleName) );
50 OSL_ASSERT(strModuleName);
52 nError = osl_getSystemPathFromFileURL(strModuleName, &Module);
54 if ( osl_File_E_None != nError )
55 rtl_uString_assign(&Module, strModuleName);
57 h = LoadLibraryW(o3tl::toW(Module->buffer));
59 if (h == nullptr)
60 h = LoadLibraryExW(o3tl::toW(Module->buffer), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
62 // In case of long path names (\\?\c:\...) try to shorten the filename.
63 // LoadLibrary cannot handle file names which exceed 260 letters.
64 // In case the path is too long, the function will fail. However, the error
65 // code can be different. For example, it returned ERROR_FILENAME_EXCED_RANGE
66 // on Windows XP and ERROR_INSUFFICIENT_BUFFER on Windows 7 (64bit)
67 if (h == nullptr && Module->length > 260)
69 std::vector<WCHAR> vec(Module->length + 1);
70 DWORD len = GetShortPathNameW(o3tl::toW(Module->buffer), vec.data(), Module->length + 1);
71 if (len )
73 h = LoadLibraryW(vec.data());
75 if (h == nullptr)
76 h = LoadLibraryExW(vec.data(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
80 ret = static_cast<oslModule>(h);
81 rtl_uString_release(Module);
82 #if OSL_DEBUG_LEVEL < 2
83 SetErrorMode(errorMode);
84 #endif
86 return ret;
89 oslModule SAL_CALL osl_loadModuleAscii(const char *pModuleName, sal_Int32 )
91 HMODULE h;
92 UINT errorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
93 oslModule ret = nullptr;
95 SAL_INFO( "sal.osl", "osl_loadModule: " << pModuleName );
96 OSL_ASSERT(pModuleName);
98 h = LoadLibraryA(pModuleName);
99 if (h == nullptr)
100 h = LoadLibraryExA(pModuleName, nullptr,
101 LOAD_WITH_ALTERED_SEARCH_PATH);
103 ret = static_cast<oslModule>(h);
104 SetErrorMode(errorMode);
106 return ret;
109 oslModule osl_loadModuleRelativeAscii(
110 oslGenericFunction baseModule, char const * relativePath, sal_Int32 mode)
112 return osl_loadModuleRelative(baseModule, OUString::createFromAscii(relativePath).pData, mode);
115 sal_Bool SAL_CALL
116 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
118 LPCWSTR pName = pModuleName ? o3tl::toW(pModuleName->buffer) : nullptr;
119 HMODULE h = GetModuleHandleW(pName);
120 if( h )
122 *pResult = static_cast<oslModule>(h);
123 return true;
126 return false;
129 void SAL_CALL osl_unloadModule(oslModule Module)
131 FreeLibrary(static_cast<HMODULE>(Module));
134 void* SAL_CALL osl_getSymbol(oslModule Module, rtl_uString *strSymbolName)
136 /* casting from a function pointer to a data pointer is invalid
137 be in this case unavoidable because the API has to stay
138 compatible. We need to keep this function which returns a
139 void* by definition */
140 return reinterpret_cast<void*>(osl_getFunctionSymbol(Module, strSymbolName));
143 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
145 rtl_String *symbolName = nullptr;
146 oslGenericFunction address;
148 OSL_ASSERT(Module);
149 OSL_ASSERT(strSymbolName);
151 rtl_uString2String(
152 &symbolName,
153 strSymbolName->buffer,
154 strSymbolName->length,
155 RTL_TEXTENCODING_UTF8,
156 OUSTRING_TO_OSTRING_CVTFLAGS
159 address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
160 rtl_string_release(symbolName);
162 return address;
165 oslGenericFunction SAL_CALL
166 osl_getAsciiFunctionSymbol( oslModule Module, const char *pSymbol )
168 oslGenericFunction fncAddr = nullptr;
170 if( pSymbol )
171 fncAddr=reinterpret_cast<oslGenericFunction>(GetProcAddress(static_cast<HMODULE>(Module), pSymbol));
173 return fncAddr;
176 sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString **pustrURL )
178 HMODULE hModule{};
179 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
180 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
181 static_cast<LPCWSTR>(pv), &hModule);
182 if (!hModule)
183 return false;
185 ::osl::LongPathBuffer<sal_Unicode> aBuffer(MAX_LONG_PATH);
187 DWORD nch = GetModuleFileNameW(hModule, o3tl::toW(aBuffer), aBuffer.getBufSizeInSymbols());
189 OUString ustrSysPath(aBuffer, nch);
190 return osl_getFileURLFromSystemPath(ustrSysPath.pData, pustrURL) == osl_File_E_None;
193 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
195 /* casting a function pointer to a data pointer (void*) is
196 not allowed according to the C/C++ standards. In this case
197 it is unavoidable because we have to stay compatible we
198 cannot remove any function. */
199 return osl_getModuleURLFromAddress(reinterpret_cast<void*>(addr), ppLibraryUrl);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */