-
Copying Handle
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
-
Re: Copying Handle
Use the first method.
A handle to an object never changes unless the object is destroyed.
-
Re: Copying Handle
Thanks Marc.
But what is int unused, part of HANDLE member? what it is used for?
-
Re: Copying Handle
It could be used for nothing (spare for future use) or it could be something that MS don't want to be public.
-
Re: Copying Handle
OK. Thanks for clear it up. I have try find it in documentation but it is not definitely clear for me. Thanks
-
Re: Copying Handle
There are different sorts of handles used in Windows:
Code:
// wtypes.h
typedef void *HANDLE;
typedef void *HMODULE;
typedef void *HINSTANCE;
typedef void *HRGN;
Code:
// 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.