CHKEY_USER::CHKEY_USER(const cstring &strProfileKey,
const cstring &strProfilePath):m_hKey(NULL), m_bUnload(false),
m_strProfileKey(strProfileKey)
{
// test privilege for loading hive
if (!_privilege(SE_RESTORE_NAME, true))
return;
// make path to reg file
cstring strRegPath(strProfilePath);
cstring::iterator it(strRegPath.end());
if (_T('\\') != *--it)
{
strRegPath.append(1, _T('\\'));
}
strRegPath.append(_T("ntuser.dat"));
// load key
long lr = RegLoadKey(HKEY_USERS, m_strProfileKey.c_str(),
strRegPath.c_str());
m_bUnload = ERROR_SUCCESS == lr;
if (!m_bUnload)
{
_privilege(SE_RESTORE_NAME, false);
}
if (ERROR_SUCCESS == lr) // open loaded key
{
RegOpenKeyEx(HKEY_USERS, m_strProfileKey.c_str(), 0, KEY_READ, &m_hKey);
}
else if (ERROR_SHARING_VIOLATION == lr) // open current user
{
RegOpenKeyEx(HKEY_CURRENT_USER, NULL, 0, KEY_READ, &m_hKey);
}
}
// from Q168877
bool CHKEY_USER::_privilege(LPTSTR szPrivilege, bool bEnable)
{
HANDLE hToken;
TOKEN_PRIVILEGES tp;
// obtain the processes token
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
// get the luid
if (!LookupPrivilegeValue(NULL, szPrivilege,
&tp.Privileges[0].Luid))
return false;
tp.PrivilegeCount = 1;
if (bEnable)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// enable or disable the privilege
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0,
(PTOKEN_PRIVILEGES)NULL, 0))
return false;
if (!CloseHandle(hToken))
return false;
return true;
}
/Andreas
"Art Komninos" <IUnk...@vtbl.com> wrote in message
news:u13VdsymCHA.2464@TK2MSFTNGP11...