F
FritzH
Hi folks,
This is probably off-topic for this group, but please feel free to
point me in the right direction.
I have a slight problem here - I'm trying to use CreateProcessAsUser
to run a process as a different user. However, it only runs it in that
user's security context (starts up fine, shows on the right desktop,
etc). I really need it to run as the target user, as the process being
launched depends on ActiveDirectory.
Can anyone give me a pointer? (pun not indended)
The code looks like this (error handling stripped out - all calls
succeed):
HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, FALSE,
exProcId);
if (!hproc) ...
// Now get the process token
HANDLE orig_usertoken = 0;
if (!OpenProcessToken(hproc, TOKEN_ALL_ACCESS,
&orig_usertoken)) ...
CloseHandle(hproc);
// Create a duplicate of the user token for the
CreateProcessAsUser
call
HANDLE usertoken = NULL;
DuplicateTokenEx(orig_usertoken, MAXIMUM_ALLOWED, NULL,
SecurityDelegation, TokenPrimary, &usertoken);
if (!usertoken) ...
// if (!ImpersonateLoggedOnUser(usertoken)) ...
// Create a new environment block for the process
void* envblock;
if (!CreateEnvironmentBlock(&envblock, usertoken, FALSE)) ...
if (!CreateProcessAsUser(usertoken, NULL,
wcscmdline, // Has to be a writeable string
NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
envblock,
path.c_str(),
&startupInfo, &procinf)) ...
// if (!RevertToSelf()) ...
// Clean up after ourselves
//
**********************************************************************
//
//
CloseHandle(orig_usertoken);
CloseHandle(usertoken);
DestroyEnvironmentBlock(envblock);
cheers!
This is probably off-topic for this group, but please feel free to
point me in the right direction.
I have a slight problem here - I'm trying to use CreateProcessAsUser
to run a process as a different user. However, it only runs it in that
user's security context (starts up fine, shows on the right desktop,
etc). I really need it to run as the target user, as the process being
launched depends on ActiveDirectory.
Can anyone give me a pointer? (pun not indended)
The code looks like this (error handling stripped out - all calls
succeed):
HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, FALSE,
exProcId);
if (!hproc) ...
// Now get the process token
HANDLE orig_usertoken = 0;
if (!OpenProcessToken(hproc, TOKEN_ALL_ACCESS,
&orig_usertoken)) ...
CloseHandle(hproc);
// Create a duplicate of the user token for the
CreateProcessAsUser
call
HANDLE usertoken = NULL;
DuplicateTokenEx(orig_usertoken, MAXIMUM_ALLOWED, NULL,
SecurityDelegation, TokenPrimary, &usertoken);
if (!usertoken) ...
// if (!ImpersonateLoggedOnUser(usertoken)) ...
// Create a new environment block for the process
void* envblock;
if (!CreateEnvironmentBlock(&envblock, usertoken, FALSE)) ...
if (!CreateProcessAsUser(usertoken, NULL,
wcscmdline, // Has to be a writeable string
NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
envblock,
path.c_str(),
&startupInfo, &procinf)) ...
// if (!RevertToSelf()) ...
// Clean up after ourselves
//
**********************************************************************
//
//
CloseHandle(orig_usertoken);
CloseHandle(usertoken);
DestroyEnvironmentBlock(envblock);
cheers!