1.创建类User32API.cs,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

using System.Collections;

namespace XXX

{

class User32API

{

private static Hashtable processWnd = null;

public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

static User32API()

{

if (processWnd == null)

{

processWnd = new Hashtable();

}

}

[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]

public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]

public static extern IntPtr GetParent(IntPtr hWnd);

[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]

public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

[DllImport("user32.dll", EntryPoint = "IsWindow")]

public static extern bool IsWindow(IntPtr hWnd);

[DllImport("kernel32.dll", EntryPoint = "SetLastError")]

public static extern void SetLastError(uint dwErrCode);

public static IntPtr GetCurrentWindowHandle()

{

IntPtr ptrWnd = IntPtr.Zero;

uint uiPid = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;  // 当前进程 ID

object objWnd = processWnd[uiPid];

if (objWnd != null)

{

ptrWnd = (IntPtr)objWnd;

if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄

{

return ptrWnd;

}

else

{

ptrWnd = IntPtr.Zero;

}

}

bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);

// 枚举窗口返回 false 并且没有错误号时表明获取成功

if (!bResult && Marshal.GetLastWin32Error() == 0)

{

objWnd = processWnd[uiPid];

if (objWnd != null)

{

ptrWnd = (IntPtr)objWnd;

}

}

return ptrWnd;

}

private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)

{

uint uiPid = 0;

if (GetParent(hwnd) == IntPtr.Zero)

{

GetWindowThreadProcessId(hwnd, ref uiPid);

if (uiPid == lParam)    // 找到进程对应的主窗口句柄

{

processWnd[uiPid] = hwnd;   // 把句柄缓存起来

SetLastError(0);    // 设置无错误

return false;   // 返回 false 以终止枚举窗口

}

}

return true;

}

}

}

2.调用类User32API获取控制台句柄的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace XXXX

{

class XXX

{

static void Main(string[] args)

{

IntPtr hwnd=User32API.GetCurrentWindowHandle();//获取句柄

Console.WriteLine("{0} ", hwnd);

Console.ReadLine();

}

}

}

linux获取控制台窗口句柄,C#获取控制台句柄的方法相关推荐

  1. python获取软件窗口句柄_Python获取浏览器窗口句柄过程解析

    句柄(handle)是C++程序设计中经常提及的一个术语.它并不是一种具体的.固定不变的数据类型或实体,而是代表了程序设计中的一个广义的概念.句柄一般是指获取另一个对象的方法--一个广义的指针,它的具 ...

  2. linux获取窗口句柄,如何获取在winexec或shellexecute中执行的句柄?

    没有一般的方法来获得应用程序的"窗口句柄",因为不能保证任何程序都有一个窗口句柄.一个程序可能有许多顶级的句柄(例如,Microsoft Word,每个文档一个),或者它可能根本没 ...

  3. python获取当前窗口句柄_人生苦短,我学python之python+selenium 多窗口、句柄(handle)...

    转载自 http://www.cnblogs.com/yoyoketang/p/6128611.html 前言 有些页面的链接打开后,会重新打开一个窗口,对于这种情况,想在新页面上操作,就得先切换窗口 ...

  4. sentinel 端口_Sentinel原理:控制台是如何获取到实时数据的

    Sentinel 系列教程,现已上传到 github 和 gitee 中: GitHub: https://github.com/all4you/sentinel-tutorial Gitee: ht ...

  5. python获取窗口句柄_Python+selenium 获取浏览器窗口坐标、句柄的方法

    1.0 获取浏览器窗口坐标 python目录可找到Webdriver.py 文件定义了get_window_rect()函数,可获取窗口的坐标和大小(长宽),但出现"Command not ...

  6. 【python句柄获取】——简单明了的获取窗口句柄,并使用句柄操作获取相应内容(全网最详细)

    点个赞留个关注吧!! 所使用模块 import win32gui import win32con import win32api import psutil import win32process 获 ...

  7. C# API之常用操作窗口类函数详解[查找所有窗口、获取目标句柄的类名、获取窗口文本、获取当前活动窗口、通过窗口句柄获取线程ID、获取指定窗口位置]

    /// <summary>/// 查找所有窗口(只要是在进程里面的)/// 如果不限制类名或者标题使用null代替/// </summary>/// <param nam ...

  8. 按键精灵通过句柄获取窗口坐标_大漠插件后台找图、自动获取坐标窗口句柄鼠标多点左单击脚本...

    大漠插件后台找图.自动获取坐标窗口句柄鼠标多点左单击 脚本 一.准备工作 第一步:下载大漠插件.解压,运行"注册大漠插件到系统.bat",把全部东东都复制到按键精灵的plugin目 ...

  9. 论文中文翻译——Double-Fetch情况如何演变为Double-Fetch漏洞:Linux内核中的双重获取研究

    本论文相关内容 论文下载地址--Web Of Science 论文中文翻译--How Double-Fetch Situations turn into Double-Fetch Vulnerabil ...

最新文章

  1. 压力测试后,BCH采用增加
  2. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
  3. 003 Android之线性布局与基础控件
  4. 又到618,.NET 千万级秒杀架构到底有多牛
  5. 远程Linux主机安装zsh插件zsh-syntax-highlighting
  6. jdbc连接数据库的步骤(转载)
  7. php获取微信小程序用户头像,微信小程序获取用户头像+昵称+openid,小程序登录!附前端后端源码!...
  8. linux mysql c语言 api_linux连接MySQL数据库(C语言 API 分析,动态插入修改数据)
  9. 功能测试常用6种方法_云端功能测试:更好的12种方法
  10. Launch文件编写
  11. 运动蓝牙耳机怎么选、几款适合运动的运动耳机
  12. springboot + shiro的配置
  13. 通过java解码web前端直接预览海康威视摄像头
  14. stopstart按钮怎么用_汽车Start-Stop启停技术简明讲解
  15. 输入一个8bit数,输出其中1的个数。如果只能使用1bit全加器,最少需要几个,请使用verilog进行描述?(附verilog代码)
  16. GDUT - 专题学习1 I - 滑动窗口
  17. 案例详解-如何在 Linux 系统中安装和使用 7zip 以及 7zip的脚本编程使用教程(非p7zip,而是官方版本7zip for linux)附deb包下载链接
  18. HUST软测1504班第2周作业成绩:WordCount
  19. 精品杀毒及防木马软件大全
  20. centos常用命令

热门文章

  1. win7cmd闪退_win7系统运行bat批处理文件出现闪退的解决方法
  2. win10找不到wifi网络_笔记本连不上WiFi怎么办?
  3. Latex---局部调大字体--下划线--首行缩进--悬挂缩进
  4. php开源 会员资料,会员详情/会员信息/用户信息
  5. win10 C盘清理总结记录
  6. 穆利堂[推荐] WxPM信息化整体解决方案-河南郑州房地产工程项目管理系统软件 穆穆-movno1
  7. wamp实现外网访问本地
  8. 在windows生成SSH秘钥连接linux远程主机
  9. F1赛车相关公开数据集
  10. c++primer 第二章 变量和基本类型