The content under the HKEY_CURRENT_USER key in the registry belongs to the current user, so the content under the key is different when you log on with different users. However, sometimes we need to read the content under the specified user's key. For example, we have a service running at session0 and system permissions, but what we want to read is the IE Proxy Server setting parameters of the users currently logged on to the local console. Of course, we cannot open the hkey_curent_user key directly from the service, and use our current token to open the HKEY_CURRENT_USER key of the system. Maybe we want to read this key through the console user played by the current thread.
Impersonateloggedonuser (htoken );
Regopenkeyex (HKEY_CURRENT_USER ...);
Reverttoself ();
In fact, this method is not feasible, because although we have obtained the token of the console user and imitated the user, because the user's environment variables and user context are not loaded in the current space, therefore, the regopenkeyex call fails.
Fortunately, Microsoft has prepared loaduserprofile () for us ();
First, we can use
Wtsgetactiveconsolesessionid gets the ID of the current console session. This API is available only on XP/2003 or later. What should I do if it is not available on 2000? 2000 can directly save this step. Because 2000 does not support User Switching, the local console will always run in session0.
Then you can use wtsqueryusertoken to obtain the User Token that the specified session has logged on. This API is still supported by XP/2003. What should I do with 2000? (Can I open the assumer.exe Token ).
Then, loaduserprofile loads the user's environment variables.
The code is pasted below, which may be incomplete. The apis I use are dynamically loaded, because our software may support 98, so we can only load them dynamically, otherwise, it cannot be run. Pay attention to the bold part. The key is here. If you have no idea, let's look at msdn. Microsoft has made it very clear. Pay tribute to the msdn team!
Handle htoken = NULL;
Bool bimpersonated = false;
Profileinfoa cuprofileinfo;
Tchar szusername [max_path];
DWORD dwusernamelen = max_path;
If (getconsoleusertoken (& htoken ))
{
If (impersonateloggedonuser (htoken ))
{
Bimpersonated = true;
// MessageBox (null, "I .. l .. user OK", "", null );
}
GetUserName (szusername, & dwusernamelen );
Memset (& cuprofileinfo, 0, sizeof (cuprofileinfo ));
Cuprofileinfo. dwsize = sizeof (profileinfoa );
Cuprofileinfo. lpusername = szusername;
Cuprofileinfo. dwflags = 1;
If (bimpersonated)
{
Reverttoself ();
Bimpersonated = false;
}
If (pfnloaduserprofile)
{
If (pfnloaduserprofile (htoken, & cuprofileinfo ))
{
Regopenkeyex ((Struct hkey _ *) cuprofileinfo. hprofile, Ieproxy_localuser_key, 0, key_query_value, & regkeyfrom );
// MessageBox (null, "Open SPC key", "", null );
}
Else
{
Regopenkeyex (HKEY_CURRENT_USER, ieproxy_localuser_key, 0, key_query_value, & regkeyfrom );
}
}
// MessageBox (null, "impersonateloggedonuser successful", "atagtctl", null );
}
Else
{
Regopenkeyex (HKEY_CURRENT_USER, ieproxy_localuser_key, 0, key_query_value, & regkeyfrom );
}