blob: 64c7380f173247c28b78e1f339b963d7374cf83f [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"
thestig02c965b2016-06-14 18:52:2310#include "base/strings/string_util.h"
[email protected]f4e911452014-03-20 06:07:2611#include "base/strings/stringprintf.h"
[email protected]a4ea1f12013-06-07 18:37:0712#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread_restrictions.h"
[email protected]f38e25f2009-04-21 00:56:0714
15namespace base {
16
[email protected]3e246222010-11-19 23:33:1317typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);
18
[email protected]0f998442014-03-25 01:59:0919namespace {
20
[email protected]3e246222010-11-19 23:33:1321NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
[email protected]f4e911452014-03-20 06:07:2622 LoadLibraryFunction load_library_api,
[email protected]0f998442014-03-25 01:59:0923 NativeLibraryLoadError* error) {
[email protected]be130682010-11-12 21:53:1624 // LoadLibrary() opens the file off disk.
[email protected]37b3c1992014-03-11 20:59:0225 ThreadRestrictions::AssertIOAllowed();
[email protected]be130682010-11-12 21:53:1626
[email protected]f38e25f2009-04-21 00:56:0727 // Switch the current directory to the library directory as the library
28 // may have dependencies on DLLs in this directory.
29 bool restore_directory = false;
[email protected]188505282009-09-16 16:31:2830 FilePath current_directory;
[email protected]37b3c1992014-03-11 20:59:0231 if (GetCurrentDirectory(&current_directory)) {
[email protected]f38e25f2009-04-21 00:56:0732 FilePath plugin_path = library_path.DirName();
[email protected]188505282009-09-16 16:31:2833 if (!plugin_path.empty()) {
[email protected]37b3c1992014-03-11 20:59:0234 SetCurrentDirectory(plugin_path);
[email protected]f38e25f2009-04-21 00:56:0735 restore_directory = true;
36 }
37 }
38
[email protected]3e246222010-11-19 23:33:1339 HMODULE module = (*load_library_api)(library_path.value().c_str());
[email protected]f4e911452014-03-20 06:07:2640 if (!module && error) {
41 // GetLastError() needs to be called immediately after |load_library_api|.
[email protected]0f998442014-03-25 01:59:0942 error->code = GetLastError();
[email protected]f4e911452014-03-20 06:07:2643 }
44
[email protected]f38e25f2009-04-21 00:56:0745 if (restore_directory)
[email protected]37b3c1992014-03-11 20:59:0246 SetCurrentDirectory(current_directory);
[email protected]f38e25f2009-04-21 00:56:0747
48 return module;
49}
50
[email protected]0f998442014-03-25 01:59:0951} // namespace
52
53std::string NativeLibraryLoadError::ToString() const {
54 return StringPrintf("%u", code);
55}
56
[email protected]f38e25f2009-04-21 00:56:0757// static
rockot596a0dd2016-08-26 00:57:5158NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
59 const NativeLibraryOptions& options,
60 NativeLibraryLoadError* error) {
[email protected]f4e911452014-03-20 06:07:2661 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error);
[email protected]3e246222010-11-19 23:33:1362}
63
64NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) {
65 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);
66
thestige38fbd62016-06-10 21:54:4067 LoadLibraryFunction load_library = reinterpret_cast<LoadLibraryFunction>(
[email protected]3e246222010-11-19 23:33:1368 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW"));
69
[email protected]f4e911452014-03-20 06:07:2670 return LoadNativeLibraryHelper(library_path, load_library, NULL);
[email protected]3e246222010-11-19 23:33:1371}
72
73// static
[email protected]f38e25f2009-04-21 00:56:0774void UnloadNativeLibrary(NativeLibrary library) {
75 FreeLibrary(library);
76}
77
78// static
79void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4080 StringPiece name) {
81 return GetProcAddress(library, name.data());
[email protected]f38e25f2009-04-21 00:56:0782}
83
[email protected]108c2a12009-06-05 22:18:0984// static
thestig02c965b2016-06-14 18:52:2385std::string GetNativeLibraryName(StringPiece name) {
86 DCHECK(IsStringASCII(name));
87 return name.as_string() + ".dll";
[email protected]108c2a12009-06-05 22:18:0988}
89
[email protected]f38e25f2009-04-21 00:56:0790} // namespace base