Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.144.10.130] |
|
Сообщ.
#1
,
|
|
|
Как узнать SID(PSid) процесса имея PID процесса...
|
Сообщ.
#2
,
|
|
|
Примерно так:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; PTokenUser = ^TTokenUser; TTokenUser = record User: array[0..0] of TSIDAndAttributes; end; procedure ConvertSidToStringSid(SID: PSID; var StringSid: LPSTR); stdcall; external advapi32 name 'ConvertSidToStringSidA'; var Form1: TForm1; implementation {$R *.dfm} function GetCurrentUserSID: String; var TokenHandle: THandle; TokenInformationClass: TTokenInformationClass; TokenInformation: PTokenUser; ReturnLength: DWORD; StringSid: LPSTR; begin Result := ''; if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle) then try TokenInformationClass := TokenUser; GetTokenInformation(TokenHandle, TokenInformationClass, nil, 0, ReturnLength); if GetLastError = ERROR_INSUFFICIENT_BUFFER then begin TokenInformation := GetMemory(ReturnLength); if TokenInformation <> nil then try if GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, ReturnLength, ReturnLength) then begin ConvertSidToStringSid(TokenInformation^.User[0].Sid, StringSid); Result := StringSid; end; finally FreeMemory(TokenInformation); end; end; finally CloseHandle(TokenHandle); end; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetCurrentUserSID); end; end. Добавлено В данном примере GetCurrentProcess можно заменить (если имеешь PID) на hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, True, PID); if hProcess <> 0 then try // теперь заменяй GetCurrentProcess значением из hProcess finally CloseHandle(hProcess); end; |
Сообщ.
#3
,
|
|
|
Задача найти имя пользователя по пиду.
Делаю всё вроде правильно а не получается - какието каракули выводит.... Может кто поможет исправить... function GetUserName(PID:DWord):string; // This procedure for WinXP type PTokenUser = ^TTokenUser; TTokenUser = record User:array[0..0] of TSIDAndAttributes; end; var hProcess, TokenHandle: THandle; TokenInformationClass: TTokenInformationClass; TokenInformation: PTokenUser; ReturnLength: DWORD; AcctName: PChar; DomainName:PChar; dwAcctName:Cardinal; dwDomainName:DWORD; eUse: SID_NAME_USE; begin Result := ''; hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, True, PID); if hProcess <> 0 then try if OpenProcessToken(hProcess, TOKEN_QUERY, TokenHandle) then try TokenInformationClass := TokenUser; GetTokenInformation(TokenHandle, TokenInformationClass, nil, 0, ReturnLength); if GetLastError = ERROR_INSUFFICIENT_BUFFER then begin TokenInformation := GetMemory(ReturnLength); if TokenInformation <> nil then try if GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation, ReturnLength, ReturnLength) then begin if LookupAccountSid(nil,TokenInformation^.User[0].Sid,AcctName,dwAcctName,DomainName,dwDomainName,eUse) then begin AcctName := GetMemory(dwAcctName+1); DomainName := GetMemory(dwDomainName+1); if LookupAccountSid(nil,TokenInformation^.User[0].Sid,AcctName,dwAcctName,DomainName,dwDomainName,eUse) then Result := StrPas(AcctName)+' '+StrPas(DomainName); FreeMemory(AcctName); FreeMemory(DomainName); end; end; finally FreeMemory(TokenInformation); end; end; finally CloseHandle(TokenHandle); end; finally CloseHandle(hProcess); end; end; |
Сообщ.
#4
,
|
|
|
AcctName: array [0..MAXCHAR - 1] of Char; DomainName:array [0..MAXCHAR - 1] of Char; ... if LookupAccountSid(nil,TokenInformation^.User[0].Sid, AcctName, dwAcctName, DomainName, dwDomainName, eUse) then begin eUse := 0; //AcctName := GetMemory(dwAcctName + 1); dwAcctName := MAXCHAR; //DomainName := GetMemory(dwDomainName + 1); dwDomainName := MAXCHAR; if LookupAccountSid(nil,TokenInformation^.User[0].Sid, AcctName, dwAcctName, DomainName, dwDomainName, eUse) then Result := StrPas(AcctName)+' '+StrPas(DomainName) else RaiseLastOSError; |