Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Impersonation

$
0
0
Hello.

I am creating a program that is set to run as the LocalSystem (NT AUTHORITY\SYSTEM) account, and it works flawless, but there comes a time where I need to change some user settings, in which I impersonate the current active user and write those changes to the registry using SystemParametersInfo API. While the changes do get written to the registry, they aren't updated until a system reboot is done.
I know there's a way to update those changes without a system reboot because if I were to run the same code in a normal user process, it would work and the changes would be immediate.
Here's my code:

HANDLE htoken1;
WTSQueryUserToken(WTSGetActiveConsoleSessionId(), &htoken1); //to get the current active user token
HANDLE htoken2;
DWORD size = sizeof(htoken2);
int test1 = GetTokenInformation(htoken1, TokenLinkedToken, &htoken2, size, &size); //to convert the user token to an elevated one
CloseHandle(htoken1);
HANDLE htoken3;
int test2 = DuplicateTokenEx(htoken2, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenImpersonation, &htoken3); //to convert the elevated user token to an impersonation token, so it can be used for the actual impersonation
CloseHandle(htoken2);



Everything works until now...
I even tried two ways to impersonate the user, but they both come out the same:

1# method:
ImpersonateLoggedOnUser(htoken3);
CloseHandle(htoken3);



2# method:
SetThreadToken(NULL, htoken3);
CloseHandle(htoken3);



And now comes the actual registry change:

DWORD value = 17
SystemParametersInfo(SPI_SETMOUSESPEED, NULL, (void*)&value, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);



From this point, I check the respective registry section (HKEY_CURRENT_USER\Control Panel\Mouse) for the current user and the changes are there, but the mouse sensitivity isn't updated without a system reboot.

Any ideas?
Thanks very much.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>