Version 0.9

Added: Filter type "IIR", which allows to specify a user-defined IIR filter of arbitrary order with custom coefficients.
Improved: For playback devices, the APO is now installed both as GFX (post-mix) and LFX (pre-mix) APO. Normally the filtering happens in GFX, but the command "Stage" allows to select LFX to apply effects based on the number of input channels.
Added: Expression language. The commands "If", "ElseIf", "Else", "EndIf", "Eval" and inline expressions allow to change the filter behaviour based on runtime conditions.
Added: Command "Copy", which copies audio data between channels.
Added: Command "Delay", which delays the audio on the currently selected channels.
Improved: Configuration files are now locked exclusively for writing while reading them.
Improved: There is now a transition (10 ms) from old to new configuration after loading.
Improved: Substantial internal restructuring to improve extensibility and maintainability. Filtering commands now work exactly in the order they were specified.
Fixed: When the microphone input was in stereo but the application requested a mono stream, the audio was heavily distorted.
Fixed: The sine sweep generated by the Benchmark application had floating-point precision issues leading to severe artifacts when certain filters were applied.
This commit is contained in:
jthedering committed 2014-08-10 18:35:59 +00:00
1 parent 11e52f6210
commit 90ee2b0aa9
89 files changed
+6396 -1375

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2012 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 <cstdarg>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "RegistryHelper.h"
#include "LogHelper.h"
using namespace std;
bool LogHelper::initialized = false;
wstring LogHelper::logPath;
bool LogHelper::enableTrace = false;
FILE* LogHelper::presetFP = NULL;
bool LogHelper::compact = false;
bool LogHelper::useConsoleColors = false;
void LogHelper::log(const char* file, int line, const void* caller, bool trace, const wchar_t* format, ...)
{
if(!initialized)
{
//Do not try to initialize again, even in case of error
initialized = true;
wchar_t temp[255];
GetTempPathW(sizeof(temp)/sizeof(wchar_t), temp);
logPath = temp;
logPath += L"EqualizerAPO.log";
try
{
if(RegistryHelper::readValue(APP_REGPATH, L"EnableTrace") != L"false")
enableTrace = true;
}
catch(RegistryException e)
{
LogFStatic(L"%s", e.getMessage());
}
}
if(trace && !enableTrace)
return;
FILE* fp;
if(presetFP == NULL)
{
errno_t err = _wfopen_s(&fp, logPath.c_str(), L"at");
if(err != 0)
return;
}
else
{
fp = presetFP;
}
if(useConsoleColors)
{
HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
if(trace)
SetConsoleTextAttribute(con, 2); // Set console color to green
else
SetConsoleTextAttribute(con, 12); // Set console color to red
}
if(!compact)
{
SYSTEMTIME ___st;
GetLocalTime(&___st);
DWORD threadId = GetCurrentThreadId();
fwprintf(fp, L"%04d-%02d-%02d %02d:%02d:%02d.%03d %d %08X (%S:%d): ",
___st.wYear, ___st.wMonth, ___st.wDay, ___st.wHour, ___st.wMinute, ___st.wSecond, ___st.wMilliseconds, threadId, caller, file, line);
}
if(trace)
fwprintf(fp, L"(TRACE) ");
va_list varArgs;
va_start(varArgs, format);
vfwprintf(fp, format, varArgs);
va_end(varArgs);
fwprintf(fp, L"\n");
if(useConsoleColors)
{
HANDLE con = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(con, 7); // Set console color to light grey (default)
}
if(presetFP == NULL)
fclose(fp);
}
void LogHelper::reset()
{
initialized = false;
logPath = L"";
enableTrace = false;
}
void LogHelper::set(FILE* fp, bool enableTrace, bool compact, bool useConsoleColors)
{
LogHelper::initialized = true;
LogHelper::presetFP = fp;
LogHelper::enableTrace = enableTrace;
LogHelper::compact = compact;
LogHelper::useConsoleColors = useConsoleColors;
}
+44
View File
@@ -0,0 +1,44 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2012 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.
*/
#pragma once
#include <string>
#include <cstdio>
#define TraceF(format, ...) LogHelper::log(__FILE__, __LINE__, this, true, format, __VA_ARGS__)
#define TraceFStatic(format, ...) LogHelper::log(__FILE__, __LINE__, NULL, true, format, __VA_ARGS__)
#define LogF(format, ...) LogHelper::log(__FILE__, __LINE__, this, false, format, __VA_ARGS__)
#define LogFStatic(format, ...) LogHelper::log(__FILE__, __LINE__, NULL, false, format, __VA_ARGS__)
class LogHelper
{
public:
static void log(const char* file, int line, const void* caller, bool trace, const wchar_t* format, ...);
static void reset();
static void set(FILE* fp, bool enableTrace, bool compact, bool useConsoleColors);
private:
static bool initialized;
static std::wstring logPath;
static bool enableTrace;
static FILE* presetFP;
static bool compact;
static bool useConsoleColors;
};
+112
View File
@@ -0,0 +1,112 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2013 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.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef _DEBUG
#include <stdlib.h>
#include <crtdbg.h>
#endif
#ifdef USE_WINDDK
// disable the declarations to use our own (see below)
#define AERT_Allocate _AERT_Allocate_disabled
#define AERT_Free _AERT_Free_disabled
#include <BaseAudioProcessingObject.h>
#undef AERT_Allocate
#undef AERT_Free
#endif
#include "LogHelper.h"
#include "MemoryHelper.h"
#ifdef USE_WINDDK
// someone forgot to add the __stdcall/WINAPI modifier to BaseAudioProcessingObject.h, so we can't use the existing declarations
extern "C"
{
HRESULT __stdcall AERT_Allocate(size_t size, void **pMemory);
HRESULT __stdcall AERT_Free(void *pMemory);
}
#endif
void* MemoryHelper::alloc(size_t size)
{
void* memory;
bool alternative = false;
size += 16;
#ifdef USE_WINDDK
HRESULT hr = AERT_Allocate(size, &memory);
if(FAILED(hr))
{
size += 16;
memory = malloc(size);
alternative = true;
}
#else
#ifdef _DEBUG
memory = _malloc_dbg(size,_NORMAL_BLOCK, __FILE__, __LINE__);
#else
memory = malloc(size);
#endif
#endif
if(memory == NULL)
{
LogFStatic(L"Allocation of %d bytes failed.", size);
return NULL;
}
size_t offset = 16 - ((size_t)memory) % 16;
if(alternative)
offset += 16;
void* ptr = ((char*)memory) + offset;
((char*)ptr)[-1] = (char)offset;
return ptr;
}
void MemoryHelper::free(void* ptr)
{
bool alternative = false;
char offset = ((char*)ptr)[-1];
if(offset > 16)
alternative = true;
void* memory = ((char*)ptr) - offset;
#ifdef USE_WINDDK
if(alternative)
{
::free(memory);
}
else
{
HRESULT hr = AERT_Free(memory);
if(FAILED(hr))
{
LogFStatic(L"Memory release failed. Error code %X", hr);
}
}
#else
#ifdef _DEBUG
_free_dbg(memory,_NORMAL_BLOCK);
#else
::free(memory);
#endif
#endif
}
+36
View File
@@ -0,0 +1,36 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2013 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.
*/
#pragma once
#ifdef USE_WINDDK
#include <BaseAudioProcessingObject.h>
#else
#define AVRT_VTABLES_BEGIN
#define AVRT_VTABLES_END
#define AVRT_CODE_BEGIN
#define AVRT_CODE_END
#endif
class MemoryHelper
{
public:
static void* alloc(size_t size);
static void free(void* ptr);
};
+48
View File
@@ -0,0 +1,48 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2012 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.
*/
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
class PrecisionTimer
{
LARGE_INTEGER freq;
LARGE_INTEGER startCount;
public:
PrecisionTimer()
{
QueryPerformanceFrequency(&freq);
}
void start()
{
QueryPerformanceCounter(&startCount);
}
double stop()
{
LARGE_INTEGER stopCount;
QueryPerformanceCounter(&stopCount);
return double(stopCount.QuadPart - startCount.QuadPart) / freq.QuadPart;
}
};
+447
View File
@@ -0,0 +1,447 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2012 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 <fstream>
#include <sstream>
#include <ObjBase.h>
#include <aclapi.h>
#include "StringHelper.h"
#include "RegistryHelper.h"
using namespace std;
DWORD RegistryHelper::windowsVersion = 0;
wstring RegistryHelper::readValue(wstring key, wstring valuename)
{
wstring result;
HKEY keyHandle = openKey(key, KEY_QUERY_VALUE | KEY_WOW64_64KEY);
LSTATUS status;
DWORD type;
DWORD bufSize;
status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, &type, NULL, &bufSize);
if(status != ERROR_SUCCESS)
{
RegCloseKey(keyHandle);
throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
if(type != REG_SZ)
{
RegCloseKey(keyHandle);
throw RegistryException(L"Registry value " + key + L"\\" + valuename + L" has wrong type");
}
wchar_t* buf = new wchar_t[bufSize / sizeof(wchar_t) + 1];
status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, NULL, (LPBYTE)buf, &bufSize);
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
{
delete buf;
throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
//Remove zero-termination
if(buf[bufSize / sizeof(wchar_t) - 1] == L'\0')
bufSize -= sizeof(wchar_t);
result = wstring((wchar_t*)buf, (wstring::size_type)bufSize/sizeof(wchar_t));
delete buf;
return result;
}
unsigned long RegistryHelper::readDWORDValue(wstring key, wstring valuename)
{
unsigned long result;
HKEY keyHandle = openKey(key, KEY_QUERY_VALUE | KEY_WOW64_64KEY);
LSTATUS status;
DWORD type;
DWORD bufSize;
status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, &type, NULL, &bufSize);
if(status != ERROR_SUCCESS)
{
RegCloseKey(keyHandle);
throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
if(type != REG_DWORD)
{
RegCloseKey(keyHandle);
throw RegistryException(L"Registry value " + key + L"\\" + valuename + L" has wrong type");
}
BYTE* buf = new BYTE[bufSize];
status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, NULL, buf, &bufSize);
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
{
delete buf;
throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
result = ((unsigned long*)buf)[0];
delete buf;
return result;
}
void RegistryHelper::writeValue(wstring key, wstring valuename, wstring value)
{
HKEY keyHandle = openKey(key, KEY_SET_VALUE | KEY_WOW64_64KEY);
LSTATUS status = RegSetValueExW(keyHandle, valuename.c_str(), 0, REG_SZ, (const BYTE*)value.c_str(), (DWORD)((value.size()+1)*sizeof(wchar_t)));
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while writing to registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
void RegistryHelper::writeMultiValue(wstring key, wstring valuename, wstring value)
{
HKEY keyHandle = openKey(key, KEY_SET_VALUE | KEY_WOW64_64KEY);
wchar_t* data = new wchar_t[value.size() + 2];
value._Copy_s(data, (value.size()+2)*sizeof(wchar_t), value.size());
data[value.size()] = L'\0';
data[value.size()+1] = L'\0';
LSTATUS status = RegSetValueExW(keyHandle, valuename.c_str(), 0, REG_MULTI_SZ, (const BYTE*)data, (DWORD)((value.size()+2)*sizeof(wchar_t)));
delete data;
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while writing to registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
void RegistryHelper::deleteValue(wstring key, wstring valuename)
{
HKEY keyHandle = openKey(key, KEY_SET_VALUE | KEY_WOW64_64KEY);
LSTATUS status = RegDeleteValueW(keyHandle, valuename.c_str());
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while deleting registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
}
void RegistryHelper::createKey(wstring key)
{
HKEY rootKey;
wstring subKey = splitKey(key, &rootKey);
HKEY keyHandle;
LSTATUS status = RegCreateKeyExW(rootKey, subKey.c_str(), 0, NULL, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, NULL, &keyHandle, NULL);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while creating registry key " + key + L": " + StringHelper::getSystemErrorString(status));
RegCloseKey(keyHandle);
}
void RegistryHelper::deleteKey(wstring key)
{
HKEY rootKey;
wstring subKey = splitKey(key, &rootKey);
LSTATUS status = RegDeleteKeyExW(rootKey, subKey.c_str(), KEY_WOW64_64KEY, 0);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while deleting registry key " + key + L": " + StringHelper::getSystemErrorString(status));
}
void RegistryHelper::makeWritable(wstring key)
{
HKEY keyHandle = openKey(key, READ_CONTROL | WRITE_DAC | KEY_WOW64_64KEY);
DWORD descriptorSize = 0;
RegGetKeySecurity(keyHandle, DACL_SECURITY_INFORMATION, NULL, &descriptorSize);
PSECURITY_DESCRIPTOR oldSd = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, descriptorSize);
LSTATUS status = RegGetKeySecurity(keyHandle, DACL_SECURITY_INFORMATION, oldSd, &descriptorSize);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while getting security information for registry key " + key + L": " + StringHelper::getSystemErrorString(status));
BOOL aclPresent, aclDefaulted;
PACL oldAcl = NULL;
if(!GetSecurityDescriptorDacl(oldSd, &aclPresent, &oldAcl, &aclDefaulted))
throw RegistryException(L"Error in GetSecurityDescriptorDacl while ensuring writability");
PSID sid = NULL;
SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
if(!AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &sid))
throw RegistryException(L"Error in AllocateAndInitializeSid while ensuring writability");
EXPLICIT_ACCESS ea;
ea.grfAccessPermissions = KEY_ALL_ACCESS;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
ea.Trustee.ptstrName = (LPWSTR)sid;
PACL acl = NULL;
if(ERROR_SUCCESS != SetEntriesInAcl(1, &ea, oldAcl, &acl))
throw RegistryException(L"Error in SetEntriesInAcl while ensuring writability");
PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if(NULL == sd)
throw RegistryException(L"Error in LocalAlloc while ensuring writability");
if(!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION))
throw RegistryException(L"Error in InitializeSecurityDescriptor while ensuring writability");
if(!SetSecurityDescriptorDacl(sd, TRUE, acl, FALSE))
throw RegistryException(L"Error in SetSecurityDescriptorDacl while ensuring writability");
status = RegSetKeySecurity(keyHandle, DACL_SECURITY_INFORMATION, sd);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while setting security information for registry key " + key + L": " + StringHelper::getSystemErrorString(status));
FreeSid(sid);
LocalFree(acl);
HeapFree(GetProcessHeap(), 0, oldSd);
LocalFree(sd);
}
void RegistryHelper::takeOwnership(wstring key)
{
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_TAKE_OWNERSHIP_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");
HKEY keyHandle = openKey(key, WRITE_OWNER | KEY_WOW64_64KEY);
PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if(NULL == sd)
throw RegistryException(L"Error in SetPrivilege while taking ownership");
if(!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION))
throw RegistryException(L"Error in InitializeSecurityDescriptor while taking ownership");
PSID sid = NULL;
SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
if(!AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &sid))
throw RegistryException(L"Error in AllocateAndInitializeSid while taking ownership");
if(!SetSecurityDescriptorOwner(sd, sid, FALSE))
throw RegistryException(L"Error in SetSecurityDescriptorOwner while taking ownership");
LSTATUS status = RegSetKeySecurity(keyHandle, OWNER_SECURITY_INFORMATION, sd);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while setting security information for registry key " + key + L": " + StringHelper::getSystemErrorString(status));
tp.Privileges[0].Attributes = 0;
if(!AdjustTokenPrivileges(tokenHandle, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
throw RegistryException(L"Error in AdjustTokenPrivileges while taking ownership");
FreeSid(sid);
LocalFree(sd);
}
bool RegistryHelper::keyExists(wstring key)
{
bool result;
HKEY rootKey;
wstring subKey = splitKey(key, &rootKey);
HKEY keyHandle;
result = (RegOpenKeyExW(rootKey, subKey.c_str(), 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &keyHandle) == ERROR_SUCCESS);
if(result)
RegCloseKey(keyHandle);
return result;
}
bool RegistryHelper::valueExists(wstring key, wstring valuename)
{
HKEY keyHandle = openKey(key, KEY_QUERY_VALUE | KEY_WOW64_64KEY);
DWORD type;
DWORD bufSize;
LSTATUS status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, &type, NULL, &bufSize);
RegCloseKey(keyHandle);
return status == ERROR_SUCCESS;
}
vector<wstring> RegistryHelper::enumSubKeys(wstring key)
{
vector<wstring> result;
HKEY keyHandle = openKey(key, KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY);
wchar_t keyName[256];
DWORD keyLength = sizeof(keyName) / sizeof(wchar_t);
int i=0;
int itemCount=0;
LSTATUS status;
while((status = RegEnumKeyExW(keyHandle, i++, keyName, &keyLength, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
{
keyLength = sizeof(keyName) / sizeof(wchar_t);
result.push_back(keyName);
}
RegCloseKey(keyHandle);
if(status != ERROR_NO_MORE_ITEMS)
throw RegistryException(L"Error while enumerating sub keys of registry key " + key + L": " + StringHelper::getSystemErrorString(status));
return result;
}
bool RegistryHelper::keyEmpty(wstring key)
{
HKEY keyHandle = openKey(key, KEY_QUERY_VALUE | KEY_WOW64_64KEY);
DWORD keyCount;
DWORD valueCount;
LSTATUS status = RegQueryInfoKeyW(keyHandle, NULL, NULL, NULL, &keyCount, NULL, NULL, &valueCount, NULL, NULL, NULL, NULL);
RegCloseKey(keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while reading info for registry key " + key + L": " + StringHelper::getSystemErrorString(status));
return keyCount == 0 && valueCount == 0;
}
void RegistryHelper::saveToFile(wstring key, vector<wstring> valuenames, wstring filepath)
{
wofstream stream(filepath);
if(!stream.good())
throw RegistryException(L"Error while opening file " + filepath + L" for writing");
stream << L"Windows Registry Editor Version 5.00\n" << endl;
stream << L"[HKEY_LOCAL_MACHINE\\" << key << L"]" << endl;
for(vector<wstring>::iterator it = valuenames.begin(); it != valuenames.end(); it++)
{
wstring& valuename = *it;
wstring value = readValue(key, valuename);
stream << L"\"" << valuename << L"\"=\"" << value << L"\"" << endl;
}
stream << endl;
stream.close();
}
wstring RegistryHelper::getGuidString(GUID guid)
{
wchar_t* temp;
if(FAILED(StringFromCLSID(guid, &temp)))
throw RegistryException(L"Could not convert GUID to string");
std::wstring result(temp);
CoTaskMemFree(temp);
return result;
}
bool RegistryHelper::isWindowsVersionAtLeast(unsigned major, unsigned minor)
{
if(windowsVersion == 0)
{
DWORD handle;
DWORD size = GetFileVersionInfoSizeW(L"kernel32.dll", &handle);
if(size != 0)
{
void* data = malloc(size);
if(GetFileVersionInfo(L"kernel32.dll", handle, size, data))
{
VS_FIXEDFILEINFO* info;
UINT len;
if(VerQueryValueW(data, L"\\", (LPVOID*)&info, &len))
windowsVersion = info->dwProductVersionMS;
}
free(data);
}
}
// this will only work for major and minor up to 99
DWORD compareVersion = ((major / 10) << 20) + ((major % 10) << 16) + ((minor / 10) << 4) + (minor % 10);
return windowsVersion >= compareVersion;
}
HKEY RegistryHelper::openKey(const wstring& key, REGSAM samDesired)
{
HKEY rootKey;
wstring subKey = splitKey(key, &rootKey);
HKEY keyHandle;
LSTATUS status = RegOpenKeyExW(rootKey, subKey.c_str(), 0, samDesired, &keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while opening registry key " + key + L": " + StringHelper::getSystemErrorString(status));
return keyHandle;
}
wstring RegistryHelper::splitKey(const wstring& key, HKEY* rootKey)
{
size_t pos = key.find(L'\\');
if(pos == wstring::npos)
throw RegistryException(L"Key " + key + L" has invalid format");
wstring rootPart = key.substr(0, pos);
wstring pathPart = key.substr(pos+1);
wstring p = StringHelper::toUpperCase(rootPart);
if(p == L"HKEY_CLASSES_ROOT")
*rootKey = HKEY_CLASSES_ROOT;
else if(p == L"HKEY_CURRENT_CONFIG")
*rootKey = HKEY_CURRENT_CONFIG;
else if(p == L"HKEY_CURRENT_USER")
*rootKey = HKEY_CURRENT_USER;
else if(p == L"HKEY_LOCAL_MACHINE")
*rootKey = HKEY_LOCAL_MACHINE;
else if(p == L"HKEY_USERS")
*rootKey = HKEY_USERS;
else
throw RegistryException(L"Unknown root key " + rootPart);
return pathPart;
}
+69
View File
@@ -0,0 +1,69 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2012 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.
*/
#pragma once
#include <string>
#include <vector>
#include <stdexcept>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define APP_REGPATH L"HKEY_LOCAL_MACHINE\\SOFTWARE\\EqualizerAPO"
class RegistryHelper
{
public:
static std::wstring readValue(std::wstring key, std::wstring valuename);
static unsigned long readDWORDValue(std::wstring key, std::wstring valuename);
static void writeValue(std::wstring key, std::wstring valuename, std::wstring value);
static void writeMultiValue(std::wstring key, std::wstring valuename, std::wstring value);
static void deleteValue(std::wstring key, std::wstring valuename);
static void createKey(std::wstring key);
static void deleteKey(std::wstring key);
static void makeWritable(std::wstring key);
static void takeOwnership(std::wstring key);
static std::vector<std::wstring> enumSubKeys(std::wstring key);
static bool keyExists(std::wstring key);
static bool valueExists(std::wstring key, std::wstring valuename);
static bool keyEmpty(std::wstring key);
static void saveToFile(std::wstring key, std::vector<std::wstring> valuenames, std::wstring filepath);
static std::wstring getGuidString(GUID guid);
static bool isWindowsVersionAtLeast(unsigned major, unsigned minor);
static HKEY openKey(const std::wstring& key, REGSAM samDesired);
private:
static std::wstring splitKey(const std::wstring& key, HKEY* rootKey);
static unsigned long windowsVersion;
};
class RegistryException
{
public:
RegistryException(const std::wstring& message):message(message) {}
std::wstring getMessage()
{
return message;
}
private:
std::wstring message;
};
+146
View File
@@ -0,0 +1,146 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2013 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 <string>
#include <sstream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "StringHelper.h"
using namespace std;
wstring StringHelper::replaceCharacters(const wstring& s, const wstring& chars, const wstring& replacement)
{
wstring result;
result.reserve(s.length());
for(unsigned i=0; i<s.length(); i++)
{
wchar_t c = s[i];
if(chars.find(c) == -1)
result += c;
else
result += replacement;
}
return result;
}
wstring StringHelper::replaceIllegalCharacters(const wstring& filename)
{
return replaceCharacters(filename, L"<>:\"/\\|?*", L"_");
}
wstring StringHelper::toWString(const string& s, unsigned codepage)
{
int length = MultiByteToWideChar(codepage, 0, s.c_str(), -1, NULL, 0);
wchar_t* charBuf = new wchar_t[length];
MultiByteToWideChar(codepage, 0, s.c_str(), -1, charBuf, length);
wstring result = charBuf;
delete charBuf;
return result;
}
wstring StringHelper::toLowerCase(const wstring& s)
{
wchar_t* charBuf = new wchar_t[s.length() + 1];
memcpy(charBuf, s.c_str(), (s.length() + 1) * sizeof(wchar_t));
errno_t err = _wcslwr_s(charBuf, s.length() + 1);
wstring result = charBuf;
delete charBuf;
if(err == 0)
return result;
else
return s;
}
wstring StringHelper::toUpperCase(const wstring& s)
{
wchar_t* charBuf = new wchar_t[s.length() + 1];
memcpy(charBuf, s.c_str(), (s.length() + 1) * sizeof(wchar_t));
errno_t err = _wcsupr_s(charBuf, s.length() + 1);
wstring result = charBuf;
delete charBuf;
if(err == 0)
return result;
else
return s;
}
wstring StringHelper::trim(const wstring& s)
{
int firstNonSpace = -1;
int lastNonSpace = -1;
for(unsigned i=0;i<s.length();i++)
{
wchar_t c = s[i];
if(!iswspace(c))
{
if(firstNonSpace == -1)
firstNonSpace = i;
lastNonSpace = i;
}
}
if(firstNonSpace == -1)
return L"";
else
return s.substr(firstNonSpace, lastNonSpace-firstNonSpace+1);
}
vector<wstring> StringHelper::split(const wstring& s, wchar_t splitChar, bool skipEmpty)
{
vector<wstring> result;
size_t prevPos = 0;
size_t pos = 0;
while((pos = s.find(splitChar, pos)) != wstring::npos)
{
wstring part = s.substr(prevPos, pos-prevPos);
if(part.length() > 0 || !skipEmpty)
result.push_back(part);
prevPos = ++pos;
}
wstring part = s.substr(prevPos);
if(part.length() > 0 || !skipEmpty)
result.push_back(part);
return result;
}
wstring StringHelper::getSystemErrorString(long status)
{
wchar_t* buf;
if(FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, 0, (LPTSTR)&buf, 0, NULL) != 0)
{
wstring result(buf);
LocalFree(buf);
return result;
}
else
return L"";
}
+37
View File
@@ -0,0 +1,37 @@
/*
This file is part of EqualizerAPO, a system-wide equalizer.
Copyright (C) 2013 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.
*/
#pragma once
#include <string>
#include <vector>
class StringHelper
{
public:
// replaces any occurrence of a character from chars in s with the replacement string
static std::wstring replaceCharacters(const std::wstring& s, const std::wstring& chars, const std::wstring& replacement);
static std::wstring replaceIllegalCharacters(const std::wstring& filename);
static std::wstring toWString(const std::string& s, unsigned codepage);
static std::wstring toLowerCase(const std::wstring& s);
static std::wstring toUpperCase(const std::wstring& s);
static std::wstring trim(const std::wstring& s);
static std::vector<std::wstring> split(const std::wstring& s, wchar_t splitChar, bool skipEmpty = true);
static std::wstring getSystemErrorString(long status);
};