转自: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. 华为S5700系列交换机AR配置静态IP双链路负载分担

    适用于:有多个以太WAN口的机型. 业务需求: 运营商1分配的接口IP为100.100.1.2,子网掩码为255.255.255.252,网关IP为100.100.1.1. 运营商2分配的接口IP为2 ...

  2. Linux下的sed命令使用详解

    sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”pattern space,接着用sed命令处理缓冲区中的内容, ...

  3. MVC bootstrap 实现 bootstrap table 左右传递数据

    源码: @{ ViewBag.Title = "Index"; } @using BC.Platform.UPMS.Models; <!DOCTYPE html> &l ...

  4. 快速将对象转化为JSON格式

    1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...

  5. Windows 10 操作系统删除Administrator登录选项

    在win10操作系统锁屏界面登录选项中突然出现了两个用户选项,一个是自己微软账户登录的用户,另一个是Administrator登录选项,每次看到好别扭,怎么去掉呢? 一.以管理员身份运行命令提示符(c ...

  6. centos 使用rz sz指令

    在linux下安装rz很方便,使用 yum install lrzsz 就可以安装,正常使用rz和sz命令. 下面对sz和rz命令的一点介绍: 一般来说,linux服务器大多是通过ssh客户端来进行远 ...

  7. cassandra 之 集群部署

    其实关于部署没啥好说的,修改config/cassandra.yaml以下几个地方就可以了 cluster_name: 'cluster_cassandra' data_file_directorie ...

  8. python学习笔记(virtualenv下载安装)

    之前博客评论中有人建议我面对多个python版本的情况.可以使用virtualenv这个python虚拟沙盒 首页是利用pip下载.关于pip如何下载安装前面的博客中已经提到就不细说 cmd直接进入p ...

  9. flask学习(二):认识web

    url详解: URL是Uniform Resource Locator 的简写,统一资源定位符 一个URL由以下及几部分组成: scheme://host:port/path/?query-strin ...

  10. Xcode export/upload error: Your session has expired. Please log in 解决方法

    问题: 突然打包账号不好使了    重登  重启  清缓存  一套都打完了   还是不好使 解决方法:  删除掉其他账号 重新登录 参考网址 http://stackoverflow.com/ques ...