| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/native_library.h" | 5 #include "base/native_library.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); | 16 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 17 | 17 |
| 18 namespace { |
| 19 |
| 18 NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, | 20 NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
| 19 LoadLibraryFunction load_library_api, | 21 LoadLibraryFunction load_library_api, |
| 20 std::string* error) { | 22 NativeLibraryLoadError* error) { |
| 21 // LoadLibrary() opens the file off disk. | 23 // LoadLibrary() opens the file off disk. |
| 22 ThreadRestrictions::AssertIOAllowed(); | 24 ThreadRestrictions::AssertIOAllowed(); |
| 23 | 25 |
| 24 // Switch the current directory to the library directory as the library | 26 // Switch the current directory to the library directory as the library |
| 25 // may have dependencies on DLLs in this directory. | 27 // may have dependencies on DLLs in this directory. |
| 26 bool restore_directory = false; | 28 bool restore_directory = false; |
| 27 FilePath current_directory; | 29 FilePath current_directory; |
| 28 if (GetCurrentDirectory(¤t_directory)) { | 30 if (GetCurrentDirectory(¤t_directory)) { |
| 29 FilePath plugin_path = library_path.DirName(); | 31 FilePath plugin_path = library_path.DirName(); |
| 30 if (!plugin_path.empty()) { | 32 if (!plugin_path.empty()) { |
| 31 SetCurrentDirectory(plugin_path); | 33 SetCurrentDirectory(plugin_path); |
| 32 restore_directory = true; | 34 restore_directory = true; |
| 33 } | 35 } |
| 34 } | 36 } |
| 35 | 37 |
| 36 HMODULE module = (*load_library_api)(library_path.value().c_str()); | 38 HMODULE module = (*load_library_api)(library_path.value().c_str()); |
| 37 if (!module && error) { | 39 if (!module && error) { |
| 38 // GetLastError() needs to be called immediately after |load_library_api|. | 40 // GetLastError() needs to be called immediately after |load_library_api|. |
| 39 DWORD last_error = GetLastError(); | 41 *error = GetLastError(); |
| 40 *error = StringPrintf("%u", last_error); | |
| 41 } | 42 } |
| 42 | 43 |
| 43 if (restore_directory) | 44 if (restore_directory) |
| 44 SetCurrentDirectory(current_directory); | 45 SetCurrentDirectory(current_directory); |
| 45 | 46 |
| 46 return module; | 47 return module; |
| 47 } | 48 } |
| 48 | 49 |
| 50 } // namespace |
| 51 |
| 49 // static | 52 // static |
| 50 NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 53 NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
| 51 std::string* error) { | 54 NativeLibraryLoadError* error) { |
| 52 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); | 55 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); |
| 53 } | 56 } |
| 54 | 57 |
| 55 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { | 58 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
| 56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); | 59 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 57 | 60 |
| 58 LoadLibraryFunction load_library; | 61 LoadLibraryFunction load_library; |
| 59 load_library = reinterpret_cast<LoadLibraryFunction>( | 62 load_library = reinterpret_cast<LoadLibraryFunction>( |
| 60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); | 63 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
| 61 | 64 |
| 62 return LoadNativeLibraryHelper(library_path, load_library, NULL); | 65 return LoadNativeLibraryHelper(library_path, load_library, NULL, NULL); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // static | 68 // static |
| 66 void UnloadNativeLibrary(NativeLibrary library) { | 69 void UnloadNativeLibrary(NativeLibrary library) { |
| 67 FreeLibrary(library); | 70 FreeLibrary(library); |
| 68 } | 71 } |
| 69 | 72 |
| 70 // static | 73 // static |
| 71 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 74 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 72 const char* name) { | 75 const char* name) { |
| 73 return GetProcAddress(library, name); | 76 return GetProcAddress(library, name); |
| 74 } | 77 } |
| 75 | 78 |
| 76 // static | 79 // static |
| 77 string16 GetNativeLibraryName(const string16& name) { | 80 string16 GetNativeLibraryName(const string16& name) { |
| 78 return name + ASCIIToUTF16(".dll"); | 81 return name + ASCIIToUTF16(".dll"); |
| 79 } | 82 } |
| 80 | 83 |
| 81 } // namespace base | 84 } // namespace base |
| OLD | NEW |