blob: faf0bdba4be5fa3bbc74d4b960f6ea0adee4e7a9 [file] [log] [blame]
[email protected]84479322011-04-18 22:06:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f38e25f2009-04-21 00:56:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/native_library.h"
6
7#include <windows.h>
8
[email protected]e3177dd52014-08-13 20:22:149#include "base/files/file_util.h"
[email protected]f4e911452014-03-20 06:07:2610#include "base/strings/stringprintf.h"
[email protected]a4ea1f12013-06-07 18:37:0711#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread_restrictions.h"
[email protected]f38e25f2009-04-21 00:56:0713
14namespace base {
15
[email protected]3e246222010-11-19 23:33:1316typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);
17
[email protected]0f998442014-03-25 01:59:0918namespace {
19
[email protected]3e246222010-11-19 23:33:1320NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
[email protected]f4e911452014-03-20 06:07:2621 LoadLibraryFunction load_library_api,
[email protected]0f998442014-03-25 01:59:0922 NativeLibraryLoadError* error) {
[email protected]be130682010-11-12 21:53:1623 // LoadLibrary() opens the file off disk.
[email protected]37b3c1992014-03-11 20:59:0224 ThreadRestrictions::AssertIOAllowed();
[email protected]be130682010-11-12 21:53:1625
[email protected]f38e25f2009-04-21 00:56:0726 // Switch the current directory to the library directory as the library
27 // may have dependencies on DLLs in this directory.
28 bool restore_directory = false;
[email protected]188505282009-09-16 16:31:2829 FilePath current_directory;
[email protected]37b3c1992014-03-11 20:59:0230 if (GetCurrentDirectory(&current_directory)) {
[email protected]f38e25f2009-04-21 00:56:0731 FilePath plugin_path = library_path.DirName();
[email protected]188505282009-09-16 16:31:2832 if (!plugin_path.empty()) {
[email protected]37b3c1992014-03-11 20:59:0233 SetCurrentDirectory(plugin_path);
[email protected]f38e25f2009-04-21 00:56:0734 restore_directory = true;
35 }
36 }
37
[email protected]3e246222010-11-19 23:33:1338 HMODULE module = (*load_library_api)(library_path.value().c_str());
[email protected]f4e911452014-03-20 06:07:2639 if (!module && error) {
40 // GetLastError() needs to be called immediately after |load_library_api|.
[email protected]0f998442014-03-25 01:59:0941 error->code = GetLastError();
[email protected]f4e911452014-03-20 06:07:2642 }
43
[email protected]f38e25f2009-04-21 00:56:0744 if (restore_directory)
[email protected]37b3c1992014-03-11 20:59:0245 SetCurrentDirectory(current_directory);
[email protected]f38e25f2009-04-21 00:56:0746
47 return module;
48}
49
[email protected]0f998442014-03-25 01:59:0950} // namespace
51
52std::string NativeLibraryLoadError::ToString() const {
53 return StringPrintf("%u", code);
54}
55
[email protected]f38e25f2009-04-21 00:56:0756// static
[email protected]84479322011-04-18 22:06:2257NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0958 NativeLibraryLoadError* error) {
[email protected]f4e911452014-03-20 06:07:2659 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error);
[email protected]3e246222010-11-19 23:33:1360}
61
62NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) {
63 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);
64
thestige38fbd62016-06-10 21:54:4065 LoadLibraryFunction load_library = reinterpret_cast<LoadLibraryFunction>(
[email protected]3e246222010-11-19 23:33:1366 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW"));
67
[email protected]f4e911452014-03-20 06:07:2668 return LoadNativeLibraryHelper(library_path, load_library, NULL);
[email protected]3e246222010-11-19 23:33:1369}
70
71// static
[email protected]f38e25f2009-04-21 00:56:0772void UnloadNativeLibrary(NativeLibrary library) {
73 FreeLibrary(library);
74}
75
76// static
77void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4078 StringPiece name) {
79 return GetProcAddress(library, name.data());
[email protected]f38e25f2009-04-21 00:56:0780}
81
[email protected]108c2a12009-06-05 22:18:0982// static
thestige38fbd62016-06-10 21:54:4083string16 GetNativeLibraryName(StringPiece16 name) {
84 return name.as_string() + ASCIIToUTF16(".dll");
[email protected]108c2a12009-06-05 22:18:0985}
86
[email protected]f38e25f2009-04-21 00:56:0787} // namespace base