blob: bcea485a1d10582016622738f1081fde471cd567 [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]188505282009-09-16 16:31:289#include "base/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
18NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
[email protected]f4e911452014-03-20 06:07:2619 LoadLibraryFunction load_library_api,
20 std::string* error) {
[email protected]be130682010-11-12 21:53:1621 // LoadLibrary() opens the file off disk.
[email protected]37b3c1992014-03-11 20:59:0222 ThreadRestrictions::AssertIOAllowed();
[email protected]be130682010-11-12 21:53:1623
[email protected]f38e25f2009-04-21 00:56:0724 // Switch the current directory to the library directory as the library
25 // may have dependencies on DLLs in this directory.
26 bool restore_directory = false;
[email protected]188505282009-09-16 16:31:2827 FilePath current_directory;
[email protected]37b3c1992014-03-11 20:59:0228 if (GetCurrentDirectory(&current_directory)) {
[email protected]f38e25f2009-04-21 00:56:0729 FilePath plugin_path = library_path.DirName();
[email protected]188505282009-09-16 16:31:2830 if (!plugin_path.empty()) {
[email protected]37b3c1992014-03-11 20:59:0231 SetCurrentDirectory(plugin_path);
[email protected]f38e25f2009-04-21 00:56:0732 restore_directory = true;
33 }
34 }
35
[email protected]3e246222010-11-19 23:33:1336 HMODULE module = (*load_library_api)(library_path.value().c_str());
[email protected]f4e911452014-03-20 06:07:2637 if (!module && error) {
38 // GetLastError() needs to be called immediately after |load_library_api|.
39 DWORD last_error = GetLastError();
40 *error = StringPrintf("%u", last_error);
41 }
42
[email protected]f38e25f2009-04-21 00:56:0743 if (restore_directory)
[email protected]37b3c1992014-03-11 20:59:0244 SetCurrentDirectory(current_directory);
[email protected]f38e25f2009-04-21 00:56:0745
46 return module;
47}
48
49// static
[email protected]84479322011-04-18 22:06:2250NativeLibrary LoadNativeLibrary(const FilePath& library_path,
51 std::string* error) {
[email protected]f4e911452014-03-20 06:07:2652 return LoadNativeLibraryHelper(library_path, LoadLibraryW, error);
[email protected]3e246222010-11-19 23:33:1353}
54
55NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path) {
56 typedef HMODULE (WINAPI* LoadLibraryFunction)(const wchar_t* file_name);
57
58 LoadLibraryFunction load_library;
59 load_library = reinterpret_cast<LoadLibraryFunction>(
60 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW"));
61
[email protected]f4e911452014-03-20 06:07:2662 return LoadNativeLibraryHelper(library_path, load_library, NULL);
[email protected]3e246222010-11-19 23:33:1363}
64
65// static
[email protected]f38e25f2009-04-21 00:56:0766void UnloadNativeLibrary(NativeLibrary library) {
67 FreeLibrary(library);
68}
69
70// static
71void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0972 const char* name) {
[email protected]f38e25f2009-04-21 00:56:0773 return GetProcAddress(library, name);
74}
75
[email protected]108c2a12009-06-05 22:18:0976// static
77string16 GetNativeLibraryName(const string16& name) {
78 return name + ASCIIToUTF16(".dll");
79}
80
[email protected]f38e25f2009-04-21 00:56:0781} // namespace base