Как по PID процесса узнать CMDLINE?, то есть командную строку

Материал из DRKB

Как по PID процесса узнать CMDLINE?, то есть командную строку[править | править код]

function GetProcessCmdLine(PID: DWORD): string;
var
  h: THandle;
  pbi: TProcessBacicInformation;
  ret: NTSTATUS;
  r: Cardinal;
  ws: WideString;
begin
  Result := '';
  if pid = 0 then exit;
  h := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, pid);
  if h = 0 then exit;
  try
    ret := NtQueryInformationProcess(h, ProcessBasicInformation, @pbi, SizeOf(pbi), @r);
    if ret = STATUS_SUCCESS then
      if ReadProcessMemory(h, pbi.PebBaseAddress.ProcessParameters.CommandLine.Buffer, PWideChar(ws),
                           pbi.PebBaseAddress.ProcessParameters.CommandLine.Length, r) then
        Result := string(ws);
  finally
    CloseHandle(h)
  end
end;


Author: Krid
Source: Взято из http://forum.sources.ru
ID: 02134