1 /*
2  * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #ifndef DLLUTIL_H
27 #define DLLUTIL_H
28 
29 #include <tchar.h>
30 #include <windows.h>
31 
32 /**
33  * Utility class to handle dynamically loadable libraries.
34  *
35  * NOTE: THIS CLASS IS NOT THREAD-SAFE!
36  */
37 class DllUtil {
38     public:
39         class Exception {};
40         class LibraryUnavailableException : public Exception {};
41         class FunctionUnavailableException : public Exception {};
42 
43         FARPROC GetProcAddress(LPCSTR name);
44 
45     protected:
DllUtil(const char * name)46         DllUtil(const char * name) : name(name), module(NULL) {}
47         virtual ~DllUtil();
48 
49         HMODULE GetModule();
50 
51         template <class FunctionType> class Function {
52             public:
Function(DllUtil * dll,LPCSTR name)53                 Function(DllUtil * dll, LPCSTR name) :
54                     dll(dll), name(name), function(NULL) {}
55 
operator()56                 inline FunctionType operator () () {
57                     if (!function) {
58                         function = (FunctionType)dll->GetProcAddress(name);
59                     }
60                     return function;
61                 }
62 
63             private:
64                 DllUtil * const dll;
65                 LPCSTR name;
66 
67                 FunctionType function;
68         };
69 
70     private:
71         const char * const name;
72         HMODULE module;
73 };
74 
75 class DwmAPI : public DllUtil {
76     public:
77         // See DWMWINDOWATTRIBUTE enum in dwmapi.h
78         static const DWORD DWMWA_EXTENDED_FRAME_BOUNDS = 9;
79 
80         static HRESULT DwmIsCompositionEnabled(BOOL * pfEnabled);
81         static HRESULT DwmGetWindowAttribute(HWND hwnd, DWORD dwAttribute,
82                 PVOID pvAttribute, DWORD cbAttribute);
83 
84     private:
85         static DwmAPI & GetInstance();
86         DwmAPI();
87 
88         typedef HRESULT (WINAPI *DwmIsCompositionEnabledType)(BOOL*);
89         Function<DwmIsCompositionEnabledType> DwmIsCompositionEnabledFunction;
90 
91         typedef HRESULT (WINAPI *DwmGetWindowAttributeType)(HWND hwnd, DWORD dwAttribute,
92                 PVOID pvAttribute, DWORD cbAttribute);
93         Function<DwmGetWindowAttributeType> DwmGetWindowAttributeFunction;
94 };
95 
96 #endif // DLLUTIL_H
97 
98