Improved: Configurator.exe now has experimental support for installing the APO even if the sound device previously did not use any APOs. (shown as "APO can be installed (experimental)")

This commit is contained in:
jthedering committed 2013-02-12 19:25:49 +00:00
1 parent ef61580c19
commit 3f07980abc
11 files changed
+284 -52

No files matched your search

+155
View File
@@ -22,6 +22,7 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ObjBase.h>
#include <aclapi.h>
#include "RegistryHelper.h"
@@ -72,6 +73,48 @@ wstring RegistryHelper::readValue(wstring key, wstring valuename)
return result;
}
unsigned long RegistryHelper::readDWORDValue(wstring key, wstring valuename)
{
unsigned long result;
LSTATUS status;
HKEY keyHandle;
status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while opening registry key HKEY_LOCAL_MACHINE\\" + key + L": " + getSystemErrorString(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 HKEY_LOCAL_MACHINE\\" + key + L"\\" + valuename + L": " + getSystemErrorString(status));
}
if(type != REG_DWORD)
{
RegCloseKey(keyHandle);
throw RegistryException(L"Registry value HKEY_LOCAL_MACHINE\\" + 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 HKEY_LOCAL_MACHINE\\" + key + L"\\" + valuename + L": " + getSystemErrorString(status));
}
result = ((unsigned long*)buf)[0];
delete buf;
return result;
}
void RegistryHelper::writeValue(wstring key, wstring valuename, wstring value)
{
LSTATUS status;
@@ -123,6 +166,118 @@ void RegistryHelper::deleteKey(wstring key)
throw RegistryException(L"Error while deleting registry key HKEY_LOCAL_MACHINE\\" + key + L": " + getSystemErrorString(status));
}
void RegistryHelper::makeWritable(wstring key)
{
LSTATUS status;
HKEY keyHandle;
status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key.c_str(), 0, READ_CONTROL | WRITE_DAC | KEY_WOW64_64KEY, &keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while opening registry key HKEY_LOCAL_MACHINE\\" + key + L": " + getSystemErrorString(status));
DWORD descriptorSize = 0;
RegGetKeySecurity(keyHandle, DACL_SECURITY_INFORMATION, NULL, &descriptorSize);
PSECURITY_DESCRIPTOR oldSd = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, descriptorSize);
status = RegGetKeySecurity(keyHandle, DACL_SECURITY_INFORMATION, oldSd, &descriptorSize);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while getting security information for registry key HKEY_LOCAL_MACHINE\\" + key + L": " + 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 HKEY_LOCAL_MACHINE\\" + key + L": " + 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");
LSTATUS status;
HKEY keyHandle;
status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key.c_str(), 0, WRITE_OWNER | KEY_WOW64_64KEY, &keyHandle);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while opening registry key HKEY_LOCAL_MACHINE\\" + key + L": " + getSystemErrorString(status));
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");
status = RegSetKeySecurity(keyHandle, OWNER_SECURITY_INFORMATION, sd);
if(status != ERROR_SUCCESS)
throw RegistryException(L"Error while setting security information for registry key HKEY_LOCAL_MACHINE\\" + key + L": " + 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;