Ver Mensaje Individual
  #14  
Antiguo 12-09-2011
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Sólo por curiosidad, ayer estuve probando una función que encontré buscando en la web, para obtener el dominio y nombre.
Pero aunque obtengo el dominio y nombre del usuario, me muestra cadena vacía cuando el nombre se trata de usuario es 'SYSTEM' o 'SERVICIO LOCAL'.

Agrego la prueba que hice, por si alguien sabe el por qué no muestra esos datos.
Código Delphi [-]
...
uses TlHelp32;

type
  TProcessEntry32Array = array of TProcessEntry32;
  
  PTokenUser = ^TTokenUser;
  TTokenUser = packed record
    User: SID_AND_ATTRIBUTES;
  end;

function GetProcess: TProcessEntry32Array;
var
  Found : Boolean;
  SnapHandle : THandle;
  pr32 : TProcessEntry32;
begin
  SnapHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  pr32.dwSize := Sizeof(ProcessEntry32);
  Found := Process32First(SnapHandle, pr32);
  while Found do
  begin
    SetLength(Result, Length(Result)+1);
    Result[Length(Result)-1] := pr32;
    Found := Process32Next(SnapHandle, pr32);
  end;
  CloseHandle(SnapHandle);
end;

{ Esta es la función que encontré }
function GetProcessUserName(ProcessID: Cardinal; out DomainName, UserName: string): Boolean;
var
  ProcHandle, ProcToken: THandle;
  InfoSize, UserNameSize, DomainNameSize: Cardinal;
  User: PTokenUser;
  Use: SID_NAME_USE;
  DomainNameArray, UserNameArray: array[0..255] of Char;
begin
  Result := False;
  DomainName := '';
  UserName := '';
  ProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
  if ProcHandle = 0 then
    Exit;
  try
    if not OpenProcessToken(ProcHandle, TOKEN_QUERY, ProcToken) then
      Exit; 
    try
      GetTokenInformation(ProcToken, TokenUser, nil, 0, InfoSize);
      User := AllocMem(InfoSize * 2); 
      try
        if GetTokenInformation(ProcToken, TokenUser, User, InfoSize * 2, InfoSize) then
        begin 
          DomainNameSize := SizeOf(DomainNameArray);
          UserNameSize := SizeOf(UserNameArray);
          Result := LookupAccountSid(nil, User^.User.Sid, UserNameArray,
            UserNameSize, DomainNameArray, DomainNameSize, Use);
          if Result then
          begin 
            SetString(DomainName, DomainNameArray, StrLen(DomainNameArray));
            SetString(UserName, UserNameArray, StrLen(UserNameArray));
          end; 
        end; 
      finally
        FreeMem(User);
      end; 
    finally
      CloseHandle(ProcToken);
    end;
  finally
    CloseHandle(ProcHandle);
  end; 
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  ProcList: TProcessEntry32Array;
  Un,Dn: string; // nombre,dominio
begin
  ProcList := GetProcess;
  ListBox1.TabWidth:= 100;
  for i:=0 to Length(ProcList)-1 do
  begin
    GetProcessUserName(ProcList[i].th32ParentProcessID, Un, Dn);
    ListBox1.Items.Add(string(ProcList[i].szExeFile)+
       Chr(9)+ Un + Chr(9) + Dn);
  end;
end;
Algo se me esta escapando y no sé que es...

Un saludo.

Edito: La aplicación consta de un TButton y un TListBox solamente.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....

Última edición por ecfisa fecha: 12-09-2011 a las 17:48:09. Razón: Agregar aclaración
Responder Con Cita