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 NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, | 18 NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
ddorwin
2014/03/21 17:24:59
Not related to this CL, but this should be static/
xhwang
2014/03/21 19:00:54
Done.
| |
19 LoadLibraryFunction load_library_api, | 19 LoadLibraryFunction load_library_api, |
20 std::string* error) { | 20 std::string* error_message, |
21 uint32* error_code) { | |
21 // LoadLibrary() opens the file off disk. | 22 // LoadLibrary() opens the file off disk. |
22 ThreadRestrictions::AssertIOAllowed(); | 23 ThreadRestrictions::AssertIOAllowed(); |
23 | 24 |
24 // Switch the current directory to the library directory as the library | 25 // Switch the current directory to the library directory as the library |
25 // may have dependencies on DLLs in this directory. | 26 // may have dependencies on DLLs in this directory. |
26 bool restore_directory = false; | 27 bool restore_directory = false; |
27 FilePath current_directory; | 28 FilePath current_directory; |
28 if (GetCurrentDirectory(¤t_directory)) { | 29 if (GetCurrentDirectory(¤t_directory)) { |
29 FilePath plugin_path = library_path.DirName(); | 30 FilePath plugin_path = library_path.DirName(); |
30 if (!plugin_path.empty()) { | 31 if (!plugin_path.empty()) { |
31 SetCurrentDirectory(plugin_path); | 32 SetCurrentDirectory(plugin_path); |
32 restore_directory = true; | 33 restore_directory = true; |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
36 HMODULE module = (*load_library_api)(library_path.value().c_str()); | 37 HMODULE module = (*load_library_api)(library_path.value().c_str()); |
37 if (!module && error) { | 38 if (!module && error_code) { |
ddorwin
2014/03/21 17:24:59
This should be "...&& (error_code || error_message
xhwang
2014/03/21 19:00:54
Now this is obsolete.
| |
38 // GetLastError() needs to be called immediately after |load_library_api|. | 39 // GetLastError() needs to be called immediately after |load_library_api|. |
39 DWORD last_error = GetLastError(); | 40 DWORD last_error = GetLastError(); |
40 *error = StringPrintf("%u", last_error); | 41 *error_message = StringPrintf("%u", last_error); |
ddorwin
2014/03/21 17:24:59
You need to check for a NULL error_message.
Actua
xhwang
2014/03/21 19:00:54
I think the new NativeLibraryLoadError helps avoid
| |
42 *error_code = last_error; | |
41 } | 43 } |
42 | 44 |
43 if (restore_directory) | 45 if (restore_directory) |
44 SetCurrentDirectory(current_directory); | 46 SetCurrentDirectory(current_directory); |
45 | 47 |
46 return module; | 48 return module; |
47 } | 49 } |
48 | 50 |
49 // static | 51 // static |
50 NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 52 NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
51 std::string* error) { | 53 std::string* error_message, |
52 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); | 54 uint32* error_code) { |
55 return LoadNativeLibraryHelper( | |
56 library_path, LoadLibraryW, error_message, error_code); | |
53 } | 57 } |
54 | 58 |
55 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { | 59 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); | 60 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
57 | 61 |
58 LoadLibraryFunction load_library; | 62 LoadLibraryFunction load_library; |
59 load_library = reinterpret_cast<LoadLibraryFunction>( | 63 load_library = reinterpret_cast<LoadLibraryFunction>( |
60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); | 64 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
61 | 65 |
62 return LoadNativeLibraryHelper(library_path, load_library, NULL); | 66 return LoadNativeLibraryHelper(library_path, load_library, NULL, NULL); |
63 } | 67 } |
64 | 68 |
65 // static | 69 // static |
66 void UnloadNativeLibrary(NativeLibrary library) { | 70 void UnloadNativeLibrary(NativeLibrary library) { |
67 FreeLibrary(library); | 71 FreeLibrary(library); |
68 } | 72 } |
69 | 73 |
70 // static | 74 // static |
71 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 75 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
72 const char* name) { | 76 const char* name) { |
73 return GetProcAddress(library, name); | 77 return GetProcAddress(library, name); |
74 } | 78 } |
75 | 79 |
76 // static | 80 // static |
77 string16 GetNativeLibraryName(const string16& name) { | 81 string16 GetNativeLibraryName(const string16& name) { |
78 return name + ASCIIToUTF16(".dll"); | 82 return name + ASCIIToUTF16(".dll"); |
79 } | 83 } |
80 | 84 |
81 } // namespace base | 85 } // namespace base |
OLD | NEW |