What is a HANDLE variable in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is a HANDLE variable in C++?

Whats a HANDLE variation in C++? Explain with examples please. Thanks!

15th May 2017, 11:38 PM
iPower
iPower - avatar
16 Answers
+ 3
Yes.
16th May 2017, 11:53 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
A Handle is an opaque type. In an OS , it is used to get a reference to an object. Basically, it is just a key an OS uses to identify all objects contained in it... Every handle contains a numeric value, which is stored in a large table that the OS uses... This number is now used to identify the object from the table and change its attributes and properties... There are several handles for streams, windows, buttons, instances of programs, etc. These simply help the operating system have a better access to objects, as this process is safer than direct referencing... Eg: HANDLE hcout = GetStdHandle(STD_OUTPUT_HANDLE); //Now, we saved the handle to the output stream in a hcout Handle variable... //Now we can alter its properties! SetConsoleTextAttribute(hcout,FOREGROUND_RED); //Passing our handle to SetConsoleTextAttribute, which helps change color of text in console... cout<<"Hello World!"<<endl; //Prints Hello World! in Red! Similarly, one can create Handles for Custom Windows, Buttons, Files, etc... There also exist handles for instances, renamed as HINSTANCE...
16th May 2017, 3:50 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
OS = Operating System Sorry I didn't mention that.
16th May 2017, 11:41 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
Yes. Thats what it is, in a nutshell.
16th May 2017, 11:43 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
The handle returned by the OpenProcess function can be used in any function that requires a handle to a process, such as the wait functions, provided the appropriate access rights were requested. Most of Windows Functions work with Handles...
16th May 2017, 11:45 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
OpenProcess opens a local Process... HANDLE WINAPI OpenProcess( _In_ DWORD dwDesiredAccess, _In_ BOOL  bInheritHandle, _In_ DWORD dwProcessId ); And returns a handle to it...
16th May 2017, 11:47 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Thanks!
16th May 2017, 4:55 PM
iPower
iPower - avatar
+ 1
one thing i didnt understand. whats the meaning of OS?
16th May 2017, 5:19 PM
iPower
iPower - avatar
+ 1
and btw. what im actually doing in this line: HANDLE aa = OpenProcess(PROCESS_ALL_ACESS, FALSE, id); why is HANDLE necessary here?
16th May 2017, 5:35 PM
iPower
iPower - avatar
+ 1
Ooh i guess i understood. Is it like a "key" to manipulate an specific object in windows? Like a process?
16th May 2017, 6:13 PM
iPower
iPower - avatar
+ 1
So its like a variable that returns a number and i can use this number to manipulate my object, right?
16th May 2017, 11:43 PM
iPower
iPower - avatar
+ 1
thank you very much!
16th May 2017, 11:44 PM
iPower
iPower - avatar
+ 1
I see. So the number returned is like the "process" reference
16th May 2017, 11:46 PM
iPower
iPower - avatar
+ 1
and the handle is the object(process) i can modify right
16th May 2017, 11:48 PM
iPower
iPower - avatar
+ 1
i mean the number returned (HANDLE) by OpenProcess is the object(process) i can modify. Is that right?
16th May 2017, 11:52 PM
iPower
iPower - avatar
+ 1
Ok. Thank you very much again!! SoloLearn's community is amazing!
16th May 2017, 11:54 PM
iPower
iPower - avatar