Chromium Code Reviews| 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, |
| 19 LoadLibraryFunction load_library_api, | 19 LoadLibraryFunction load_library_api, |
| 20 std::string* error) { | 20 uint32* error_code) { |
| 21 // LoadLibrary() opens the file off disk. | 21 // LoadLibrary() opens the file off disk. |
| 22 ThreadRestrictions::AssertIOAllowed(); | 22 ThreadRestrictions::AssertIOAllowed(); |
| 23 | 23 |
| 24 // Switch the current directory to the library directory as the library | 24 // Switch the current directory to the library directory as the library |
| 25 // may have dependencies on DLLs in this directory. | 25 // may have dependencies on DLLs in this directory. |
| 26 bool restore_directory = false; | 26 bool restore_directory = false; |
| 27 FilePath current_directory; | 27 FilePath current_directory; |
| 28 if (GetCurrentDirectory(¤t_directory)) { | 28 if (GetCurrentDirectory(¤t_directory)) { |
| 29 FilePath plugin_path = library_path.DirName(); | 29 FilePath plugin_path = library_path.DirName(); |
| 30 if (!plugin_path.empty()) { | 30 if (!plugin_path.empty()) { |
| 31 SetCurrentDirectory(plugin_path); | 31 SetCurrentDirectory(plugin_path); |
| 32 restore_directory = true; | 32 restore_directory = true; |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 | 35 |
| 36 HMODULE module = (*load_library_api)(library_path.value().c_str()); | 36 HMODULE module = (*load_library_api)(library_path.value().c_str()); |
| 37 if (!module && error) { | 37 if (!module && error_code) { |
| 38 // GetLastError() needs to be called immediately after |load_library_api|. | 38 // GetLastError() needs to be called immediately after |load_library_api|. |
| 39 DWORD last_error = GetLastError(); | 39 *error_code = GetLastError(); |
| 40 *error = StringPrintf("%u", last_error); | |
| 41 } | 40 } |
| 42 | 41 |
| 43 if (restore_directory) | 42 if (restore_directory) |
| 44 SetCurrentDirectory(current_directory); | 43 SetCurrentDirectory(current_directory); |
| 45 | 44 |
| 46 return module; | 45 return module; |
| 47 } | 46 } |
| 48 | 47 |
| 49 // static | 48 // static |
| 50 NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 49 NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
| 51 std::string* error) { | 50 std::string* /* error_message */, |
|
ddorwin
2014/03/20 22:01:12
Shouldn't we try to be as consistent as possible (
xhwang
2014/03/21 01:42:58
Hmm, then the log message will be (error: 5 - 5) o
ddorwin
2014/03/21 04:20:06
Yes, but if you don't set it, callers that want an
xhwang
2014/03/21 17:09:18
Done.
| |
| 52 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error); | 51 uint32* error_code) { |
| 52 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error_code); | |
| 53 } | 53 } |
| 54 | 54 |
| 55 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { | 55 NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) { |
| 56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); | 56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name); |
| 57 | 57 |
| 58 LoadLibraryFunction load_library; | 58 LoadLibraryFunction load_library; |
| 59 load_library = reinterpret_cast<LoadLibraryFunction>( | 59 load_library = reinterpret_cast<LoadLibraryFunction>( |
| 60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); | 60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")); |
| 61 | 61 |
| 62 return LoadNativeLibraryHelper(library_path, load_library, NULL); | 62 return LoadNativeLibraryHelper(library_path, load_library, NULL); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // static | 65 // static |
| 66 void UnloadNativeLibrary(NativeLibrary library) { | 66 void UnloadNativeLibrary(NativeLibrary library) { |
| 67 FreeLibrary(library); | 67 FreeLibrary(library); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // static | 70 // static |
| 71 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 71 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 72 const char* name) { | 72 const char* name) { |
| 73 return GetProcAddress(library, name); | 73 return GetProcAddress(library, name); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // static | 76 // static |
| 77 string16 GetNativeLibraryName(const string16& name) { | 77 string16 GetNativeLibraryName(const string16& name) { |
| 78 return name + ASCIIToUTF16(".dll"); | 78 return name + ASCIIToUTF16(".dll"); |
| 79 } | 79 } |
| 80 | 80 |
| 81 } // namespace base | 81 } // namespace base |
| OLD | NEW |