Link to home
Create AccountLog in
Microsoft Development

Microsoft Development

--

Questions

--

Followers

Top Experts

Avatar of cinder_tian
cinder_tian

What's on earth the meaning of handle?
 I'm beginning to learn program.I don't understand
the meaning of "handle".is it a pointer or address?
Could you tell me more about handle?Thank you!

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of FengYuanFengYuan

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of MadshiMadshi

Some additions to FengYuan's comment:

There are (at least) 3 different kind of handles in windows:

- GDI handles
- Kernel/System handles
- User handles

GDI handles are already explained by FengYuan in depth. User handles are Window handles and such. Then we have Kernel/System handles, which are all those kind of handles which you can close with "CloseHandle". Examples are process handles, thread handles, file handles, event handles, socket handles and much more. A kernel handle is something like an index into an internal handle table of the current process (that means a kernel handle is only valid in one process). A kernel handle points to a specific kernel object and grants access to this object with specific access rights. You can have several handles for the same kernel object, each handle with a different (or with the same) access mask. Some objects get destroyed in the moment when the last handle to them is closed (e.g. events, mutexes and such), others do not (e.g. processes/threads).
In the win9x family handles are a restricted resource. E.g. when you look at the CreateWindow documentation, you'll see that win9x only supports 16K user handles. So in win9x you have to be very careful about handles. In winNT handles are not restricted.
When your process terminates (regardless whether crashing or not), Windows closes all kernel and user handles that you have opened. I'm not sure about GDI handles. FengYuan?

Regards, Madshi.

To make it a little more simple -

If you want to write data to a file on disk, you only refer to the file by its real name once, when you open it.  The action of opening the file means Windows gives a handle to the open file - a handle is a pointer to the open file, stored internally in windows.  Whenever you read or write to the open file, you don't use the file name, you just use the handle that windows gave you instead, and Windows handles moving the data on and off the disk.

Similar is when you have asked Windows to create a new window for you, or another object.  Windows handles the manipulation of the object for you, and you just refer to the object by the handle Windows gave you when you first opened/created the object.

Madshi, GDI handles will be deleted when a process terminates.

On Windows NT/2000, GDI/USER handles still have system-wide limitation, because of its table-based implementation. GDI handles are still limited to 16384 handles.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


>> Madshi, GDI handles will be deleted when a process terminates.

Thanx for the info.

>> On Windows NT/2000, GDI/USER handles still have system-wide limitation, because of its table-based implementation. GDI handles are still limited to 16384 handles.

:-(

Think of a HANDLE as a random number stamped onto something so later on when you see that thing you can recognize it by comparing the number on it and the number you remember.

A handle is used to identify an object by windows. It is a number used internally by windows. Such numbers are #defined in "Windows.h" and other header files used by windows.

Don't worry about the internal representation of handles.


eg: you may create more thon one windows( I mean a window that appears on screen) in your program. Now you wish to draw on a window. which window???. How do u tell windows Operating system to draw on window-1 of the program??.
When you give the handle of window-1 to say a function like TextOut text is displayed in window-1. If u give handle of window-2 text will be drawn on window-2.


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I made a mistake while giving the previous comment. Kindly
ignore it. Here is th rectified comment.

A handle is used to identify an object by windows. It is a number used internally by windows. Such numbers
are #defined in "Windows.h" and other header files used by windows.

Don't worry about the internal representation of handles.
It is not important as it is used internally by windows.

eg: you may create more thon one windows( I mean a window that appears on screen) in your program. Now
you wish to draw on a window. which window???. How do u tell windows Operating system to draw on window-1
of the program??.
When you give the handle of window-1 to say a function like GetDC you get a device context for window-1 and can now draw on window-1 using a function like TextOut.
If u give handle of window-2 to GetDC you will get device context for window-2 and can output text/anything else you like on window-2.


A Handle is a numeric value that references exactly one windows object at one time. Handle is returned to the caller application by the windows. Suppose you create a windows object 'X'. Windows allocates memory for the new object. In future, for efficient memory handling Windows might want to move the memoru chunk allocated to the new object to some other location. This activity changes the object's address value ( pointer ). To make this process transparent to the application, windows maintains handles.
It is internally managed as a hashtable like structure having (handle,pointer-address) as key-value pairs.

Handles can refer to any Windows object's like GDI or USER etc.

Hope this helps you.

This is NO hash-table like structure to translate a handle in Windows to an object. Part of handle is an index.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


If this proposed answer has helped you, please accept it, grade it and close this question.  If it is not, reject it and add comments as to your progress and what else is needed.

Thanks,
Moondancer
Community Support Moderator @ Experts Exchange

Hi Moondancer,

in case you will close this question, you should not give the points to onkarsingh, because FengYuan and me both gave more info than he, and that before him. You can give the points to FengYuan, that's okay for me...

Regards, Madshi.

P.S: onkarsingh is a bit right, but quite a bit wrong also. In win9x for system/kernel handles there really is a process wide table, which has one object pointer for each handle, but besides also an access field, furthermore the system/kernel object pointers do not change in win9x. The meaning of win9x system handles is to combine an object with a specified access mask. It's different for other handle types. See all the previous comments.

Perhaps a point split is in order here, where both FengYuan and you share the points equally.  I'd like to give Asker a few days to respond before taking next action, and thank you for your input and patience.  I am sorry each time I see questions asked, help provided only to have the questions abandoned.  Hopefully this activity via comments will bring Asker back to finalize this.

Moondancer
Community Support Moderator @ Experts Exchange

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Points for Madshi in this topic area here:  qid=20249798
Please comment in that question to complete this point split award.

Thanks.

Moondancer
Community Support Moderator @ Experts Exchange
Microsoft Development

Microsoft Development

--

Questions

--

Followers

Top Experts

Most development for the Microsoft platform is done utilizing the technologies supported by the.NET framework. Other development is done using Visual Basic for Applications (VBA) for programs like Access, Excel, Word and Outlook, with PowerShell for scripting, or with SQL for large databases.