首页 > 编程语言 >根据窗口句柄获取所在程序路径

根据窗口句柄获取所在程序路径

时间:2023-05-13 14:33:20浏览次数:34  
标签:end 句柄 路径 TObject pHandle path var 窗口

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses PsAPI; {GetModuleFileNameEx 函数需要它}

{根据窗口句柄获取所在程序路径的函数}
function GetProcessExePath(h: HWND): string;
var
  pid: Cardinal;
  pHandle: THandle;
  buf: array[0..MAX_PATH] of Char;
begin
  {先获取进程 ID}
  GetWindowThreadProcessId(h, @pid);
  {再获取进程句柄}
  pHandle := OpenProcess(PROCESS_ALL_ACCESS, False, pid);
  {获取进程路径}
  GetModuleFileNameEx(pHandle, 0, buf, Length(buf));
  CloseHandle(pHandle);
  Result := buf;
end;

{测试当前程序}
procedure TForm1.Button1Click(Sender: TObject);
var
  path: string;
begin
  path := GetProcessExePath(Handle);
  ShowMessage(path);
end;

{测试记事本 - 需要随便打开一个记事本}
procedure TForm1.Button2Click(Sender: TObject);
var
  wh: HWND;
  path: string;
begin
  wh := FindWindow('Notepad', nil);
  path := GetProcessExePath(wh);
  ShowMessage(path);
end;

end.

标签:end,句柄,路径,TObject,pHandle,path,var,窗口
From: https://www.cnblogs.com/lucken2000/p/17397352.html

相关文章

  • 滑动窗口的最大值
    classSolution{public:vector<int>res;deque<int>q;vector<int>maxInWindows(vector<int>&nums,intk){for(inti=0;i<nums.size();i++){//先弹出左端点,保证队列里所有元素都是有效的......
  • 创建异形窗口1
    unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,StdCtrls;type TForm1=class(TForm)  Button1:TButton;  procedureButton1Click(Sender:TObject);  procedureFormDblClick......
  • 用鼠标获取任意窗口的句柄, 并把它当作子窗体
    unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,StdCtrls,ExtCtrls;type TForm1=class(TForm)  Panel1:TPanel;  Edit1:TEdit;  Button1:TButton;  Button2:TButton;......
  • 解决Tomcat服务器开启时DOS窗口的乱码问题(控制台乱码)
    从tomcat的目录中找:conf->logging.properties,用记事本打开,找到如下信息: 将UTF-8改为GBK(WIndows的命令窗口采用GBK的编码方式)......
  • 车辆路径问题——CVPR的Python实现
    车辆路径问题通常被定义为装运一系列点或接收点,通过他们组织车辆适当途径有序。在一定的约束条件,如对商品的需求,交货数量,交付的交付时间,车辆容量限制,行驶里程限制,时间限制,以实现某些目标。如果最短距离,最低的成本,尽可能少的时间,尽量少使用车辆。在物流和运输,因为运输点,更多的客户,......
  • 拿到当前句柄di与鼠标当前位置信息Sx,Sy
    importtime,win32guiimportpyautoguitime.sleep(2)handle1=win32gui.GetForegroundWindow()#GetForegroundWindow使用此方法返回一个句柄di保存在handle1print("显示当前句柄id:",handle1)x,y=pyautogui.position()#使用pyautogui.position()使用此方法返回两个数值......
  • 力扣124. 二叉树中的最大路径和
    二叉树中的 路径 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。路径和 是路径中各节点值的总和。给你一个二叉树的根节点 root ,返回其 最大路径和 。 示例......
  • 在PhpStorm项目工具窗口中显示.idea文件夹
    转自:https://www.codenong.com/33010238/ 对于.idea,它也有专用的注册表设置(默认情况下,我至少在PhpStorm中启用了此设置)Help|FindAction...并查找registry(或在Windows上使用默认键盘映射通过Maintenance Ctrl+Alt+Shift+/)进入内部-查找projectView.hide.dot.idea条......
  • 计算给定目录下文件路径
    计算给定目录下所有文件的绝对路径deffile_abso_path(dir_path):'''func:计算给定父类目录下的所有文件的绝对路径'''final_path_list=[]forparent,dirnames,filenamesinos.walk(dir_path,followlinks=True):#按照父类目录到子类目录进行......
  • oracle 禁用和强制直接路径读
    HowToForceDirectPathReadforSQLStatements(DocID2426051.1)TherearewaystodisabledirectpathreadforSQLstatementsasfollows:1.event10949level12._serial_direct_read=NEVERHowever,therearenodirectmethodstoforcethedirec......