-
Notifications
You must be signed in to change notification settings - Fork 41
/
VoicemeeterAPOInfo.cpp
517 lines (433 loc) · 14.8 KB
/
VoicemeeterAPOInfo.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2017 Jonas Thedering
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.
*/
#include "stdafx.h"
#include <ShlObj.h>
#include <fstream>
#include <algorithm>
#include <KsMedia.h>
#include <shellapi.h>
#include <TlHelp32.h>
#include <Winternl.h>
#include "helpers/RegistryHelper.h"
#include "VoicemeeterAPOInfo.h"
using namespace std;
#define voicemeeterKeyPath L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\VB:Voicemeeter {17359A74-1236-5467}"
#define voicemeeterWowKeyPath L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\VB:Voicemeeter {17359A74-1236-5467}"
#define uninstallStringValueName L"UninstallString"
static const wchar_t* startupFilename = L"Equalizer APO Voicemeeter Client.lnk";
static const wchar_t* clientFilename = L"VoicemeeterClient.exe";
static const wchar_t* voicemeeterClientKeyPath = USER_REGPATH L"\\Voicemeeter Client";
static const wchar_t* sampleRateValueName = L"sampleRate";
typedef NTSTATUS(NTAPI* pfnNtQueryInformationProcess)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
void VoicemeeterAPOInfo::prependInfos(vector<shared_ptr<AbstractAPOInfo>>& list)
{
wstring voicemeeterDirectory;
if (RegistryHelper::keyExists(voicemeeterKeyPath))
voicemeeterDirectory = RegistryHelper::readValue(voicemeeterKeyPath, uninstallStringValueName);
else if (RegistryHelper::keyExists(voicemeeterWowKeyPath))
voicemeeterDirectory = RegistryHelper::readValue(voicemeeterWowKeyPath, uninstallStringValueName);
if (voicemeeterDirectory.length() > 0)
{
size_t index = voicemeeterDirectory.find_last_of(L'\\');
wstring setupFilename = voicemeeterDirectory.substr(index + 1);
long voicemeeterType = 1;
if (setupFilename == L"VoicemeeterProSetup.exe")
voicemeeterType = 2;// banana
else if (setupFilename == L"Voicemeeter8Setup.exe")
voicemeeterType = 3;// potato
unsigned outputCount;
if (voicemeeterType == 3)
outputCount = 5;
else if (voicemeeterType == 2)
outputCount = 3;
else
outputCount = 1;
bool defaultDevice = false;
list.erase(remove_if(list.begin(), list.end(), [&defaultDevice](shared_ptr<AbstractAPOInfo>& info) {
if (info->getDeviceName().find(L"VB-Audio VoiceMeeter") != wstring::npos)
{
if (info->isDefaultDevice())
defaultDevice = true;
return true;
}
return false;
}), list.end());
bool anyInstalled = false;
for (unsigned i = 0; i < outputCount; i++)
{
wstringstream sstream;
sstream << "Output A" << (i + 1);
shared_ptr<AbstractAPOInfo> info = *list.insert(list.begin() + i, make_shared<VoicemeeterAPOInfo>(sstream.str(), true));
if (info->isInstalled())
anyInstalled = true;
}
if (defaultDevice)
{
for (unsigned i = 0; i < outputCount; i++)
{
shared_ptr<VoicemeeterAPOInfo>& info = (shared_ptr<VoicemeeterAPOInfo>&)list[i];
if (!anyInstalled || info->isInstalled())
{
info->defaultDevice = true;
break;
}
}
}
}
else
{
// Voicemeeter was uninstalled but Voicemeeter Client might still be installed
wstring startupFilePath = getStartupPath();
wstring argString = getLinkArgs(startupFilePath);
vector<wstring> args = splitArgs(argString);
int i = 0;
for (wstring arg : args)
{
shared_ptr<AbstractAPOInfo> info = *list.insert(list.begin() + i, make_shared<VoicemeeterAPOInfo>(arg, false));
i++;
}
}
}
VoicemeeterAPOInfo::VoicemeeterAPOInfo(const wstring& connectionName, bool voicemeeterInstalled)
: connectionName(connectionName), voicemeeterInstalled(voicemeeterInstalled)
{
wstring startupFilePath = getStartupPath();
wstring path;
wstring argString = getLinkArgs(startupFilePath, &path);
vector<wstring> args = splitArgs(argString);
installed = find(args.begin(), args.end(), connectionName) != args.end();
sampleRate = 48000;
try
{
if (RegistryHelper::keyExists(voicemeeterClientKeyPath) && RegistryHelper::valueExists(voicemeeterClientKeyPath, sampleRateValueName))
sampleRate = (unsigned)RegistryHelper::readDWORDValue(voicemeeterClientKeyPath, sampleRateValueName);
}
catch (RegistryException e)
{
// ignore
}
wstring clientPath = getClientPath();
changes = (path != clientPath);
}
wstring VoicemeeterAPOInfo::getConnectionName() const
{
return connectionName;
}
wstring VoicemeeterAPOInfo::getDeviceName() const
{
return L"Voicemeeter";
}
wstring VoicemeeterAPOInfo::getDeviceGuid() const
{
return L"";
}
wstring VoicemeeterAPOInfo::getDeviceString() const
{
return getConnectionName() + L" " + getDeviceName();
}
unsigned VoicemeeterAPOInfo::getChannelCount() const
{
return 8;
}
unsigned VoicemeeterAPOInfo::getSampleRate() const
{
return sampleRate;
}
unsigned long VoicemeeterAPOInfo::getChannelMask() const
{
return KSAUDIO_SPEAKER_7POINT1_SURROUND;
}
bool VoicemeeterAPOInfo::isInput() const
{
return false;
}
bool VoicemeeterAPOInfo::isInstalled() const
{
return installed;
}
bool VoicemeeterAPOInfo::canBeUpgraded() const
{
return false;
}
bool VoicemeeterAPOInfo::hasChanges() const
{
return changes;
}
bool VoicemeeterAPOInfo::isExperimental() const
{
return false;
}
bool VoicemeeterAPOInfo::isEnhancementsDisabled() const
{
return false;
}
bool VoicemeeterAPOInfo::isDefaultDevice() const
{
return defaultDevice;
}
bool VoicemeeterAPOInfo::isDisabled() const
{
return false;
}
bool VoicemeeterAPOInfo::isUnplugged() const
{
return false;
}
bool VoicemeeterAPOInfo::isVoicemeeterInstalled() const
{
return voicemeeterInstalled;
}
void VoicemeeterAPOInfo::install()
{
wstring startupFilePath = getStartupPath();
wstring argString = getLinkArgs(startupFilePath);
vector<wstring> args = splitArgs(argString);
vector<wstring>::iterator it = find(args.begin(), args.end(), connectionName);
if (it == args.end())
args.push_back(connectionName);
sort(args.begin(), args.end());
argString = joinArgs(args);
wstring clientPath = getClientPath();
createLink(startupFilePath, clientPath, argString);
}
void VoicemeeterAPOInfo::uninstall()
{
wstring startupFilePath = getStartupPath();
wstring argString = getLinkArgs(startupFilePath);
vector<wstring> args = splitArgs(argString);
vector<wstring>::iterator it = find(args.begin(), args.end(), connectionName);
if (it != args.end())
args.erase(it);
if (args.size() > 0)
{
sort(args.begin(), args.end());
argString = joinArgs(args);
wchar_t filename[MAX_PATH];
GetModuleFileNameW(NULL, filename, ARRAYSIZE(filename));
PathRemoveFileSpecW(filename);
wstring clientPath = filename;
clientPath = clientPath + L"\\" + clientFilename;
createLink(startupFilePath, clientPath, argString);
}
else
{
DeleteFileW(startupFilePath.c_str());
}
}
void VoicemeeterAPOInfo::reinstall()
{
uninstall();
install();
}
wstring VoicemeeterAPOInfo::getStartupPath()
{
PWSTR startupPath;
SHGetKnownFolderPath(FOLDERID_Startup, KF_FLAG_DONT_UNEXPAND, NULL, &startupPath);
wstring result(startupPath);
CoTaskMemFree(startupPath);
return result + L"\\" + startupFilename;
}
wstring VoicemeeterAPOInfo::getClientPath()
{
wchar_t filename[MAX_PATH];
GetModuleFileNameW(NULL, filename, ARRAYSIZE(filename));
PathRemoveFileSpecW(filename);
wstring clientPath = filename;
clientPath = clientPath + L"\\" + clientFilename;
return clientPath;
}
void VoicemeeterAPOInfo::createLink(const wstring& lnkPath, const wstring& path, const wstring& args)
{
IShellLink* shellLink;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&shellLink);
if (SUCCEEDED(hr))
{
shellLink->SetPath(path.c_str());
shellLink->SetArguments(args.c_str());
IPersistFile* persistFile;
hr = shellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&persistFile);
if (SUCCEEDED(hr))
{
hr = persistFile->Save(lnkPath.c_str(), TRUE);
persistFile->Release();
}
shellLink->Release();
}
}
wstring VoicemeeterAPOInfo::getLinkArgs(const wstring& lnkPath, wstring* path)
{
wstring result;
IShellLink* shellLink;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&shellLink);
if (SUCCEEDED(hr))
{
IPersistFile* persistFile;
hr = shellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&persistFile);
if (SUCCEEDED(hr))
{
hr = persistFile->Load(lnkPath.c_str(), STGM_READ);
if (SUCCEEDED(hr))
{
wchar_t buf[MAX_PATH];
hr = shellLink->GetArguments(buf, ARRAYSIZE(buf));
if (SUCCEEDED(hr))
result = buf;
if (path != NULL)
{
hr = shellLink->GetPath(buf, ARRAYSIZE(buf), NULL, 0);
if (SUCCEEDED(hr))
*path = buf;
}
}
persistFile->Release();
}
shellLink->Release();
}
return result;
}
vector<wstring> VoicemeeterAPOInfo::splitArgs(const wstring& argString)
{
vector<wstring> result;
if (argString.length() > 0)
{
int argc;
wchar_t** argv = CommandLineToArgvW(argString.c_str(), &argc);
for (int i = 0; i < argc; i++)
result.push_back(argv[i]);
LocalFree(argv);
}
return result;
}
wstring VoicemeeterAPOInfo::joinArgs(const vector<wstring>& args)
{
wstring result;
for (const wstring& arg : args)
{
if (result.length() > 0)
result += L" ";
if (arg.find(' ') != wstring::npos)
result += L"\"" + arg + L"\"";
else
result += arg;
}
return result;
}
void VoicemeeterAPOInfo::ensureVoicemeeterClientRunning()
{
wstring startupFilePath = getStartupPath();
wstring argString = getLinkArgs(startupFilePath);
vector<wstring> args = splitArgs(argString);
wstring clientPath = getClientPath();
HANDLE tokenHandle;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenHandle))
throw RegistryException(L"Error in OpenProcessToken while taking ownership");
LUID luid;
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
throw RegistryException(L"Error in LookupPrivilegeValue while taking ownership");
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(tokenHandle, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
throw RegistryException(L"Error in AdjustTokenPrivileges while taking ownership");
HMODULE module = LoadLibraryW(L"ntdll.dll");
if (module == NULL)
throw exception("Could not load ntdll.dll");
SCOPE_EXIT{FreeLibrary(module); };
pfnNtQueryInformationProcess NtQueryInformationProcess = (pfnNtQueryInformationProcess)GetProcAddress(module, "NtQueryInformationProcess");
if (NtQueryInformationProcess == NULL)
throw exception("Function NtQueryInformationProcess not found in ntdll.dll");
HANDLE snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshotHandle == INVALID_HANDLE_VALUE)
throw exception("Could not take a snapshot of all processes");
SCOPE_EXIT{CloseHandle(snapshotHandle); };
bool matchingProcessExists = false;
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
bool loop = Process32FirstW(snapshotHandle, &entry) != 0;
while (loop)
{
if (wcscmp(entry.szExeFile, clientFilename) == 0)
{
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, entry.th32ProcessID);
if (processHandle == INVALID_HANDLE_VALUE)
throw exception("Could not open process");
SCOPE_EXIT{CloseHandle(processHandle); };
PROCESS_BASIC_INFORMATION basicInformation;
NTSTATUS status = NtQueryInformationProcess(processHandle, ProcessBasicInformation, &basicInformation, sizeof(basicInformation), NULL);
if (status < 0)
throw exception("Could not query process information");
_PEB peb;
if (!ReadProcessMemory(processHandle, basicInformation.PebBaseAddress, &peb, sizeof(peb), NULL))
throw exception("Could not read peb from process memory");
RTL_USER_PROCESS_PARAMETERS processParams;
if (!ReadProcessMemory(processHandle, peb.ProcessParameters, &processParams, sizeof(processParams), NULL))
throw exception("Could not read process parameters from process memory");
wchar_t* cmdLineBuf = new wchar_t[processParams.CommandLine.Length / sizeof(wchar_t)];
SCOPE_EXIT{delete cmdLineBuf; };
if (!ReadProcessMemory(processHandle, processParams.CommandLine.Buffer, cmdLineBuf, processParams.CommandLine.Length, NULL))
throw exception("Could not read command line from process memory");
wstring cmdLine(cmdLineBuf, processParams.CommandLine.Length / sizeof(wchar_t));
vector<wstring> processArgs = splitArgs(cmdLine);
wstring path = processArgs.front();
processArgs.erase(processArgs.begin());
if (path != clientPath || processArgs != args)
closeProcess(entry.th32ProcessID);
else
matchingProcessExists = true;
}
loop = Process32NextW(snapshotHandle, &entry) != 0;
}
if (!matchingProcessExists && !args.empty())
{
ShellExecuteW(NULL, NULL, clientPath.c_str(), argString.c_str(), NULL, SW_SHOWDEFAULT);
}
}
void VoicemeeterAPOInfo::saveVoicemeeterSampleRate(unsigned sampleRate)
{
try
{
RegistryHelper::createKey(voicemeeterClientKeyPath);
RegistryHelper::writeDWORDValue(voicemeeterClientKeyPath, sampleRateValueName, sampleRate);
}
catch (RegistryException e)
{
// ignore
}
}
void VoicemeeterAPOInfo::closeProcess(unsigned long processId)
{
HANDLE snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshotHandle == INVALID_HANDLE_VALUE)
throw exception("Could not take a snapshot of all processes");
SCOPE_EXIT{CloseHandle(snapshotHandle); };
THREADENTRY32 entry;
entry.dwSize = sizeof(THREADENTRY32);
bool loop = Thread32First(snapshotHandle, &entry) != 0;
while (loop)
{
if (entry.th32OwnerProcessID == processId)
// will just fail for threads not having a message queue
PostThreadMessageW(entry.th32ThreadID, WM_QUIT, 0, 0);
loop = Thread32Next(snapshotHandle, &entry) != 0;
}
}