刚刚翻译了一个,试试吧
// ntdll!NtQueryInformationProcess (NT specific!)
//
// The function copies the process information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQueryInformationProcess(
// IN HANDLE ProcessHandle, // handle to process
// IN PROCESSINFOCLASS InformationClass, // information type
// OUT PVOID ProcessInformation, // pointer to buffer
// IN ULONG ProcessInformationLength, // buffer size in bytes
// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
// // variable that receives
// // the number of bytes
// // written to the buffer
// );
function GetParentProcessID(dwId: DWORD): DWORD;
const
ProcessBasicInformation = 0;
type
TPROCESS_BASIC_INFORMATION = packed record
ExitStatus : DWORD;
PebBaseAddress : DWORD;
AffinityMask : DWORD;
BasePriority : DWORD;
UniqueProcessId : ULONG;
InheritedFromUniqueProcessId: ULONG;
end;
TNtQueryInformationProcess = function(
ProcessHandle: THandle;
ProcessInformationClass: Integer;
ProcessInformation: Pointer;
ProcessInformationLength: ULong;
ReturnLength: PULong): Integer; stdcall;
var
NtQueryInformationProcess :TNtQueryInformationProcess;
status : LONGINT;
hProcess : THandle;
pbi : TPROCESS_BASIC_INFORMATION;
begin
Result := DWORD(-1);
NtQueryInformationProcess := GetProcAddress(
GetModuleHandle('ntdll'),
'NtQueryInformationProcess'
);
if @NtQueryInformationProcess = nil then Exit;
// Get process handle
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwId);
if hProcess <> 0 then
begin
// Retrieve information
status := NtQueryInformationProcess(hProcess,
ProcessBasicInformation,
@pbi,
sizeof(TPROCESS_BASIC_INFORMATION),
nil);
// Copy parent Id on success
if status = 0 then
Result := pbi.InheritedFromUniqueProcessId;
CloseHandle (hProcess);
end;
end;
function GetProcessName(dwid: THandle): String;
var
h : THandle;
iLen : integer;
hMod : HMODULE;
cbNeeded : DWORD;
hProcess : DWORD;
begin
Result := '';
h := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, dwID);
if h > 0 then
begin
if EnumProcessModules(h, @hMod, sizeof(hMod), cbNeeded) then
begin
SetLength(Result, MAX_PATH);
iLen := GetModuleFileNameEx(h, hMod, PChar(Result), MAX_PATH);
if iLen <> 0 then
begin
SetLength(Result, StrLen(PCHAR(Result)));
end;
end;
CloseHandle(h);
end;
end;
procedure TForm9.Button1Click(Sender: TObject);
begin
ShowMessage(GetProcessName(GetParentProcessID(GetCurrentProcessID)));
end;