Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Jeff
Wed, 23 May 2007 15:17
System service accessing HKEY_CURRENT_USER
Author: 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
Re: System service accessing HKEY_CURRENT_USER
Author: Remy Lebeau (TeamB)
Date: Wed, 23 May 2007 17:19
Date: Wed, 23 May 2007 17:19
28 lines
1723 bytes
1723 bytes
"Jeff" <Jeff@nospam.com> wrote in message
news:46545b9a@newsgroups.borland.com...
> I've got a system service that needs to access a user's registry
value.
In order to do that, the service must first log in to the user's
account as well as load that user's Registry hive if it ihas not
already been loaded by the OS earlier.
> 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).
You did not load the user's Registry hive. You need to call
RegLoadKey() after impersonating the user. Have a look at the
following article for more details:
Why Do Certain Win32 Technologies Misbehave in Windows NT
Services?
http://www.microsoft.com/msj/0398/service2.aspx
In particular, look at the "Registry Hives" section at the very end of
the article.
Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads