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