Espero que te sea de utilidad:
Código Delphi
[-]
procedure RunAs(ACommandLine, AUserName, ADomain, APassword: PAnsiChar);
var
hToken: Cardinal;
ProcessInfo: TProcessInformation;
StartupInfo: TStartupInfo;
function SetUserObjectFullAccess(hUserObject: THandle): Boolean;
var
Sd: PSecurity_Descriptor;
Si: Security_Information;
begin
Sd := Ptr(LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH));
InitializeSecurityDescriptor(Sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(Sd, True, nil, False);
Si := DACL_SECURITY_INFORMATION;
Result := SetUserObjectSecurity(hUserObject, Si, Sd);
LocalFree(HLOCAL(Sd));
end;
begin
Win32Check(LogonUser(AUserName, ADomain, APassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken));
try
ImpersonateLoggedOnUser(hToken);
try
SetUserObjectFullAccess(GetThreadDesktop(GetCurrentThreadId));
SetUserObjectFullAccess(GetProcessWindowStation);
ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
StartupInfo.cb := SizeOf(TStartupInfo);
StartupInfo.wShowWindow := SW_SHOW;
if not CreateProcessAsUser(hToken, nil, ACommandLine, nil, nil,
False, CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,
nil, nil, StartupInfo, ProcessInfo) then
RaiseLastOsError;
finally
RevertToSelf;
end;
finally
CloseHandle(hToken);
end;
end;
Saludos!