OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/thread_restrictions.h" | 10 #include "base/thread_restrictions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 // static | 15 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
16 NativeLibrary LoadNativeLibrary(const FilePath& library_path) { | 16 |
| 17 NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
| 18 LoadLibraryFunction load_library_api) { |
17 // LoadLibrary() opens the file off disk. | 19 // LoadLibrary() opens the file off disk. |
18 base::ThreadRestrictions::AssertIOAllowed(); | 20 base::ThreadRestrictions::AssertIOAllowed(); |
19 | 21 |
20 // Switch the current directory to the library directory as the library | 22 // Switch the current directory to the library directory as the library |
21 // may have dependencies on DLLs in this directory. | 23 // may have dependencies on DLLs in this directory. |
22 bool restore_directory = false; | 24 bool restore_directory = false; |
23 FilePath current_directory; | 25 FilePath current_directory; |
24 if (file_util::GetCurrentDirectory(¤t_directory)) { | 26 if (file_util::GetCurrentDirectory(¤t_directory)) { |
25 FilePath plugin_path = library_path.DirName(); | 27 FilePath plugin_path = library_path.DirName(); |
26 if (!plugin_path.empty()) { | 28 if (!plugin_path.empty()) { |
27 file_util::SetCurrentDirectory(plugin_path); | 29 file_util::SetCurrentDirectory(plugin_path); |
28 restore_directory = true; | 30 restore_directory = true; |
29 } | 31 } |
30 } | 32 } |
31 | 33 |
32 HMODULE module = LoadLibrary(library_path.value().c_str()); | 34 HMODULE module = (*load_library_api)(library_path.value().c_str()); |
33 if (restore_directory) | 35 if (restore_directory) |
34 file_util::SetCurrentDirectory(current_directory); | 36 file_util::SetCurrentDirectory(current_directory); |
35 | 37 |
36 return module; | 38 return module; |
37 } | 39 } |
38 | 40 |
39 // static | 41 // static |
| 42 NativeLibrary LoadNativeLibrary(const FilePath& library_path) { |
| 43 return LoadNativeLibraryHelper(library_path, LoadLibraryW); |
| 44 } |
| 45 |
| 46 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
| 47 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 48 |
| 49 LoadLibraryFunction load_library; |
| 50 load_library = reinterpret_cast<LoadLibraryFunction>( |
| 51 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
| 52 |
| 53 return LoadNativeLibraryHelper(library_path, load_library); |
| 54 } |
| 55 |
| 56 // static |
40 void UnloadNativeLibrary(NativeLibrary library) { | 57 void UnloadNativeLibrary(NativeLibrary library) { |
41 FreeLibrary(library); | 58 FreeLibrary(library); |
42 } | 59 } |
43 | 60 |
44 // static | 61 // static |
45 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 62 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
46 const char* name) { | 63 const char* name) { |
47 return GetProcAddress(library, name); | 64 return GetProcAddress(library, name); |
48 } | 65 } |
49 | 66 |
50 // static | 67 // static |
51 string16 GetNativeLibraryName(const string16& name) { | 68 string16 GetNativeLibraryName(const string16& name) { |
52 return name + ASCIIToUTF16(".dll"); | 69 return name + ASCIIToUTF16(".dll"); |
53 } | 70 } |
54 | 71 |
55 } // namespace base | 72 } // namespace base |
OLD | NEW |