Click to See Complete Forum and Search --> : Copying Handle


Irwan
July 18th, 2010, 02:30 AM
Hi,


If I to copy a handle and store it in a data member of a class, should I:

1. copy the value such as
HWND m_hwndthiswindow = hwnd;

2. get a pointer to the terget hwnd such as
HWND *m_hwndthiswindow = &hwnd;


which one is better? does value of hwnd/hInstance/other handle structure is changing?


what is actually "int unused" defined in HANDLE structure?

Thanks

Marc G
July 18th, 2010, 08:16 AM
Use the first method.
A handle to an object never changes unless the object is destroyed.

Irwan
July 18th, 2010, 09:10 AM
Thanks Marc.

But what is int unused, part of HANDLE member? what it is used for?

S_M_A
July 18th, 2010, 10:07 AM
It could be used for nothing (spare for future use) or it could be something that MS don't want to be public.

Irwan
July 18th, 2010, 10:42 AM
OK. Thanks for clear it up. I have try find it in documentation but it is not definitely clear for me. Thanks

Igor Vartanov
July 20th, 2010, 07:58 AM
There are different sorts of handles used in Windows:
// wtypes.h
typedef void *HANDLE;

typedef void *HMODULE;

typedef void *HINSTANCE;

typedef void *HRGN;

// winnt.h
#ifdef STRICT
typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
typedef HANDLE *PHANDLE;
. . .
#ifndef WIN_INTERNAL
DECLARE_HANDLE (HWND);
DECLARE_HANDLE (HHOOK);
#ifdef WINABLE
DECLARE_HANDLE (HEVENT);
#endif
#endif
So some types of handles are just pointers to something, but other ones are some unique numbers of int size though stored as a structure type. That's the trick for not letting compiler do implicit casts to integer types or other handle types of the same size. ;)

You should remember only two things about handle: 1) it uniquely identifies some system wide known object within same object class (windows, files, module instances, etc.), and 2) it intentionally obscures the details of the identified object.