转自: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. GIT使用—安装配置及工作流程

    一.Git 与 SVN 区别 GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等. 1.GIT是分布式的,SVN不是:这是GIT和其它非分布式的版本控制系统,例如SVN,CV ...

  2. Secondary ,Supplementary alignment 和bwa mem的-M -Y参数

    1.supplementary alignment supplementary alignment是指一条read的一部分和参考区域1比对成功,另一部分和参考区域2比对成功,参考区域1和参考区域2没有 ...

  3. VRChat简易教程1-开发环境准备(SDK)

    原文:https://docs.vrchat.com/docs/setting-up-the-sdk 1 Unity 2017.4.15f1 下载地址https://download.unity3d. ...

  4. linux新手学习之Arch Linux入门经验分享

    我一直是以Ubuntu与Fedora作为新手入门的系统,但是其实我真正想推荐的是Arch,经过前面的学习,或许你对Linux已经有了一个大致的了解,现在如果你想加速你的步伐,也许可以看看本文.如果要问 ...

  5. 《Pro Git》第2章 Git基础

    1.获取Git仓库 1.1从现有的目录中初始化仓库 进入项目目录,git init,会创建一个名为.git的子目录 1.2克隆现有的仓库 git clone [url],会将远程Git仓库中的每一个文 ...

  6. iOS 10 系统 AVPlayer视频播放不了问题解决

    使用[AVAudioPlayer Play]时出现了异常... 由于xcode中设置了当所有异常出现时的断点,,解决办法是将all改为Objective-C: libc++abi.dylib`__cx ...

  7. mysql官网下载链接——绿色版&安装版

    windows64位5.5.60安装版 https://downloads.mysql.com/archives/get/file/mysql-5.5.60-winx64.msi windows64位 ...

  8. Spring -- aop, 用Aspectj进行AOP开发

    1. 概要 添加类库:aspectjrt.jar和aspectjweaver.jar 添加aop schema. 定义xml元素:<aop:aspectj-autoproxy> 编写jav ...

  9. 教你打造一个Android组件化开发框架

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 CC:Component Caller,一个android组件化开发框架, 已开源,github地址:https://github ...

  10. windows8.1下安装Cygwin并通过apt-cyg安装软件包

    参考 http://yuanshuilee.blog.163.com/blog/static/2176972752014126786185/ http://www.cnblogs.com/zhang- ...