Run as user
1.example
RunAsUser - Run a Program Under Specific User Account - www.dennisbabkin.com
2.
LogonUserW function
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-logonuserw
3.
function CreateProcessWithLogon(lpUsername: PWideChar;
lpDomain: PWideChar;
lpPassword: PWideChar;
dwLogonFlags: DWORD;
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
var lpStartupInfo: TStartupInfo;
var lpProcessInfo: TProcessInformation):
BOOL; stdcall;
external 'advapi32' name
'CreateProcessWithLogonW';
function CreateEnvironmentBlock(var lpEnvironment: Pointer;
hToken: THandle;
bInherit: BOOL): BOOL; stdcall; external
'userenv';
function DestroyEnvironmentBlock(pEnvironment: Pointer): BOOL; stdcall;
external 'userenv';
const
LOGON_WITH_PROFILE = $00000001;
{------------
Emulate the RunAs function
--------------}
function RunAs(User, Password, Command: String): Integer;
var dwSize: DWORD;
hToken: THandle;
lpvEnv: Pointer;
pi: TProcessInformation;
si: TStartupInfo;
szPath: Array [0..MAX_PATH] of WideChar;
begin
ZeroMemory(@szPath, SizeOf(szPath));
ZeroMemory(@pi, SizeOf(pi));
ZeroMemory(@si, SizeOf(si));
si.cb:=SizeOf(TStartupInfo);
if LogonUser(PChar(User), nil, PChar(Password), LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, hToken) then
begin
if CreateEnvironmentBlock(lpvEnv, hToken, True) then
begin
dwSize:=SizeOf(szPath) div SizeOf(WCHAR);
if (GetCurrentDirectoryW(dwSize, @szPath) > 0) then
begin
if (CreateProcessWithLogon(PWideChar(WideString(User)), nil,
PWideChar(WideString(Password)),
LOGON_WITH_PROFILE, nil, PWideChar(WideString(Command)),
CREATE_UNICODE_ENVIRONMENT,
lpvEnv, szPath, si, pi)) then
begin
result:=ERROR_SUCCESS;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end
else
result:=GetLastError;
end
else
result:=GetLastError;
DestroyEnvironmentBlock(lpvEnv);
end
else
result:=GetLastError;
CloseHandle(hToken);
end
else
result:=GetLastError;
end;
4.https://stackoverflow.com/questions/21330060/delphi-run-as-different-user
The API function that you are looking for is CredUIPromptForCredentials
. This returns a username and password that you can pass to CreateProcessWithLogonW
.
LogonUserW function
4.
function CreateProcessWithLogonW(
lpUsername, // user's name
lpDomain, // user's domain
lpPassword:PWideChar; // user's password
dwLogonFlags:dword; // logon option
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
const lpStartupInfo: tSTARTUPINFO;
var lpProcessInformation: TProcessInformation
): BOOL; stdcall;external 'advapi32.dll';
{
BOOL CreateProcessWithLogonW(
LPCWSTR lpUsername, // user's name
LPCWSTR lpDomain, // user's domain
LPCWSTR lpPassword, // user's password
DWORD dwLogonFlags, // logon option
LPCWSTR lpApplicationName, // executable module name
LPWSTR lpCommandLine, // command-line string
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCWSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFOW lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInfo // process information
);
procedure TForm1.FormCreate(Sender: TObject);
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar (StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOWNORMAL;
if not CreateProcessWithLogonW({'your name'},nil,{'your password'},0,
'notepad.exe', nil,
0, nil, nil,StartupInfo , ProcessInfo) then
RaiseLastWin32Error;
end;