Delphi 获取当前进程的父进程


本文整理自网络,侵删。

 

有时候,我们创建了一个子进程,我们需要知道自己所隶属的父进程的ID。有几个办法,一个是调用微软未公开的 NtQueryInformationProcess 函数,另一个就是下面提供的一个函数,利用了公开的函数,运行需要包含 tlhelp32 单元。


 function GetParentProcessId: Cardinal;
  var
    hSnapshot: THandle;
    AEntry: TProcessEntry32;
  begin
    Result := 0;
    hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if hSnapshot <> INVALID_HANDLE_VALUE then
    begin
      try
        AEntry.dwSize := sizeof(TProcessEntry32);
        if Process32First(hSnapshot, AEntry) then
        begin
          repeat
            if AEntry.th32ProcessID = GetCurrentProcessId then
            begin
              Result := AEntry.th32ParentProcessID;
              break;
            end;
          until not Process32Next(hSnapshot, AEntry);
        end;
      finally
        CloseHandle(hSnapshot);
      end;
    end;
  end;

 

这个函数直接会返回当前的父进程的ID,如果找不到,返回0.

当然,根据上面的原因,你很容易就可以写出枚举所有子进程的函数了,这里就不再缀述了,大家可以根据自己的想法写写看。

相关阅读 >>

Delphi 字符串替换

Delphi第三方控件通用安装方法

Delphi listview用法

Delphi 通过 arp 协议获取局域网内指定 ip 地址的机器的 mac 地址

github上通过星级评估排名前10的最受欢迎的开源Delphi项目

Delphi xe7中stringgrid组件的使用

Delphi urlencode

Delphi richedit根据鼠标位置定位光标的方法

Delphi动态复选框源码

Delphi google text to speech api

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...