句柄是什么
句柄在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();
}
}
}
https://telugusaahityam.com/User:RosariaElkins
http://slavuta.0pk.me/viewtopic.php?id=3282#p10467
https://nsibirsk.ru/forum/obyavleniya/topic-20903.html
https://livestreet.imbachat.com/2024/01/20/istoriya-audioformatov-ot-vinila-do-striminga.html
https://moneybux.boltun.su/viewtopic.php?id=730#p1590
http://tvoirostov.ru/blogi/istorija_audioformatov_ot_vinila_do_striminga/
http://www.bisound.com/forum/showthread.php?p=601725
http://freereklama.borda.ru/?1-0-0-00008478-000-0-0-1706616668
https://may-green.ru/forum/user/13262/
https://xdpascal.com/index.php/Mp3gid.co
Мак Сима Мгла – Пьяная Кровь скачать песню в mp3 и слушать онлайн бесплатно
Мак Сима Мгла – Пьяная Кровь
http://hobby-svarka.ru/viewtopic.php?f=5&t=4452
https://elearnportal.science/wiki/Mp3bit.pw_2
Real System – There Is No More Love (Randy Norton Edit) скачать песню в mp3 и слушать онлайн бесплатно
Real System – There Is No More Love (Randy Norton Edit)
https://aznakaevsk.borda.ru/?1-3-0-00000806-000-0-0-1704440178
http://mygame.5nx.ru/viewtopic.php?f=110&t=49866
https://taksafonchik.borda.ru/?1-3-0-00013971-000-0-0-1706615787
https://shooting-russia.ru/forum/?PAGE_NAME=profile_view&UID=31880
http://share.psiterror.ru/2024/02/01/vidy-hip-hopa-vostochnoe-poberezhe-zapadnoe-poberezhe-yuzhnyy-stil-i-td.html
http://borderforum.ru/viewtopic.php?f=26&t=8200
https://villagewebcompany.net/index.php/Mp3bit.pw
https://muigniteforum.com/index.php?/gallery/image/202-%D1%8D%D0%B2%D0%BE%D0%BB%D1%8E%D1%86%D0%B8%D1%8F-%D0%B7%D0%B2%D1%83%D0%BA%D0%B0-%D0%BF%D0%BE%D0%BF%D1%83%D0%BB%D1%8F%D1%80%D0%BD%D1%8B%D0%B5-%D0%BC%D1%83%D0%B7%D1%8B%D0%BA%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5-%D0%B0%D0%BB%D1%8C%D0%B1%D0%BE%D0%BC%D1%8B-%D1%81-%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%B0-90-%D1%85/
https://sciencewiki.science/wiki/User:WillisKastner71
http://old.kakdelat.ru/company/personal/user/1695/blog/7602/
http://skodarapidclub.ru/forum/showthread.php?p=1584832#post1584832
https://vikmarkovci.7bb.ru/viewtopic.php?id=3419#p32341
https://bbarlock.com/index.php/User:NolaStines
https://reveal.ru/gate.html?name=Journal&file=display&jid=51200
http://www.rrsclub.ru/showthread.php?p=36955#post36955
Skryaga – Пливеш У Човні скачать песню в mp3 и слушать онлайн бесплатно
Skryaga – Пливеш У Човні