Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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;
- ProcInfo : TProcessInformation;
- 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;
- WaitForSingleObject(ProcInfo.hProcess, INFINITE);
- 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;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement