Article View: borland.public.cppbuilder.nativeapi
Article #1982System service accessing HKEY_CURRENT_USER
From: Jeff
Date: Wed, 23 May 2007 15:17
Date: Wed, 23 May 2007 15:17
63 lines
2877 bytes
2877 bytes
I've got a system service that needs to access a user's registry value.
I've written a function to impersonate the user and then access the registry
(HKEY_CURRENT_USER), but it always get back a false value (see code below).
Am I not impersonating the user correctly? Is there a better way to access
this Registry value from my service?
HANDLE hToken = NULL;
HANDLE hDupToken = NULL;
bool boolRet;
try
{
try
{
DWORD dwSessionId = WTSGetActiveConsoleSessionId(); //Get the
user's session ID
if (dwSessionId == 0xFFFFFFFF)
return false;
WTSQueryUserToken(dwSessionId, &hToken); //Use the session ID
to get info on their security token
if (hToken == NULL)
return false;
//Get an impersonation of their token
DuplicateToken(hToken, SecurityImpersonation, &hDupToken);
if (hDupToken == NULL)
{
CloseHandle(hToken);
return false;
}
//Impersonate the logged on user
BOOL bRes = ImpersonateLoggedOnUser(hDupToken);
if (bRes)
{
TRegistry *ptr_Reg = new TRegistry;
try
{
ptr_Reg->RootKey = HKEY_CURRENT_USER;
ptr_Reg->OpenKey(USERREG_KEYPATH, false);
boolRet = ptr_Reg->ReadBool(ACCESSREGVAL);
} __finally {
ptr_Reg->CloseKey();
delete ptr_Reg;
}
}
else
boolRet = false;
} __finally {
CloseHandle(hDupToken);
CloseHandle(hToken);
RevertToSelf();
}
} catch (Exception &exception) {
Sysutils::ShowException(&exception, System::ExceptAddr());
}
return boolRet;
Thanks,
Jeff
Technical Details
Message-ID:
<46545b9a@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,896f59c6a0f395d7,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Jeff" <Jeff@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: System service accessing HKEY_CURRENT_USER Date: Wed, 23 May 2007 09:17:56 -0600 Lines: 65 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 NNTP-Posting-Host: 198.68.195.178 Message-ID: <46545b9a@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1179933594 198.68.195.178 (23 May 2007 08:19:54 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail