Files
itgmania212121/stepmania/src/archutils/Win32/RegistryAccess.cpp
T

217 lines
6.0 KiB
C++
Raw Normal View History

2004-04-25 03:47:50 +00:00
#include "global.h"
#include "RegistryAccess.h"
#include "RageLog.h"
#include "RageUtil.h"
2005-12-20 20:43:29 +00:00
#include <windows.h>
2004-04-25 03:47:50 +00:00
/* Given "HKEY_LOCAL_MACHINE\hardware\foo", return "hardware\foo", and place
* the HKEY_LOCAL_MACHINE constant in key. */
static bool GetRegKeyType( const RString &sIn, RString &sOut, HKEY &key )
2004-04-25 03:47:50 +00:00
{
2005-11-23 22:47:47 +00:00
size_t iBackslash = sIn.find( '\\' );
if( iBackslash == sIn.npos )
2004-04-25 03:47:50 +00:00
{
2005-11-23 22:47:47 +00:00
LOG->Warn( "Invalid registry key: \"%s\" ", sIn.c_str() );
2004-04-25 03:47:50 +00:00
return false;
}
RString sType = sIn.substr( 0, iBackslash );
2004-04-25 03:47:50 +00:00
2005-11-23 22:47:47 +00:00
if( !sType.CompareNoCase( "HKEY_CLASSES_ROOT" ) ) key = HKEY_CLASSES_ROOT;
else if( !sType.CompareNoCase( "HKEY_CURRENT_CONFIG" ) ) key = HKEY_CURRENT_CONFIG;
else if( !sType.CompareNoCase( "HKEY_CURRENT_USER" ) ) key = HKEY_CURRENT_USER;
else if( !sType.CompareNoCase( "HKEY_LOCAL_MACHINE" ) ) key = HKEY_LOCAL_MACHINE;
else if( !sType.CompareNoCase( "HKEY_USERS" ) ) key = HKEY_USERS;
2004-04-25 03:47:50 +00:00
else
{
2005-11-23 22:47:47 +00:00
LOG->Warn( "Invalid registry key: \"%s\" ", sIn.c_str() );
2004-04-25 03:47:50 +00:00
return false;
}
2005-11-23 22:47:47 +00:00
sOut = sIn.substr( iBackslash+1 );
2004-04-25 03:47:50 +00:00
return true;
}
/* Given a full key, eg. "HKEY_LOCAL_MACHINE\hardware\foo", open it and return it.
* On error, return NULL. */
enum RegKeyMode { READ, WRITE };
static HKEY OpenRegKey( const RString &sKey, RegKeyMode mode, bool bWarnOnError = true )
2004-04-25 03:47:50 +00:00
{
RString sSubkey;
2005-11-23 22:47:47 +00:00
HKEY hType;
if( !GetRegKeyType(sKey, sSubkey, hType) )
2004-04-25 03:47:50 +00:00
return NULL;
2005-11-23 22:47:47 +00:00
HKEY hRetKey;
LONG retval = RegOpenKeyEx( hType, sSubkey, 0, (mode==READ) ? KEY_READ:KEY_WRITE, &hRetKey );
2004-04-25 03:47:50 +00:00
if ( retval != ERROR_SUCCESS )
{
if( bWarnOnError )
LOG->Warn( werr_ssprintf(retval, "RegOpenKeyEx(%x,%s) error", hType, sSubkey.c_str()) );
2004-04-25 03:47:50 +00:00
return NULL;
}
2005-11-23 22:47:47 +00:00
return hRetKey;
2004-04-25 03:47:50 +00:00
}
bool RegistryAccess::GetRegValue( const RString &sKey, const RString &sName, RString &sVal )
2004-04-25 03:47:50 +00:00
{
HKEY hKey = OpenRegKey( sKey, READ );
2005-11-23 22:47:47 +00:00
if( hKey == NULL )
2004-04-25 03:47:50 +00:00
return false;
char sBuffer[MAX_PATH];
2005-11-23 22:47:47 +00:00
DWORD iSize = sizeof(sBuffer);
DWORD iType;
LONG iRet = RegQueryValueEx( hKey, sName, NULL, &iType, (LPBYTE)sBuffer, &iSize );
RegCloseKey( hKey );
if( iRet != ERROR_SUCCESS )
2004-04-25 03:47:50 +00:00
return false;
/* Actually, CStrings are 8-bit clean, so we can accept any type of data. Remove
* this if that becomes useful. */
2005-11-23 22:47:47 +00:00
if( iType != REG_SZ && iType != REG_MULTI_SZ && iType != REG_EXPAND_SZ && iType != REG_BINARY )
2004-04-25 03:47:50 +00:00
return false; /* type mismatch */
2005-11-23 22:47:47 +00:00
if( iSize && (iType == REG_SZ || iType == REG_MULTI_SZ || iType == REG_EXPAND_SZ) )
--iSize; /* remove nul terminator */
2004-04-25 03:47:50 +00:00
sVal = RString( sBuffer, iSize );
2004-04-25 03:47:50 +00:00
return true;
}
bool RegistryAccess::GetRegValue( const RString &sKey, const RString &sName, int &iVal, bool bWarnOnError )
2004-04-25 03:47:50 +00:00
{
HKEY hKey = OpenRegKey( sKey, READ, bWarnOnError );
2005-11-23 22:47:47 +00:00
if( hKey == NULL )
2004-04-25 03:47:50 +00:00
return false;
2005-11-23 22:47:47 +00:00
DWORD iValue;
DWORD iSize = sizeof(iValue);
DWORD iType;
LONG iRet = RegQueryValueEx( hKey, sName, NULL, &iType, (LPBYTE) &iValue, &iSize );
RegCloseKey( hKey );
if( iRet != ERROR_SUCCESS )
2004-04-25 03:47:50 +00:00
return false;
2005-11-23 22:47:47 +00:00
if( iType != REG_DWORD )
2004-04-25 03:47:50 +00:00
return false; /* type mismatch */
2005-11-23 22:47:47 +00:00
iVal = iValue;
2004-04-25 03:47:50 +00:00
return true;
}
bool RegistryAccess::GetRegValue( const RString &sKey, const RString &sName, bool &bVal )
{
int iVal;
bool b = GetRegValue( sKey, sName, iVal );
bVal = !!iVal;
return b;
}
bool RegistryAccess::GetRegSubKeys( const RString &sKey, vector<RString> &lst, const RString &regex, bool bReturnPathToo )
2004-04-25 03:47:50 +00:00
{
HKEY hKey = OpenRegKey( sKey, READ );
2005-11-23 22:47:47 +00:00
if( hKey == NULL )
2004-04-25 03:47:50 +00:00
return false;
Regex re(regex);
bool bError = false;
for( int index = 0; ; ++index )
{
FILETIME ft;
char szBuffer[MAX_PATH];
2005-11-23 22:47:47 +00:00
DWORD iSize = sizeof(szBuffer);
LONG iRet = RegEnumKeyEx( hKey, index, szBuffer, &iSize, NULL, NULL, NULL, &ft);
if( iRet == ERROR_NO_MORE_ITEMS )
2004-04-25 03:47:50 +00:00
break;
2005-11-23 22:47:47 +00:00
if( iRet != ERROR_SUCCESS )
2004-04-25 03:47:50 +00:00
{
2005-11-23 22:47:47 +00:00
LOG->Warn( werr_ssprintf(iRet, "GetRegSubKeys(%p,%i) error", hKey, index) );
2004-04-25 03:47:50 +00:00
bError = true;
break;
}
RString sStr( szBuffer, iSize );
2004-04-25 03:47:50 +00:00
2005-11-23 22:47:47 +00:00
if( re.Compare(sStr) )
2004-04-25 03:47:50 +00:00
{
if( bReturnPathToo )
2005-11-23 22:47:47 +00:00
sStr = sKey + "\\" + sStr;
lst.push_back( sStr );
2004-04-25 03:47:50 +00:00
}
}
2005-11-23 22:47:47 +00:00
RegCloseKey( hKey );
2004-04-25 03:47:50 +00:00
return !bError;
}
2004-05-15 20:13:19 +00:00
bool RegistryAccess::SetRegValue( const RString &sKey, const RString &sName, const RString &sVal )
{
HKEY hKey = OpenRegKey( sKey, WRITE );
if( hKey == NULL )
return false;
bool bSuccess = true;
TCHAR sz[255];
2005-12-21 07:50:14 +00:00
if( sVal.size() > 254 )
return false;
strcpy( sz, sVal.c_str() );
LONG lResult = ::RegSetValueEx(hKey, LPCTSTR(sName), 0, REG_SZ, (LPBYTE)sz, strlen(sz) + 1);
if( lResult != ERROR_SUCCESS )
bSuccess = false;
::RegCloseKey(hKey);
return bSuccess;
}
bool RegistryAccess::SetRegValue( const RString &sKey, const RString &sName, bool bVal )
{
HKEY hKey = OpenRegKey( sKey, WRITE );
if( hKey == NULL )
return false;
bool bSuccess = true;
if (::RegSetValueEx(hKey, LPCTSTR(sName), 0,
REG_BINARY, (LPBYTE)&bVal, sizeof(bVal))
!= ERROR_SUCCESS)
bSuccess = false;
::RegCloseKey(hKey);
return bSuccess;
}
2004-05-15 20:13:19 +00:00
/*
* (c) 2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/