Einzelnen Beitrag anzeigen

Stephan

Registriert seit: 28. Jun 2002
Ort: Tuttlingen
245 Beiträge
 
Delphi 2007 Professional
 
#3

Re: TExec.exec mit Parameter?

  Alt 27. Nov 2007, 13:49
Argh, ja natürlich. Hatte vergessen, dass das garkeine Standardklasse ist. Ich hab hier mal was rauskopiert:

Delphi-Quellcode:
function TExec.RunAs(FileName:String;User,Domain,Password:String):Boolean;
begin
  if StartType<>0 then Result:=False else
  Result:=StartClientProcess(PChar(User),PChar(Domain),PChar(Password),PChar(FileName),fErr);
end;

function TExec.StartClientProcess(lpszUsername, lpszDomain, lpszPassword, lpCommandLine: PChar;var ERR_CODE:integer): Boolean;
var
  si: STARTUPINFO;
  ProfileInfo: TProfileInfo;
begin
  StartType:=1;
  ERR_CODE:=ERR_NONE;
  Result:=False;
  hdesktop:=0;
  hwinst:=0;
  hwinstSave:=0;
  pS:=nil;
  try
  if not LogonUser(lpszUsername,lpszDomain,lpszPassword,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,hToken) then
  begin
    ERR_CODE:=ERR_LOGON;
    Exit;
  end;
  hwinstSave:=GetProcessWindowStation;
  ERR_CODE:=ERR_WINST;
  if hwinstSave = 0 then Exit;
  hwinst := OpenWindowStation('winsta0',False, READ_CONTROL or WRITE_DAC);
  if hwinst = 0 then Exit;
  if not SetProcessWindowStation(hwinst) then Exit;
  ERR_CODE:=ERR_DESKTOP;
  hdesktop := OpenDesktop('default', 0, False, READ_CONTROL or WRITE_DAC or DESKTOP_WRITEOBJECTS or DESKTOP_READOBJECTS);
  if not SetProcessWindowStation(hwinstSave) then Exit;
  if hdesktop = 0 then Exit;
  if not GetLogonSID(hToken, pS) then Exit;
  if not AddAceToWindowStation(hwinst, pS) then Exit;
  if not AddAceToDesktop(hdesktop, pS) then Exit;

  FillChar(ProfileInfo, SizeOf(TProfileInfo),#0);
  ProfileInfo.dwSize := SizeOf(TProfileInfo);
  ProfileInfo.dwFlags := PI_NOUI;
  ProfileInfo.lpUserName :=lpszUsername;
  ERR_CODE:=ERR_LOADPROFILE;
  if not LoadUserProfile(hToken, ProfileInfo) then Exit;
  ERR_CODE:=ERR_IMPERSONATE;
  if not ImpersonateLoggedOnUser(hToken) then Exit;

  ZeroMemory(@si, SizeOf(STARTUPINFO));
  si.cb := SizeOf(STARTUPINFO);
  si.lpDesktop := PChar('winsta0\default');
  ERR_CODE:=ERR_CREATEPROCESS;
  Result := CreateProcessAsUser(hToken,lpCommandLine,nil, nil, nil, False,
      NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, nil, PAnsiChar(ExtractFileDir(lpCommandLine)), si, pi);
  if not Result then ERR_CODE:=ERR_CREATEPROCESS else ERR_CODE:=ERR_NONE;
  RevertToSelf();

  finally

  end;
end;
interessant dürfte die Zeile sein:

Delphi-Quellcode:
  Result := CreateProcessAsUser(hToken,lpCommandLine,nil, nil, nil, False,
      NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, nil, PAnsiChar(ExtractFileDir(lpCommandLine)), si, pi);
http://msdn2.microsoft.com/en-us/library/ms682429.aspx
Der Dritte Parameter sollte es sein. Ich probiers und melde mich dann wieder.
viele Grüße

Stephan
  Mit Zitat antworten Zitat