User-Profile-Image
hankin
  • 5
  • 首页
  • 留言
  • 仓库
  • 云端
  • 分类
    • 随笔
    • 安卓逆向
    • php
    • node.js
    • C#
  • 页面
    • 个人技术栈
    • 留言
  • 友链
    • 沉沦云API
    • 沉沦云端
    • SinKingMusic
    • 美和易思刷课
    • 神奇的七云
    • khaos编程小站
    • 小九实验室
    • 一叶三秋
    • 青年的故事
    • :李白云博客
    • 噜阿噜-资源站
    • 小k
Help?

Please contact us on our email for need any support

Support
    首页   ›   C#   ›   正文
C#

c#通过句柄进行模拟操作

2020-05-08 21:43:07
226105  1254 7

句柄是什么

句柄在windows编程中是一个很重要的概念,在许多地方都扮演着重要的角色。在windows环境中,句柄是用来标识项目的,这些项目包括:

(1)模块(module)

(2)任务(task)

(3)实例(instance)

(4)文件(file)

(5)内存块(block of memory)

(6)菜单(menu)

(7)控制(control)

(8)字体(font)

(9)资源(recource),包括图标(icon),光标(cursor),字符串(string)等

(10)GDI对象(GDI object),包括位图(bitmap),画刷(brush),元文件(metafile),调色板(palette),画笔(pen),区域(region),以及设备描述表(device context)

Windows是一个以虚拟内存为基础的操作系统,在这种环境下,Windows内存管理器经常在内存中来回移动对象,以此来满足各种应用程序的需要。对象被移动意味着它的地址变化了。由于地址总是如此变化,所以Windows操作系统为各应用程序腾出一些内存地址,用来专门登记各应用对象在内存中的地址变化,而这地址(存储单元的位置)本身是不变的。Windows内存管理器在移动对象在内存中的位置后,把对象新的地址告知这个句柄地址来保存。这样我们只需记住这个句柄地址就可以间接地知道对象具体在内存中的哪个位置。这个地址是在对象装载(Load)时由系统分配给的,当系统卸载时(Unload)又释放给系统。

因此,Windows程序中并不是用物理地址来标识一个内存块,文件,任务,或动态装入模块的,相反,WINDOWS API给这些项目分配确定的句柄,并将句柄返回给应用程序,然后通过句柄来进行操作。

句柄地址(稳定)->记载着对象在内存中的地址->对象在内存中的地址(不稳定)->实际对象。但是,必须注意注意的是,程序每次重新启动,系统不能保证分配给这个程序的句柄还是原来的那个句柄,而且绝大多数情况的确是不一样的。

句柄可以做什么

句柄所有操作是通过windows的user32接口完成,因此需要导入user32.dll,通过句柄我们可以跨程序的执行任何操作,修改任何windows窗体,这是一个十分强大的功能,可以帮助我们实现很多人工繁琐的操作

以下是我封装的句柄操作类,可以直接使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

namespace AutoLogin
{
    public struct WindowInfo
    {
        public IntPtr hWnd;
        public string szWindowName;
        public string szClassName;
    }
    public struct ControInfo
    {
        public IntPtr hWnd;
        public string ControName;
        public string ClassName;
    }
    class GetWindowInfo
    {
        [DllImport("shell32.dll")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam);
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
        //查找子控件
        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        //发送消息
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
        //窗口置顶
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        //遍历子控件
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        //回调函数
        public delegate bool CallBack(IntPtr hwnd, int lParam);
        public static IntPtr FindWindowInfo(string soft, string classname = "")
        {
            WindowInfo[] a = GetAllDesktopWindows();
            int i = 0;
            int index = 0;
            for (i = 0; i < a.Length; i++)
            {
                if (!classname.Equals(""))
                {
                    if (a[i].szWindowName.ToString().Contains(soft) && classname == a[i].szClassName.ToString())
                    {
                        index = i;
                        break;
                    }
                }
                else
                {
                    if (a[i].szWindowName.ToString().Contains(soft))
                    {
                        index = i;
                        break;
                    }
                }
            }
            if (i == 0)
            {
                return (IntPtr)0;
            }
            return a[index].hWnd;
        }
        public static WindowInfo GetInfo(IntPtr hWnd)
        {
            WindowInfo wnd = new WindowInfo();
            StringBuilder sb = new StringBuilder(256);
            wnd.hWnd = hWnd;
            GetWindowTextW(hWnd, sb, sb.Capacity);
            wnd.szWindowName = sb.ToString();
            GetClassNameW(hWnd, sb, sb.Capacity);
            wnd.szClassName = sb.ToString();
            return wnd;
        }
        //枚举控件
        public static ControInfo[] GetALLContros(IntPtr parent)
        {
            List<ControInfo> ConList = new List<ControInfo>();
            EnumChildWindows(parent,delegate (IntPtr hWnd, int lParam)
            {
                ControInfo wnd = new ControInfo();
                wnd.hWnd = hWnd;
                StringBuilder sb = new StringBuilder(256);
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.ClassName = sb.ToString();
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.ControName = sb.ToString();
                ConList.Add(wnd);
                return true;
            }, 0);
            return ConList.ToArray();
        }
        //枚举窗体
        private static WindowInfo[] GetAllDesktopWindows()
        {
            List<WindowInfo> wndList = new List<WindowInfo>();
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
                wnd.hWnd = hWnd;
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();
                //Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);
                wndList.Add(wnd);
                return true;
            }, 0);
            return wndList.ToArray();
        }
    }
}

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

7 打赏
评论 (5,570)

回复给 点击这里取消回复。

欢迎您 游客  

  • https://mazda-demio.ru/forums/index.php?autocom=gallery&req=si&img=6336

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7034

    4周前
    回复
  • http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4542

    4周前
    回复
  • http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4457

    4周前
    回复
  • https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=5111

    4周前
    回复
  • https://hrv-club.ru/forums/index.php?autocom=gallery&req=si&img=7178

    4周前
    回复
  • http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4860

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7336

    4周前
    回复
  • http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4857

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7335

    4周前
    回复
  • http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4543

    4周前
    回复
  • http://atora.ru/blogs/interesnye-statyi/foo-fighters-energiya-roka-i-dukh-nezavisimosti1.php

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7336

    4周前
    回复
  • https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=5104

    4周前
    回复
  • https://berforum.ru/gallery/image/8946-15/

    4周前
    回复
  • https://hrv-club.ru/forums/index.php?autocom=gallery&req=si&img=3736

    4周前
    回复
  • https://mazda-demio.ru/forums/index.php?autocom=gallery&req=si&img=6621

    4周前
    回复
  • https://myteana.ru/forums/index.php?autocom=gallery&req=si&img=6911

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7320

    4周前
    回复
  • http://passo.su/forums/index.php?autocom=gallery&req=si&img=4328

    4周前
    回复
  • http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5502

    4周前
    回复
  • https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7319

    4周前
    回复
  • https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=5092

    4周前
    回复
  • https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=5071

    4周前
    回复
  • http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5484

    4周前
    回复
  • http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5483

    4周前
    回复
  • https://myteana.ru/forums/index.php?autocom=gallery&req=si&img=6867

    4周前
    回复
  • https://hrv-club.ru/forums/index.php?autocom=gallery&req=si&img=7131

    4周前
    回复
  • http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5468

    4周前
    回复
  • http://toyota-porte.ru/forums/index.php?autocom=gallery&req=si&img=3372

    4周前
    回复
1 … 177 178 179 180 181 … 186
流逝中沉沦
12文章 50924评论 1470点赞 4054228浏览

随机文章
QQWeb登陆P值算法
5年前
SinKingPHP个人MVC框架
5年前
c#通过句柄进行模拟操作
5年前
c#音乐解析组件(dll)
5年前
QQ全套扫码加速
5年前
最新评论
+376
网站留言
Copyright © 2025 网站备案号: 皖ICP备18022767号-3
沉沦云网络. SinKingCloud
主页
页面
  • 个人技术栈
  • 留言
博主
流逝中沉沦
流逝中沉沦 管理员
一个热爱生活热爱技术的00后少年
12 文章 50924 评论 4054228 浏览
测试
测试

主题风格 设置选项


布局
背景颜色
背景渐变
背景图片
微信 QQ空间 QQ好友 新浪微博

“扫一扫”分享到微信

赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付