Показать сообщение отдельно
  #7  
Старый 24.01.2012, 12:24
Zorkov Igor Zorkov Igor вне форума
Новичок
 
Регистрация: 28.07.2009
Сообщения: 85
Репутация: 50
По умолчанию Имя пользователя процесса, типа NT AUTHORITY\NETWORK SERVICE

В разных ОС для некоторых процессов получить имя пользователя можно только с помощью драйвера

Код:
procedure TForm1.Button1Click(Sender: TObject);
var
  ReturnLength: DWORD;
  peUse: SID_NAME_USE;
  SIDAndAttributes: PSIDAndAttributes;
  Domain, Name: PWideChar;
  ProcessHandle, TokenHandle: THandle;
begin
  // Vista и выше PROCESS_QUERY_LIMITED_INFORMATION = $1000;
  ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, True, StrToInt(Edit1.Text));
  if ProcessHandle <> 0 then
  begin
    try
      if OpenProcessToken(ProcessHandle, TOKEN_QUERY, TokenHandle) then
      begin
        try
          GetTokenInformation(TokenHandle, TokenUser, nil, 0, ReturnLength);
          GetMem(SIDAndAttributes, ReturnLength);
          if SIDAndAttributes <> nil then
          begin
            try
              if GetTokenInformation(TokenHandle, TokenUser, SIDAndAttributes, ReturnLength, ReturnLength) then
              begin
                ReturnLength := MAX_PATH;
                GetMem(Name, ReturnLength);
                GetMem(Domain, ReturnLength);
                try
                  if LookupAccountSidW(nil, SIDAndAttributes.SID, Name, ReturnLength, Domain, ReturnLength, peUse) then
                    ShowMessage(Domain + '/' + Name);
                finally
                  FreeMem(Name);
                  FreeMem(Domain);
                end;
              end;
            finally
              FreeMem(SIDAndAttributes, ReturnLength);
            end;
          end;
        finally
          CloseHandle(TokenHandle);
        end;
      end;
    finally
      CloseHandle(ProcessHandle);
    end;
  end
  else
  begin
    ShowMessage('Невозможно открыть процесс (' + Edit1.Text + ')');
  end;
end;
Ответить с цитированием