gpt4 book ai didi

delphi - 如何从 Windows 任务管理器(“应用程序”选项卡)获取应用程序及其在 Delphi 硬盘上的位置

转载 作者:行者123 更新时间:2023-12-03 15:10:50 27 4
gpt4 key购买 nike

我想获取在 Windows 任务管理器的“应用程序”选项卡(不是“进程”选项卡)中运行和可见的程序列表,并获取它们在 HDD 上的位置?
我需要在 Delphi 中完成。有人可以帮忙吗?

最佳答案

据我所知,任务管理器中的“应用程序”选项卡是一个顶级窗口列表,这些窗口不属于其他窗口,没有父窗口,也不是工具窗口。在我的Process Info ,我有一个名为 AppInfo.pas 的单元,它返回具有此类特征的窗口列表,并且该列表与您在任务管理器中看到的内容相匹配。以下是作为 EnumWindows API 函数的回调函数编写的代码的主要部分:

{$IFDEF DELPHI2007UP}
class function TAppWindowCollection.EnumWinProc(wHandle: HWND; lparam: integer): Bool;
{$ELSE}
function EnumWinProc(wHandle: HWND; lparam: integer): Bool; stdcall;
{$ENDIF}
Const
MAX_TEXT = MAX_PATH;
var
WindowItem : TWindowItem;
strText,strClass : array [0..MAX_TEXT] of char;
IsAppMainWin : Boolean;
begin
//Check if the window is a visible application main window.
IsAppMainWin := IsWindowVisible(wHandle) AND //Visible
(GetWindow(wHandle,GW_OWNER) = 0) AND //Not owned by other windows
(GetParent(wHandle) = 0) AND //Does not have any parent
(GetWindowLong(wHandle,GWL_EXSTYLE) AND WS_EX_TOOLWINDOW = 0); //Not a tool window

if IsAppMainWin then
begin
WindowItem := TAppWindowCollection(lparam).Add;

GetWindowText(wHandle,strText,MAX_TEXT);
GetClassName(wHandle,strClass,MAX_TEXT);

WindowItem.FCaption := strText;
WindowItem.FHandle := wHandle;
WindowItem.FWindowClass := strClass;
GetWindowThreadProcessId(wHandle,WindowItem.FProcessID);
end;

Result := True;
end;

完整源码可以引用AppInfo.pas .

and get their locations on HDD

这些只是窗口。如果你想获取每个项目对应的EXE文件的路径,你应该首先找到拥有该窗口的进程,使用 GetWindowThreadProcessID API函数。这就是我在上面的代码中所做的。获得进程 ID 后,您可以从中获取进程句柄,并枚举其模块。第一个模块是主 EXE 文件。我在 TProcessInfo 组件中实现了该组件,该组件与 AppInfo.pas 包含在同一包中。

关于delphi - 如何从 Windows 任务管理器(“应用程序”选项卡)获取应用程序及其在 Delphi 硬盘上的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3382384/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com