[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/native_library.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 11 | #include "base/strings/stringprintf.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 17 | typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 18 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 21 | NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 22 | LoadLibraryFunction load_library_api, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 23 | NativeLibraryLoadError* error) { |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 24 | // LoadLibrary() opens the file off disk. |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 25 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 26 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 27 | // Switch the current directory to the library directory as the library |
| 28 | // may have dependencies on DLLs in this directory. |
| 29 | bool restore_directory = false; |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 30 | FilePath current_directory; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 31 | if (GetCurrentDirectory(¤t_directory)) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 32 | FilePath plugin_path = library_path.DirName(); |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 33 | if (!plugin_path.empty()) { |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 34 | SetCurrentDirectory(plugin_path); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 35 | restore_directory = true; |
| 36 | } |
| 37 | } |
| 38 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 39 | HMODULE module = (*load_library_api)(library_path.value().c_str()); |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 40 | if (!module && error) { |
| 41 | // GetLastError() needs to be called immediately after |load_library_api|. |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 42 | error->code = GetLastError(); |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 45 | if (restore_directory) |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 46 | SetCurrentDirectory(current_directory); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 47 | |
| 48 | return module; |
| 49 | } |
| 50 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 51 | } // namespace |
| 52 | |
| 53 | std::string NativeLibraryLoadError::ToString() const { |
| 54 | return StringPrintf("%u", code); |
| 55 | } |
| 56 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 57 | // static |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 58 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 59 | const NativeLibraryOptions& options, |
| 60 | NativeLibraryLoadError* error) { |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 61 | return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
| 65 | typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 66 | |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 67 | LoadLibraryFunction load_library = reinterpret_cast<LoadLibraryFunction>( |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 68 | GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
| 69 | |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 70 | return LoadNativeLibraryHelper(library_path, load_library, NULL); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // static |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 74 | void UnloadNativeLibrary(NativeLibrary library) { |
| 75 | FreeLibrary(library); |
| 76 | } |
| 77 | |
| 78 | // static |
| 79 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 80 | StringPiece name) { |
| 81 | return GetProcAddress(library, name.data()); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 84 | // static |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 85 | std::string GetNativeLibraryName(StringPiece name) { |
| 86 | DCHECK(IsStringASCII(name)); |
| 87 | return name.as_string() + ".dll"; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 90 | } // namespace base |