Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run as another user

Status
Not open for further replies.

wuxapian

Programmer
Jun 13, 2001
52
GB
Does anyone know how I can swap user credentials within my code to allow me to do certain tasks as an administrator (could supply login info to the program if necessary)?

TIA
Wux.
 
Two steps are necessary: 1) Logon and 2) "Impersonation". Example:
Code:
HANDLE hToken;
LogonUser(localUser, NULL, localPassword, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, &hToken);
ImpersonateLoggedOnUser(hToken);
CloseHandle(hToken);

Of course, you have to add error handling etc.
 
I forgot: To cancel the impersonation, use RevertToSelf()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top