-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDllLoader.h
More file actions
140 lines (121 loc) · 3.42 KB
/
Copy pathDllLoader.h
File metadata and controls
140 lines (121 loc) · 3.42 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// DllLoader.h
//
// Copyright (c) Nikolay Raspopov, 2009-2023.
// This file is part of USB Oblivion (https://www.cherubicsoft.com/en/projects/usboblivion/)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#pragma once
template <typename FuncTypeTTT>
struct TypeOnlyStruct
{
typedef FuncTypeTTT FuncType;
};
#define FuncPtrType(funcType) TypeOnlyStruct<funcType>::FuncType
#define FuncLoad(x,y) ((x).LoadFunc((dyn_##y),(#y)))
class CDllLoader
{
private:
CString m_sDllName;
HINSTANCE m_pDll;
public:
inline CDllLoader() noexcept
: m_pDll ( nullptr )
{
}
inline CDllLoader(LPCTSTR szDllName, bool bloadNow = true)
: m_sDllName ( szDllName )
, m_pDll ( nullptr )
{
if ( bloadNow )
{
LoadLibrary();
}
}
~CDllLoader()
{
Free();
}
inline bool Load(LPCTSTR szDllName)
{
// existing lib loaded?
if ( m_pDll )
return true;
m_sDllName = szDllName;
return LoadLibrary();
}
inline operator bool() const noexcept
{
return ( m_pDll != nullptr );
}
template < typename FuncTTT >
FuncTTT LoadFunc(FuncTTT& c, LPCSTR fNameStr)
{
// existing lib loaded?
if ( ! LoadLibrary() )
return (FuncTTT)nullptr;
// load func from dll
FuncTTT fPtr = (FuncTTT)GetProcAddress ( m_pDll, fNameStr );
if ( ! fPtr )
{
ATLTRACE( "ERROR: cannot locate function name (%s) from dll\n", fNameStr );
return (FuncTTT)nullptr;
}
c = fPtr;
return fPtr;
}
void Free() noexcept
{
if ( m_pDll )
{
ATLTRACE( _T("Unloading DLL %s ...\n"), (LPCTSTR)m_sDllName );
ATLVERIFY( ::FreeLibrary( m_pDll ) );
ATLTRACE( _T("DLL %s successfully unloaded.\n"), (LPCTSTR)m_sDllName );
m_pDll = nullptr;
}
}
bool LoadLibrary(HINSTANCE hInstance = nullptr)
{
// existing lib loaded?
if ( m_pDll )
return true;
// load from:
// 1. The directory from which the application loaded.
// 2. The current directory.
// 3. The Windows system directory.
// 4. The Windows directory.
// 5. The directories that are listed in the PATH environment variable.
ATLTRACE( _T("Trying LoadLibrary %s ...\n"), (LPCTSTR)m_sDllName );
m_pDll = ::LoadLibrary( m_sDllName );
if ( m_pDll )
return true;
// 6. The module directory (if dll).
if ( hInstance )
{
CString self;
GetModuleFileName( hInstance, self.GetBuffer( MAX_PATH + 1 ), MAX_PATH );
self.ReleaseBuffer ();
self = self.Mid( 0, self.ReverseFind( _T('\\') ) + 1 );
m_sDllName = self + m_sDllName;
ATLTRACE( _T("Trying LoadLibrary %s ...\n"), (LPCTSTR)m_sDllName );
m_pDll = ::LoadLibrary( m_sDllName );
if ( m_pDll )
return true;
}
ATLTRACE( _T("ERROR: cannot load library %s\n"), (LPCTSTR)m_sDllName );
return false;
}
};