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