HANDLE和HMOUDLE、HWND讲解
HWND是线程相关的,通常指明窗口句柄,通过句柄找到窗口所属进程和线程
Handle 是代表系统的内核对象,如文件句柄,线程句柄,进程句柄。
系统对内核对象以链表的形式进行管理,载入到内存中的每一个内
核对象都有一个线性地址,同时相对系统来说,在串列中有一个索引位置,这个索引位置就是内核对象的handle。
HINSTANCE的本质是模块基地址,他仅仅在同一进程中才有意义,跨进程的HINSTANCE是没有意义
HMODULE 是代表应用程序载入的模块,在win32系统下表示为被载入模块的线性地址。
HINSTANCE 在win32下与HMODULE是相同的东西(只有在16位windows上,二者有所不同).
————————————————
What is the difference between HINSTANCE and HMODULE?
They mean the same thing today, but at one time they were quite different.
It all comes from 16-bit Windows.
In those days, a "module" represented a file on disk that had been loaded into memory, and the module "handle" was a handle to a data structure that described the parts of the file, where they come from, and where they had been loaded into memory (if at all). On the other hand an "instance" represented a "set of variables".
One analogy that might (or might not) make sense is that a "module" is like the code for a C++ class - it describes how to construct an object, it implements the methods, it describes how the objects of the class behave. On the other hand, an "instance" is like a C++ object that belongs to that class - it describes the state of a particular instance of that object.
In C# terms, a "module" is like a "type" and an instance is like an "object". (Except that modules don't have things like "static members", but it was a weak analogy anyway.)
Here's a diagram. (Recall that we discussed 16-bit HRSRC in a previous entry.)
In 16-bit Windows, all programs ran in a single address space, and if a DLL was used by five programs, it was loaded only once into memory. In particular, it got only one copy of its data segment. (In C++/C# terms, a DLL is like a "singleton class".)
That's right, DLLs were system-global rather than per-process. The DLL did not get a separate copy of its data for each process that loaded it. If that was important to your DLL, you had to keep track of it yourself.
In geek terms, there was only one "instance" of a DLL in the system.
On the other hand, if you ran two copies of Notepad, each one got its separate set of variables - there were two "instances".
Both running copies of Notepad shared the NOTEPAD module (so the code and resources were shared), but each had its own copy of its variables (separate data segment). There were two "instances" of Notepad.
The "instance" handles in the above diagrams are the data segments.
Programs are identified by their the instance handle. You can't use the module handle, because the two copies of Notepad have the same module handle (since the same code is running in each). The thing that makes them different is that each has its own set of global variables.
This is why the WinExec and ShellExecute functions return HINSTANCE: They are holdovers from 16-bit Windows, where HINSTANCEs were the way to identify running programs.
The method by which code receives its HINSTANCE (i.e., knows where its global variables are) I will leave for a future article. It is somehow related to the now-obsolete MakeProcInstance function.
When it came to design Win32, the question then arose, "What do we do with HINSTANCE and HMODULE for Win32?" Since programs ran in separate address spaces, you didn't have instance handles visible across process boundaries. So the designers took the only thing they had: The base address of the module. This was analogous to the HMODULE, since the file header describes the contents of the file and its structure. And it was also analogous to the HINSTANCE, since the data was kept in the data segment, which was mapped into the process directly.
So in Win32, HINSTANCE and HMODULE are both just the base address of the module.
Tomorrow, I'll talk about that mysterious hinstPrev parameter to WinMain.
————————————————
版权声明:本文为CSDN博主「VCDI」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27011361/article/details/82959656
HANDLE和HMOUDLE、HWND讲解的更多相关文章
- QQ登陆功能的实现2
QQ登陆功能的实现2 由于看到园子里有朋友说需要讲解和剖析实现的步骤,前面的QQ登陆实现只有代码,所以这篇补上 1. 分析 1). 当运行QQ.exe后会出现qq登陆界面的窗体 2). 我们用spy ...
- Windows核心编程随笔
最近在学习Windows底层原理,准备写个系列文章分享给大家,Michael Li(微软实习期间的Mentor,为人超好)在知乎回答过一些关于学习Windows原理的书籍推荐,大家可以拜读其中一本来入 ...
- C#与C++通信
# C#与C++相互发送消息 # ## C#端: ## namespace CshapMessage { /// /// MainWindow.xaml 的交互逻辑 /// public partia ...
- C#调用win32 api 操作其它窗口
实现以下功能: 找到窗体 找到控件(也叫子窗体) 获取内容 获取位置 设置 位置 内容 鼠标点击 示范 1. 找窗体 以操作系统自带的计算器为例 string clWindow = "Cal ...
- vfp 智能感知拓展应用
*======================================================================================== * * Versio ...
- C#封装好的Win32API
Kernel.cs using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = Syst ...
- c# 列举所有窗口和子窗口
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam); [DllImport("user32.dll", Exact ...
- Direct2D教程(外篇)环境配置
2014年世界杯首场淘汰赛马上开始了,闲着没事,整理以前的博客草稿打发时间,意外的发现这篇文章,本来是打算加入到Direct2D那个系列的,不知道为什么把它给遗漏了.环境配置,对于熟手来说,不是什么重 ...
- WPFの三种方式实现快捷键
最近,对wpf添加快捷键的方式进行了整理.主要用到的三种方式如下: 一.wpf命令: 资源中添加命令 <Window.Resources> <RoutedUICommand x:Ke ...
- 在Delphi中如何控制其它应用程序窗口
在编写Delphi的应用程序中,常常涉及对其它Windows应用程序的操作.例如,在数据库的管理系统中,财务人员需要使用计算器,即可调用Windows内含的计算器功能,若每次使用,均通过“开始/程序/ ...
随机推荐
- 芯片下载相关-CH32系列芯片下载方式汇总及教程
一.CH32Fx系列芯片下载 1.ISP下载: 2.SWD下载 3.脱机烧录器下载: 二.CH32Vx系列芯片下载 1.ISP下载: 2.SWD下载: 3.脱机烧录器下载: 4.CH32V003下载注 ...
- Element-Ui表单移除校验clearValidate和resetFields
添加和修改公用一个弹窗,点击添加弹窗后,如果没移除表单校验的话,再点击修改弹窗时校验就会被记住,所以需要移除校验,但在清空表单校验时会报如下错误: 那么,你只需要加上这段话即可 this.$nextT ...
- 关于联想对Jim博士的质疑
对Jim博士质疑的质疑 因为关注司马南,从他的空间里看到Jim博士和其龃龉,大致看了Jim博士头条里的文章,因为看到自己常用的EPICS,上午匆忙就写了上面的文. Jim博士是去年在头条上看到的,因为 ...
- 用 HTTP 协议下载资源(WinINet 实现)
用 HTTP 协议下载资源(WinINet 实现) WinINet 使用 HTTP 协议下载资源的流程 相关函数 InternetCrackUrl 解析 URL BOOL InternetCrackU ...
- 玫瑰花变蚊子血,自动化无痕浏览器对比测试,新贵PlayWright Vs 老牌Selenium,基于Python3.10
也许每一个男子全都有过这样的两个女人,至少两个.娶了红玫瑰,久而久之,红的变了墙上的一抹蚊子血,白的还是床前明月光:娶了白玫瑰,白的便是衣服上沾的一粒饭黏子,红的却是心口上一颗朱砂痣.--张爱玲< ...
- 如何获取win10用户最高权限
第五步,在(输入对象名称)方框中输入"System Managed Accounts Group",再点击"检查名称" 转载: 百度经验: https: ...
- tensorflow读书笔记
TensorFlow 是一个用于研究和生产的开放源代码机器学习库.TensorFlow 提供了各种 API,可供初学者和专家在桌面.移动.网络和云端环境下进行开发. TensorFlow是采用数据流图 ...
- docker-compose部署rocketmq
docker-compose安装: 1.从github上下载docker-compose二进制文件安装 Ubuntu下载docker-compose文件 sudo curl -L https://gi ...
- vue-fullpage全屏插件使用
直入主题:vue项目中想做一个全屏翻滚的效果,vue-fullpage 就很不错 下面介绍vue-fullpage 的使用方法,这里封装成了vue的一个指令的形式来进行使用 1.安装vue-fullp ...
- Vue学习笔记之Vue-Router
1. 概述 Vue Router 是 Vue.js 的官方路由.它与 Vue.js 核心深度集成,让用 Vue.js 构建单页应用变得轻而易举.功能包括: 嵌套路由映射 动态路由选择 模块化.基于组件 ...