unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons;
type
TForm1 =
class(TForm)
Button1: TButton;
E1: TLabeledEdit;
E2: TLabeledEdit;
E3: TLabeledEdit;
E4: TLabeledEdit;
SpeedButton1: TSpeedButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
LOGON_WITH_PROFILE = $00000001;
LOGON_NETCREDENTIALS_ONLY = $00000002;
var
Form1: TForm1;
implementation
{$R *.dfm}
function CreateProcessWithLogonW
(
lpUsername,
// user"s name
lpDomain,
// user"s domain
lpPassword: PWideChar;
// user"s password
dwLogonFlags: DWORD;
// logon option
lpApplicationName: PWideChar;
// executable module name
lpCommandLine: PWideChar;
// command-line string
dwCreationFlags: DWORD;
// creation flags
lpEnvironment: Pointer;
// new environment block
lpCurrentDirectory: PWideChar;
// current directory name
const lpStartupInfo: TStartupInfo;
// startup information
var lpProcessInformation: TProcessInformation
//process inf
): Bool;
stdcall;
external "advapi32.dll";
procedure TForm1.Button1Click(Sender: TObject);
var
UserName,
UserDomain,
Password,
CmdLine:Pwidechar;
si: TStartupInfo;
ProcInfo: TProcessInformation;
begin
FillChar(si,sizeof(si), 0);
si.cb:=sizeof(si);
GetMem(UserName, 512);
GetMem(UserDomain, 512);
GetMem(Password, 512);
GetMem(CmdLine, 512);
try
UserName:=StringToWideChar(E2.Text,UserName,512);
UserDomain:=StringToWideChar(E4.Text,UserDomain,512);
Password:=StringToWideChar(E3.Text,Password,512);
cmdline:=StringToWideChar(E1.Text,cmdline,512);
if not CreateProcessWithLogonW(
UserName,
UserDomain,
Password,
LOGON_WITH_PROFILE,
nil,
CmdLine,
CREATE_DEFAULT_ERROR_MODE,
//CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,
nil,
nil, si, ProcInfo)
then
begin
MessageDlg(SysErrorMessage(GetLastError)+#10#13+inttostr(GetLastError), mtError, [mbOK], 0);
end else
begin
WaitForSingleObject(procinfo.hProcess, INFINITE);
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
ShowMessage("Success !!!");
end;
finally
FreeMem(UserName, 512);
FreeMem(UserDomain, 512);
FreeMem(Password, 512);
FreeMem(CmdLine, 512);
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute
then
begin
E1.Text:=OpenDialog1.FileName;
end;
end;
end.