-
July 18th, 2010, 02:30 AM
#1
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
-
July 18th, 2010, 08:16 AM
#2
Re: Copying Handle
Use the first method.
A handle to an object never changes unless the object is destroyed.
-
July 18th, 2010, 09:10 AM
#3
Re: Copying Handle
Thanks Marc.
But what is int unused, part of HANDLE member? what it is used for?
-
July 18th, 2010, 10:07 AM
#4
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.
-
July 18th, 2010, 10:42 AM
#5
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
-
July 20th, 2010, 07:58 AM
#6
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.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|