运行实例
下载地址
https://download.csdn.net/download/u012932582/87755579
用到的两个win api
/// <summary>
/// 通过窗口句柄获取线程ID(TID)和进程ID(PID)
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="PID">返回进程ID</param>
/// <returns>返回线程ID</returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int PID); //获取线程ID
/// <summary>
/// 获得包含指定点的窗口的句柄
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(System.Drawing.Point p);
调用
//坐标
int x = int.Parse(this.tx_point_x.Text);
int y = int.Parse(this.tx_point_y.Text);
//根据坐标获取句柄
IntPtr _hwnd = WindowFromPoint(new Point(x, y));
int pid = 0;
//根据句柄找pid
GetWindowThreadProcessId(_hwnd, out pid);
//根据pid找进程详细信息
var p = FindInfoByPid(pid.ToString());
this.textBox1.Text = $"抓取句柄:{_hwnd.ToString()} \r\n" +
$"PID:{pid.ToString()} \r\n" +
$"进程名:{p.ProcessName} \r\n" +
$"运行文件名:{p.FileName} \r\n" +
$"运行程序路径:{p.FileAddress} \r\n" +
$"文件版本:{p.FileVersion} \r\n" +
$"文件描述:{p.FileDescription} \r\n" +
$"产品名称:{p.ProductName} \r\n" +
$"产品版本:{p.ProductVersion} \r\n";
/// <summary>
/// 根据PID找信息
/// </summary>
/// <param name="strProcid"></param>
/// <returns></returns>
public CloudUpload FindInfoByPid(string strProcid)
{
try
{
foreach (Process p in Process.GetProcesses())
{
if (p.Id.Equals(Int32.Parse(strProcid)))
{
var t = new CloudUpload()
{
ProcessName = p.ProcessName,
FileName = p.MainModule.ModuleName,
FileAddress = p.MainModule.FileName,
FileVersion = p.MainModule.FileVersionInfo.FileVersion,
ProductName = p.MainModule.FileVersionInfo.ProductName,
ProductVersion = p.MainModule.FileVersionInfo.ProductVersion,
FileDescription = p.MainModule.FileVersionInfo.FileDescription
};
return t;
}
}
return null;
}
catch
{
return null;
}
}