I stumbled upon another what I think is initializer, like int and char, named HANDLE.
It's also used when creating an application, I've browsed the net and found that it is a sort of pointer to an object that can change without me knowing it... eh... It wasn't really that good of an explanation. Can anyone explain it a bit more... dumbed down for me? Giving examples usually helps.

int integer;
char character;
HANDLE handle;

Questions:

  1. What does it do?
  2. How can I use this effectively in a code that doesn't have to do with windows itself? and by that I mean, can I use a handle on one of my own objects or variables or whatever it does?
  3. Can it be used to "point" to a class or a class member? If yes, please give an example

Answer should (doesn't have to) include/have/be:

  • an example
  • explained, if possible, line by line

Sidenote: I've seen this HANDLE used in another code called JASS (Blizz code), where the handle could be a timer, unit, trigger, effect, anything that is an object. If you'd rather explain it through JASS, I won't mind (It's easier to understand Blizz's dumbed down language).
Sidenote: Hope I was clear enough for people to understand. :)

Recommended Answers

All 4 Replies

Is it a Windows handle, the one in windows.h? It's not an initializer, it's what int and char are: a type.

If it's a window handle, it's basically as specific as a pointer. A pointer used by windows to keep track of resources, varying from windows (window handles) to files (file handles).

Here's a bit more info on windows variables, including HANDLE: http://en.wikibooks.org/wiki/Windows_Programming/Handles_and_Data_Types

hm, need more easy to read examples... about this thing, that's what's wrong with most of these things, all they do is blabber on about the most general things, but I have found that it is easier practice to see it in action, much like the cplusplus.com site. I have yet to find an explanation on that site about HANDLEs tho :( (probably blind)

Windows has a table of resources (Windows, buttons, icons, mouse pointers, menus, etc.)
Each resource has an unique ID.
With that ID, you can get that resource, and functions that create a new resource return such an ID.
The simplest example being

HANDLE WINAPI CreateFile(
  __in      LPCTSTR lpFileName,
  __in      DWORD dwDesiredAccess,
  __in      DWORD dwShareMode,
  __in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  __in      DWORD dwCreationDisposition,
  __in      DWORD dwFlagsAndAttributes,
  __in_opt  HANDLE hTemplateFile
);

Also on http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

This function creates a resource (a file) and gives you its ID, so you can pass that to ReadFile() for example:

BOOL WINAPI ReadFile(
  __in         HANDLE hFile,
  __out        LPVOID lpBuffer,
  __in         DWORD nNumberOfBytesToRead,
  __out_opt    LPDWORD lpNumberOfBytesRead,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

Second param. :)

So, in this case, HANDLE is like C's FILE*: An unique ID for a file.

Ah, this explains alot, I'll bookmark that page, and this page for future reference! Although, I know little about the API, but not enough to understand anything of what you said, apart from the unique ID for the file, which is highly desireable I believe? Cause for this project I'm doing I REALLY need ID's for all the monsters spawned, so that I can then refer back to the specific monster. Altho I guess if you have a static monster, you can just get his location and you're done. So, maybe you can create static objects in windows? :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.