转自:http://qqhack8.blog.163.com/blog/static/11414798520113363829505/
C# 通过窗口句柄获取程序路径 图标
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace K8加强版任务管理器
{
class k8taskmgr
{
private const int MAX_PATH = 260;
public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
[DllImport("coredll.dll")]
public extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
[DllImport("coredll.dll")]
public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
[DllImport("coredll.dll")]
public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);
[DllImport("coredll.dll")]
public extern static bool CloseHandle(IntPtr hObject);
[DllImport("Coredll.dll", EntryPoint = "GetModuleFileName")]
private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr ExtractIconEx(string fileName, int index, ref IntPtr hIconLarge, ref IntPtr hIconSmall, uint nIcons);
//通过句柄获取运行程序路径
public static String GetAppRunPathFromHandle(IntPtr hwnd)
{
int pId = 0;
IntPtr pHandle = IntPtr.Zero;
GetWindowThreadProcessId(hwnd, ref pId);
pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pId);
StringBuilder sb = new StringBuilder(MAX_PATH);
GetModuleFileName(pHandle, sb, sb.Capacity);
CloseHandle(pHandle);
return sb.ToString();
}
//根据句柄获取运行程序小图标 //当然也可以获取大图标
private Icon GetSmallIconFromHandle(IntPtr hwnd)
{
IntPtr hLargeIcon = IntPtr.Zero;
IntPtr hSmallIcon = IntPtr.Zero;
String filePath = GetAppRunPathFromHandle(hwnd);
ExtractIconEx(filePath, 0, ref hLargeIcon, ref hSmallIcon, 1);
Icon icon = null;
try
{
icon = (Icon)Icon.FromHandle(hSmallIcon);
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
}
return icon;
}
}
}