CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Copying Handle

  1. #1
    Join Date
    Jun 2010
    Posts
    34

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Copying Handle

    Use the first method.
    A handle to an object never changes unless the object is destroyed.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jun 2010
    Posts
    34

    Re: Copying Handle

    Thanks Marc.

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

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jun 2010
    Posts
    34

    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

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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
  •  





Click Here to Expand Forum to Full Width

Featured