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