Newsgroup: it.comp.lang.delphi
Titolo: Esecuzione comando con privilegi amministrativi

Come posso fare in delphi per lanciare un'applicazione (un
file di setup che mi server per aggiornare dei programmi)
con privilegi amministrativi conoscendo ID e PSW?
Grazie

Adrian Sassatelli wrote:

Buono studio:
function GetUserObjectName(hUserObject: THandle; var Name: string):
boolean;
var dw: DWord;
begin
  Name := EmptyStr;
  GetUserObjectInformation(hUserObject, UOI_NAME, PChar(Name), 0, dw);
  SetLength(Name, dw + 1);
  Result := GetUserObjectInformation(hUserObject, UOI_NAME,
PChar(Name), dw, dw);
  if Result then SetLength(Name, dw - 1)
  else Name := EmptyStr;
end;
function SetUserObjectAllAccess(hUserObject: THANDLE): boolean;
var
  pSD: PSecurity_Descriptor;
  si: Security_Information; { dword }
begin
  (* Initialize a security descriptor. *)
  pSD := PSecurity_Descriptor(
    LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH));
  if pSD = nil then
  begin
    Result := FALSE; exit;
  end;
  if not InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION) then
  begin
    LocalFree(HLOCAL(pSD));
    Result := FALSE; exit;
  end;
  (* Add a NULL disc. ACL to the security descriptor. *)
  if not SetSecurityDescriptorDacl(pSD,
    TRUE, // specifying a disc. ACL
    PACL(nil),
    FALSE) then // not a default disc. ACL
  begin
    LocalFree(HLOCAL(pSD));
    Result := FALSE; exit;
  end;
  (* Add the security descriptor to the userobject (like a window or a
DDE
     conversation), NOT to a kernelobject (like a process, thread or
event). *)
  si := DACL_SECURITY_INFORMATION;
  Result := SetUserObjectSecurity(hUserObject, si, pSD);
  LocalFree(HLOCAL(pSD));
end;
function GetPrivilegeDisplayName(const PrivilegeName: string): string;
{ PrivilegeName is of string type 'SE_'* }
var dw, li: DWord;
begin
  Result := EmptyStr; dw := 0; li := 0; { li:=
dword(MAKELANGID(LANG_DEFAULT, LANG_USER)); }
  if not LookupPrivilegeDisplayName(nil, PChar(PrivilegeName),
PChar(Result), dw, li)
    then dw := 256;
  SetLength(Result, dw + 1);
  if LookupPrivilegeDisplayName(nil, PChar(PrivilegeName),
PChar(Result), dw, li)
    then SetLength(Result, StrLen(PChar(Result)))
  else Result := EmptyStr;
end;
function ExecAndWait(const Filename, Params, Username, Domain,
Password: string; WindowState: word): boolean; overload;
var
  startUpInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CurrentDir: string;
  ApplicationName: string;
  CmdLine: string;
  hUserToken: THANDLE;
  hWindowStation: HWINSTA;
  hDesktop: HDESK;
  WinStaName, DeskTopName: string;
const
  SE_TCB_NAME = 'SeTcbPrivilege';
  DEFWINSTATION: string = 'WinSta0';
  DEFDESKTOP: string = 'Default';
  WHITESPACE: string = ' ' {SPACE} + chr(9) {TAB} + chr(10) {LF};
  DOMUSERSEP: string = '\';
begin
  { Enclose filename in quotes to take care of long filenames with
spaces. }
  CmdLine := '"' + Filename + '" ' + Params;
  CurrentDir := ExtractFilePath(Filename);
  ApplicationName := Filename;
  Result := LogonUser(PChar(Username), PChar(Domain), PChar(Password),
    LOGON32_LOGON_INTERACTIVE,
    LOGON32_PROVIDER_DEFAULT, hUserToken);
  if not Result then
  begin
{$IFDEF MY_DEBUG}
    SaveToFile(DebugFile, 'Error: ' + SysErrorMessage(GetLastError) +
#13#10);
{$ENDIF}
    case GetLastError of
      ERROR_PRIVILEGE_NOT_HELD:
        begin
{$IFDEF MY_DEBUG}
          AppendToFile(DebugFile, 'Error: ERROR_PRIVILEGE_NOT_HELD' +
#13#10);
{$ENDIF}
        end;
      ERROR_LOGON_FAILURE:
        begin
{$IFDEF MY_DEBUG}
          AppendToFile(DebugFile, 'Error: ERROR_LOGON_FAILURE' +
#13#10);
{$ENDIF}
        end;
      ERROR_ACCESS_DENIED:
        begin
{$IFDEF MY_DEBUG}
          AppendToFile(DebugFile, 'Error: ERROR_ACCESS_DENIED' +
#13#10);
{$ENDIF}
        end;
    end;
  end;
{$IFDEF MY_DEBUG}
  AppendToFile(DebugFile, 'LogonUser: ' + BoolToStr(Result, True) +
#13#10);
{$ENDIF}
  hWindowStation := GetProcessWindowStation;
  if not GetUserObjectName(hWindowStation, WinStaName) then
    WinStaName := DEFWINSTATION;
{$IFDEF MY_DEBUG}
  Result := SetUserObjectAllAccess(hWindowStation);
  AppendToFile(DebugFile, 'SetUserObjectAllAccess: ' +
BoolToStr(Result, True) + #13#10);
{$ELSE}
  SetUserObjectAllAccess(hWindowStation);
{$ENDIF}
  hDesktop := GetThreadDesktop(GetCurrentThreadId);
  if not GetUserObjectName(hDesktop, DeskTopName) then
    DeskTopName := DEFDESKTOP;
{$IFDEF MY_DEBUG}
  Result := SetUserObjectAllAccess(hDesktop);
  AppendToFile(DebugFile, 'SetUserObjectAllAccess: ' +
BoolToStr(Result, True) + #13#10);
{$ELSE}
  SetUserObjectAllAccess(hDesktop);
{$ENDIF}
  FillChar(startUpInfo, sizeof(startUpInfo), 0);
  with startUpInfo do
  begin
    cb := sizeof(startUpInfo);
    lpTitle := PChar('SignCode');
    lpDesktop := PChar(WinStaName + '\' + DeskTopName);
    wShowWindow := WindowState;
  end;
  Result := CreateProcessAsUser(hUserToken,
    nil, PChar(CmdLine), nil, nil, FALSE {no inherit handles},
    CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,
    nil, nil, startUpInfo, procInfo);
{$IFDEF MY_DEBUG}
  AppendToFile(DebugFile, 'CreateProcessAsUser: ' + BoolToStr(Result,
True) + #13#10);
  AppendToFile(DebugFile, 'CmdLine: ' + CmdLine + #13#10);
{$ENDIF}
  { Wait for it to finish. }
  if Result then
  begin
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
     { Clean up the handles. }
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread);
  end;
end;
-- 
I am what I am, I do what I can
Cerchi un programmatore PHP/MySQL? 
http://url.repository.biz/go/1Y
L'uomo, conscio di sbagliare, persevera.

 con privilegi amministrativi conoscendo ID e PSW?
Ho risolto! Ho usato l'API CreateProcessWithLogonW.
Grazie mille a tutti