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->code = 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 |
| 52 std::string NativeLibraryLoadError::ToString() const { |
| 53 return StringPrintf("%u", code); |
| 54 } |
| 55 |
49 // static | 56 // static |
50 NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 57 NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
51 std::string* error) { | 58 NativeLibraryLoadError* error) { |
52 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); | 59 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); |
53 } | 60 } |
54 | 61 |
55 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { | 62 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); | 63 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
57 | 64 |
58 LoadLibraryFunction load_library; | 65 LoadLibraryFunction load_library; |
59 load_library = reinterpret_cast<LoadLibraryFunction>( | 66 load_library = reinterpret_cast<LoadLibraryFunction>( |
60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); | 67 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
61 | 68 |
(...skipping 10 matching lines...) Expand all Loading... |
72 const char* name) { | 79 const char* name) { |
73 return GetProcAddress(library, name); | 80 return GetProcAddress(library, name); |
74 } | 81 } |
75 | 82 |
76 // static | 83 // static |
77 string16 GetNativeLibraryName(const string16& name) { | 84 string16 GetNativeLibraryName(const string16& name) { |
78 return name + ASCIIToUTF16(".dll"); | 85 return name + ASCIIToUTF16(".dll"); |
79 } | 86 } |
80 | 87 |
81 } // namespace base | 88 } // namespace base |
OLD | NEW |