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 .
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>
29 #include <rtl/ustring.hxx>
30 #include <sal/log.hxx>
31 #include <o3tl/char16_t2wchar_t.hxx>
32 #include <systools/win32/extended_max_path.hxx>
36 under WIN32, we use the void* oslModule
37 as a WIN32 HANDLE (which is also a 32-bit value)
40 oslModule SAL_CALL
osl_loadModule(rtl_uString
*strModuleName
, sal_Int32
/*nRtldMode*/ )
42 #if OSL_DEBUG_LEVEL < 2
43 UINT errorMode
= SetErrorMode(SEM_NOOPENFILEERRORBOX
| SEM_FAILCRITICALERRORS
);
45 SAL_INFO( "sal.osl", "osl_loadModule: " << OUString(strModuleName
) );
46 OSL_ASSERT(strModuleName
);
49 oslFileError nError
= osl_getSystemPathFromFileURL(strModuleName
, &Module
.pData
);
51 if ( osl_File_E_None
!= nError
)
52 Module
= OUString::unacquired(&strModuleName
);
54 HMODULE h
= LoadLibraryW(o3tl::toW(Module
.getStr()));
57 h
= LoadLibraryExW(o3tl::toW(Module
.getStr()), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH
);
59 // In case of long path names (\\?\c:\...) try to shorten the filename.
60 // LoadLibrary cannot handle file names which exceed 260 letters.
61 // In case the path is too long, the function will fail. However, the error
62 // code can be different. For example, it returned ERROR_FILENAME_EXCED_RANGE
63 // on Windows XP and ERROR_INSUFFICIENT_BUFFER on Windows 7 (64bit)
64 if (h
== nullptr && Module
.getLength() > 260)
66 std::vector
<WCHAR
> vec(Module
.getLength() + 1);
67 DWORD len
= GetShortPathNameW(o3tl::toW(Module
.getStr()), vec
.data(), vec
.size());
70 h
= LoadLibraryW(vec
.data());
73 h
= LoadLibraryExW(vec
.data(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH
);
77 #if OSL_DEBUG_LEVEL < 2
78 SetErrorMode(errorMode
);
81 return static_cast<oslModule
>(h
);
84 oslModule SAL_CALL
osl_loadModuleAscii(const char *pModuleName
, sal_Int32
)
86 UINT errorMode
= SetErrorMode(SEM_NOOPENFILEERRORBOX
| SEM_FAILCRITICALERRORS
);
88 SAL_INFO( "sal.osl", "osl_loadModule: " << pModuleName
);
89 OSL_ASSERT(pModuleName
);
91 HMODULE h
= LoadLibraryA(pModuleName
);
93 h
= LoadLibraryExA(pModuleName
, nullptr,
94 LOAD_WITH_ALTERED_SEARCH_PATH
);
96 SetErrorMode(errorMode
);
98 return static_cast<oslModule
>(h
);
101 oslModule
osl_loadModuleRelativeAscii(
102 oslGenericFunction baseModule
, char const * relativePath
, sal_Int32 mode
)
104 return osl_loadModuleRelative(baseModule
, OUString::createFromAscii(relativePath
).pData
, mode
);
108 osl_getModuleHandle(rtl_uString
*pModuleName
, oslModule
*pResult
)
110 LPCWSTR pName
= pModuleName
? o3tl::toW(pModuleName
->buffer
) : nullptr;
111 HMODULE h
= GetModuleHandleW(pName
);
114 *pResult
= static_cast<oslModule
>(h
);
121 void SAL_CALL
osl_unloadModule(oslModule Module
)
123 FreeLibrary(static_cast<HMODULE
>(Module
));
126 void* SAL_CALL
osl_getSymbol(oslModule Module
, rtl_uString
*strSymbolName
)
128 /* casting from a function pointer to a data pointer is invalid
129 be in this case unavoidable because the API has to stay
130 compatible. We need to keep this function which returns a
131 void* by definition */
132 return reinterpret_cast<void*>(osl_getFunctionSymbol(Module
, strSymbolName
));
135 oslGenericFunction SAL_CALL
osl_getFunctionSymbol( oslModule Module
, rtl_uString
*strSymbolName
)
138 assert(strSymbolName
);
140 auto symbolName(OUStringToOString(OUString::unacquired(&strSymbolName
), RTL_TEXTENCODING_UTF8
));
142 return osl_getAsciiFunctionSymbol(Module
, symbolName
.getStr());
145 oslGenericFunction SAL_CALL
146 osl_getAsciiFunctionSymbol( oslModule Module
, const char *pSymbol
)
148 oslGenericFunction fncAddr
= nullptr;
151 fncAddr
=reinterpret_cast<oslGenericFunction
>(GetProcAddress(static_cast<HMODULE
>(Module
), pSymbol
));
156 sal_Bool SAL_CALL
osl_getModuleURLFromAddress( void *pv
, rtl_uString
**pustrURL
)
159 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
160 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
161 static_cast<LPCWSTR
>(pv
), &hModule
);
165 osl::LongPathBuffer
<sal_Unicode
> aBuffer(EXTENDED_MAX_PATH
);
167 DWORD nch
= GetModuleFileNameW(hModule
, o3tl::toW(aBuffer
), aBuffer
.getBufSizeInSymbols());
169 OUString
ustrSysPath(aBuffer
, nch
);
170 return osl_getFileURLFromSystemPath(ustrSysPath
.pData
, pustrURL
) == osl_File_E_None
;
173 sal_Bool SAL_CALL
osl_getModuleURLFromFunctionAddress( oslGenericFunction addr
, rtl_uString
** ppLibraryUrl
)
175 /* casting a function pointer to a data pointer (void*) is
176 not allowed according to the C/C++ standards. In this case
177 it is unavoidable because we have to stay compatible we
178 cannot remove any function. */
179 return osl_getModuleURLFromAddress(reinterpret_cast<void*>(addr
), ppLibraryUrl
);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */