转自: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;
        }

}
}

C# 通过窗口句柄获取程序路径 图标的更多相关文章

  1. WPF 获取程序路径的一些方法,根据程序路径获取程序集信息

    一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...

  2. C#获取程序路径

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  3. Android获取程序路径 (/data/data/appname)

    Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...

  4. Delphi~通过程序窗体句柄获取程序路径

    http://www.cnblogs.com/Jesses/articles/1636323.html 引用PsAPI var  h:HWND;  pid: Cardinal;  pHandle: T ...

  5. golang获取程序运行路径

    golang获取程序运行路径: /* 获取程序运行路径 */ func getCurrentDirectory() string { dir, err := filepath.Abs(filepath ...

  6. C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)

    C#.ASP.NET获取当前应用程序的绝对路径,获取程序工作路径   ============================================ 使用 Application.Start ...

  7. [WinAPI] API 12 [获取程序所在的目录、程序模块路径,获取和设置当前目录]

    Windows系统提供一组API实现对程序运行时相关目录的获取和设置.用户可以使用GetCurrentDirectory和SetCurrentDirectory获取程序的当前目录,获取模块的路径使用G ...

  8. delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)

    uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...

  9. inux关于readlink函数获取运行路径的小程序

    inux关于readlink函数获取运行路径的小程序   相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int  re ...

随机推荐

  1. 安装专业版的linux的方法 图解安装专业版的linux

    按装一个linux系统其实很简单,不再像以前那样光光盘就好些个,一不小心还又可能装错,实在也是一个大问题.现在好了基本上都是简单安装+网络升级先安装主要的后面如果需要什么在装什么?大大简化了流程.不行 ...

  2. Linux系统下配置squid代理服务器的过程详解

    简单记录一下Squid透明代理服务器的配置 环境:VirtualBox + CentOS 6.0 + squid-3.1.4-1.el6.i686 0.检查squid是否默认安装,没有安装的先安装 [ ...

  3. CentOS 7 安装各个桌面版本

    http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...

  4. Zookeeper初步了解

    Zookeeper初步了解: Zookeeper实现了许多复杂的事情,例如实现了Zookeeper Atomic Broadcasting Protocal来广播状态信息的变化,Fast Paxas ...

  5. All Classic Bluetooth profile for iPhone

    iPhone BC profiles Profile Decription HFP1.6 1.通知客户端有电话拨入:2.免提功能:3.音频的输入输出机制. PBAP 1.下载通讯录:2.查找通讯录:3 ...

  6. 多线程之wait,notify,volatile,synchronized,sleep

    最近在学习多线程,现在进行总结一下吧.首先要了解一下以下几个名词. (1)wait:当线程调用wait()方法时,当前该线程会进入阻塞状态,且释放锁,使用wait方法的时候,必须配合synchroni ...

  7. 使用nagios检测windows服务器

    1.安装nagios windows客户端 下载NSClient++的安装包,注意根据实际的32/64CPU来下载 下载地址 http://sourceforge.net/projects/nscpl ...

  8. MFC clist 学习设计

    最近想设计一款WEBSHELL的综合破解工具. 然后设计到了日志输出那儿,因为MFC不熟悉,刚学.所以一直在想用edit控件好还是clist比较好. 今天设计了一下日志输出界面,然后记录一下学习笔记. ...

  9. gitlab库迁移

    gitlab 迁移 gitlab上一共有两个分之,一级提交记录. git clone --bare http://111.222.333.xxx/jiqing/test.git 执行成功后,会多一个t ...

  10. srm开发(基于ssh)(4)

    1 input处理内容补充 -在struts2里面有错误处理机制,当上传文件超过默认的大小,自动返回结果input -在struts.xml中配置input的返回结果 <!-- 配置input结 ...