Windows的所有句柄类型定义
qq:9611153 微信lgs9611153
时间:2012-11-21
问题相关:
包含4个内容:常见句柄类型,全部句柄及描述,句柄函数宏定义,MFC中GDI类使用
常见句柄类型:
文件:ntdll.h
类型:OB_TYPE_TYPE
句柄类型 说明
HBITMP:标示位图句柄
HBRUSH:标示画刷句柄
HCOURSOR:标示鼠标光标句柄
HDC:标示设备环境句柄
HFONT:标示字体句柄
HICON:标示图标句柄
HINSTANCE:标示当前实例句柄
HMENU:标示菜单句柄
HPALETTE:颜色调色板句柄
HPEN:标示画笔句柄
HWND:标示窗口句柄
HFILE:标示文件句柄
全部句柄及描述:
Object Description
Access token
The handle is returned by the CreateRestrictedToken, DuplicateToken, DuplicateTokenEx, OpenProcessToken, or OpenThreadToken function.
Change notification
The handle is returned by the FindFirstChangeNotification function.
Communications device
The handle is returned by the CreateFile function.
Console input
The handle is returned by the CreateFile function when CONIN$ is specified, or by the GetStdHandle function when STD_INPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.
Console screen buffer
The handle is returned by the CreateFile function when CONOUT$ is specified, or by the GetStdHandle function when STD_OUTPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.
Desktop
The handle is returned by the GetThreadDesktop function.
Event
The handle is returned by the CreateEvent or OpenEvent function.
File
The handle is returned by the CreateFile function.
File mapping
The handle is returned by the CreateFileMapping function.
Job
The handle is returned by the CreateJobObject function.
Mailslot
The handle is returned by the CreateMailslot function.
Mutex
The handle is returned by the CreateMutex or OpenMutex function.
Pipe
A named pipe handle is returned by the CreateNamedPipe or CreateFile function. An anonymous pipe handle is returned by the CreatePipe function.
Process
The handle is returned by the CreateProcess, GetCurrentProcess, or OpenProcess function.
Registry key
The handle is returned by the RegCreateKey, RegCreateKeyEx, RegOpenKey, or RegOpenKeyEx function. Note that registry key handles returned by the RegConnectRegistry function cannot be used in a call to DuplicateHandle.
Windows Me/98/95: You cannot use DuplicateHandle to duplicate registry key handles.
Semaphore
The handle is returned by the CreateSemaphore or OpenSemaphore function.
Socket
The handle is returned by the socket or accept function.
Thread
The handle is returned by the CreateProcess, CreateThread, CreateRemoteThread, or GetCurrentThread function
Timer
The handle is returned by the CreateWaitableTimer or OpenWaitableTimer function.
Window station
The handle is returned by the GetProcessWindowStation function.
句柄函数宏定义:
很多宏看上去很形象,但实际上隐藏了细节
文件:windowsx.h
/****** KERNEL Macro APIs ****************************************************/
#define GetInstanceModule(hInstance) (HMODULE)(hInstance)
#define GlobalPtrHandle(lp) ((HGLOBAL)GlobalHandle(lp))
#define GlobalLockPtr(lp) ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
#define GlobalUnlockPtr(lp) GlobalUnlock(GlobalPtrHandle(lp))
#define GlobalAllocPtr(flags, cb) (GlobalLock(GlobalAlloc((flags), (cb))))
#define GlobalReAllocPtr(lp, cbNew, flags) (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
#define GlobalFreePtr(lp) (GlobalUnlockPtr(lp), (BOOL)(ULONG_PTR)GlobalFree(GlobalPtrHandle(lp)))
/****** GDI Macro APIs *******************************************************/
#define DeletePen(hpen) DeleteObject((HGDIOBJ)(HPEN)(hpen))
#define SelectPen(hdc, hpen) ((HPEN)SelectObject((hdc), (HGDIOBJ)(HPEN)(hpen)))
#define GetStockPen(i) ((HPEN)GetStockObject(i))
#define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
#define SelectBrush(hdc, hbr) ((HBRUSH)SelectObject((hdc), (HGDIOBJ)(HBRUSH)(hbr)))
#define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
#define DeleteRgn(hrgn) DeleteObject((HGDIOBJ)(HRGN)(hrgn))
#define CopyRgn(hrgnDst, hrgnSrc) CombineRgn(hrgnDst, hrgnSrc, 0, RGN_COPY)
#define IntersectRgn(hrgnResult, hrgnA, hrgnB) CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_AND)
#define SubtractRgn(hrgnResult, hrgnA, hrgnB) CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_DIFF)
#define UnionRgn(hrgnResult, hrgnA, hrgnB) CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_OR)
#define XorRgn(hrgnResult, hrgnA, hrgnB) CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_XOR)
#define DeletePalette(hpal) DeleteObject((HGDIOBJ)(HPALETTE)(hpal))
#define DeleteFont(hfont) DeleteObject((HGDIOBJ)(HFONT)(hfont))
#define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
#define GetStockFont(i) ((HFONT)GetStockObject(i))
#define DeleteBitmap(hbm) DeleteObject((HGDIOBJ)(HBITMAP)(hbm))
#define SelectBitmap(hdc, hbm) ((HBITMAP)SelectObject((hdc), (HGDIOBJ)(HBITMAP)(hbm)))
#define InsetRect(lprc, dx, dy) InflateRect((lprc), -(dx), -(dy))
...
MFC的CGdiObject对象不需要手动释放句柄:
对于MFC的CGdiObject对象,并不需要手动释放,离栈自动释放句柄。例如
CBitmap BmpSlider;
BmpSlider.LoadBitmap(IDB_TOOLBK);
//BmpSlider.DeleteObject();//不需要手动删除对象,CBitmap离栈自动释放句柄
代码跟踪:
class CBitmap : public CGdiObject
class CGdiObject : public CObject
_AFXWIN_INLINE CGdiObject::~CGdiObject()
{
AFX_BEGIN_DESTRUCTOR
DeleteObject();
AFX_END_DESTRUCTOR
}