-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtils.hpp
194 lines (152 loc) · 3.64 KB
/
Utils.hpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
OmniMIDI v15+ (Rewrite) for Windows NT
This file contains the required code to run the driver under Windows 7 SP1 and later.
*/
#pragma once
#ifndef _UTILS_H
#define _UTILS_H
#include <Windows.h>
#include <guiddef.h>
#include <strsafe.h>
#include <cassert>
#include <ShlObj.h>
#define ImpMMFunc(f) LibImport((void**)&MM##f, #f)
#define ImpFunc(f) LibImport((void**)&f, #f)
#define loadLib LoadLibraryA
#define loadLibW LoadLibraryW
#define freeLib(x) FreeLibrary((HMODULE)x)
#define getLib GetModuleHandleA
#define getLibW GetModuleHandleW
#define getAddr(x, y) GetProcAddress((HMODULE)x, y)
namespace OmniMIDI {
enum FIDs {
System,
UserFolder
};
class SysPath {
public:
bool GetFolderPath(const FIDs FolderID, wchar_t* String, size_t StringLen);
};
class LibImport
{
protected:
void** funcptr = nullptr;
const char* funcname = nullptr;
public:
LibImport(void** pptr, const char* pfuncname) {
funcptr = pptr;
*(funcptr) = nullptr;
funcname = pfuncname;
}
~LibImport() {
*(funcptr) = nullptr;
}
void* GetPtr() { return *(funcptr); }
const char* GetName() { return funcname; }
bool SetPtr(void* lib = nullptr, const char* ptrname = nullptr) {
void* ptr = (void*)-1;
if (lib == nullptr && ptrname == nullptr)
{
if (funcptr)
*(funcptr) = nullptr;
return true;
}
ptr = (void*)getAddr(lib, ptrname);
if (!ptr)
return false;
if (ptr != *(funcptr))
*(funcptr) = ptr;
return true;
}
};
class Lib {
protected:
const wchar_t* Name;
void* Library = nullptr;
bool Initialized = false;
bool LoadFailed = false;
bool AppSelfHosted = false;
LibImport* Funcs = nullptr;
size_t FuncsCount = 0;
public:
void* Ptr() { return Library; }
bool IsOnline() { return (Library != nullptr && Initialized && !LoadFailed); }
Lib(const wchar_t* pName, LibImport** pFuncs, size_t pFuncsCount) {
Name = pName;
Funcs = *pFuncs;
FuncsCount = pFuncsCount;
}
~Lib() {
UnloadLib();
}
bool LoadLib(wchar_t* CustomPath = nullptr) {
OmniMIDI::SysPath Utils;
char CName[MAX_PATH] = { 0 };
wchar_t SysDir[MAX_PATH] = { 0 };
wchar_t DLLPath[MAX_PATH] = { 0 };
int swp = 0;
if (Library == nullptr) {
if (CustomPath != nullptr) {
swp = swprintf(DLLPath, MAX_PATH, L"%s\\%s.dll\0", CustomPath, Name);
assert(swp != -1);
if (swp != -1) {
wcstombs(CName, DLLPath, MAX_PATH);
Library = loadLibW(DLLPath);
if (!Library)
return false;
}
else return false;
}
else {
swp = swprintf(DLLPath, MAX_PATH, L"%s.dll\0", Name);
assert(swp != -1);
if (swp != -1) {
Library = loadLibW(DLLPath);
if (!Library)
{
if (Utils.GetFolderPath(FIDs::System, SysDir, sizeof(SysDir))) {
swp = swprintf(DLLPath, MAX_PATH, L"%s\\OmniMIDI\\%s.dll\0", SysDir, Name);
assert(swp != -1);
if (swp != -1) {
Library = loadLibW(DLLPath);
assert(Library != 0);
if (!Library) {
wcstombs(CName, Name, MAX_PATH);
return false;
}
}
else return false;
}
else return false;
}
}
else return false;
}
}
for (size_t i = 0; i < FuncsCount; i++)
Funcs[i].SetPtr(Library, Funcs[i].GetName());
Initialized = true;
return true;
}
bool UnloadLib() {
if (Library != nullptr) {
if (AppSelfHosted)
{
AppSelfHosted = false;
}
else {
bool r = freeLib(Library);
assert(r == true);
if (!r) {
throw;
}
}
Library = nullptr;
}
LoadFailed = false;
Initialized = false;
return true;
}
};
}
#endif