I'm looking for a way to elevate the execution privileges of a thread or process without the UAC popup appearing. The user who runs the process is an admin user and I have his username and password available.
I need to do that in order to do some administrative stuff like restarting a service and writing files to system directories. My application is run remotely and there is no interactive user to confirm the UAC dialog. Disabling UAC is not an option.
I've tried juggling LogonUser(), ImpersonateLoggedOnUser(), CreateProcessAsUser() and DuplicateTokenEx() for the better part or two days but couldn't figure out the right combination and if at all this is even possible.
Specifically what I've tried is this:
HANDLE token = 0;
LogonUserA(user, NULL, pass, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, &token);
HANDLE impToken = 0;
DuplicateToken(token, SecurityImpersonation, &impToken);
ImpersonateLoggedOnUser(impToken);
CreateFileA("C:\\windows\\blabla.dll", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
the last call fails with GetLastError()=1346, "Either a required impersonation level was not provided, or the provided impersonation level is invalid."
What am I doing wrong?
Note - this is running on Win2008 R2