summaryrefslogtreecommitdiffstats
path: root/third_party/cld/bar/common/scopedlibrary.h
blob: 0e8ceee5d94c1de39f9e03238d8aef3b7f747640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BAR_COMMON_SCOPEDLIBRARY_H_
#define BAR_COMMON_SCOPEDLIBRARY_H_


// A scoped object to safely load and free a DLL referenced by name.
// Provides an access to a handle to loaded library (HMODULE type).
//
// Example:
//     ScopedLibrary library(LIBRARY_NAME);
//     ... = ::GetProcAddress(library.handle(), FUNCTION_NAME);
class ScopedLibrary {
 public:
  // Always creates initialized ScopedLibrary.
  // [in] file_name - library's file name.
  explicit ScopedLibrary(const TCHAR *file_name)
      : library_(::LoadLibrary(file_name)) {}
  // Unloads owned library, if any.
  ~ScopedLibrary() {
    if (library_ != NULL)
      ::FreeLibrary(library_);
  }
  inline HMODULE handle() const { return library_; }
  // Returns true if library was loaded successfully.
  bool IsValid() const { return library_ != NULL; }

 private:
  // Handle to loaded library.
  const HMODULE library_;
  DISALLOW_COPY_AND_ASSIGN(ScopedLibrary);
};


// A class representing a pointer to a function retrieved from DLL.
// FunctionPrototype is a regular C-style pointer-to-function type
// definition. For example, type of WinAPI IsValidSid function:
//     BOOL (WINAPI*)(PSID)
//
// Example:
//     FunctionFromDll<BOOL (WINAPI*)(PSID)> is_valid_sid;
//     ... = is_valid_sid.function()(...);
template<typename FunctionPrototype>
class FunctionFromDll {
 public:
  FunctionFromDll() : function_(NULL) {}
  // Binds this object to a function from DLL.
  // [in] library - handle to a library containing a function.
  //     Must not be NULL.
  // [in] name - name of the function.
  void Bind(HMODULE library, const char *name) {
    function_ =
        reinterpret_cast<FunctionPrototype>(::GetProcAddress(library, name));
  }
  inline FunctionPrototype function() const { return function_; }
  // Returns true if function was bound successfully.
  bool IsValid() const { return function_ != NULL; }

 private:
  // Pointer to the function.
  FunctionPrototype function_;
  DISALLOW_COPY_AND_ASSIGN(FunctionFromDll);
};


#endif  // BAR_COMMON_SCOPEDLIBRARY_H_