句柄 Handle 的通俗解析

句柄 :相当于一个编号,可以唯一标识一个对象、资源、服务。
起类似对象指针的作用,创建句柄就是构造,销毁句柄就是析构,用句柄调用函数相当于传入this指针。
句柄是操作系统的一种引用。它没有编程参考的语义,但它做的是让系统资源知道在API调用中传递时所指的内容。

handle 最初被微软翻译为“把手”。握住“把手”就可以唯一控制这个“门”。
后来被翻译成蹩脚的“句柄”……
解释分析自下文:

What is a handle in Windows Programming?

A handle is a reference for the operating system. It does not have the semantics of a programming reference but what it does do is allow the system resources to know what you are referring to when it is passed in an API call. Usually, the HANDLE is wrapped in an instance of a class. CWnd is a good example, it contains an HWND which is a handle to a window. You can do this. CWnd *pWnd = CWnd::FromHandle(hWnd) Note: that CWnd::FromHandle(hWnd) is static and does not require an instance. It will pass you back the wrapper that the hWnd is wrapped by. Well not quite! If the handle is not actually wrapped by an object it will create one AND IT WILL ONLY BE TEMPORARY.So use it the once then throw it away. It can create the instance because the hWnd has enough information in its struct for windows to instantiate a CWnd object. It does not add it to the handle v object table, so it is only temporary. The HWND is in fact a kernel object and theres more ? HWND (CWnd and CWnd-derived classes) HDC (CDC and CDC-derived classes) HMENU (CMenu) HPEN (CGdiObject) HBRUSH (CGdiObject) HFONT (CGdiObject) HBITMAP (CGdiObject) HPALETTE (CGdiObject) HRGN (CGdiObject) HIMAGELIST (CImageList) SOCKET (CSocket) (Should have been HSOCKET?) + others. I am not sure if all of these would pass back a temporary object if required. GetDC(hWnd) will get you a hDC from an hWnd but it will be temporary, probably better to use the CDC claa. It may be a fundamental requirement for windows programming? For a proper explanation look up google with this "Inside MFC: Handle Maps and Temporary Objects"
句柄是操作系统的一种引用。它没有编程参考的语义,但它做的是让系统资源知道在API调用中传递时所指的内容。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯岭决

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值