Thread: Tricky #define macro...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    70

    Tricky #define macro...

    When looking up HINSTANCE (yes, I know what it means but I wanted to see the var tp) i see that its defined by DECLARE_HANDLE, a rather advanced macro:

    Code:
    #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
    Could someone take the time to explain what it means? I figured a handle would be just number (prob. Unisgned Int 32) but this is something else... ant it's not a struct with any members either..

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    DECLARE_HANDLE(HINSTANCE); will expand to:
    
    struct HINSTANCE__ { int unused; }; typedef struct HINSTANCE__ *HINSTANCE;
    Windows declares handles as pointers to dummy structures for type safety. This means the compiler can give a warning if you try to assign an HINSTANCE to another type. If it was simply declared as a void*, there would be less type safety.

    You can consider a handle as a numeric value rather than a pointer to anything. For most handle types, the meaning of the number is opaque. However, both a HINSTANCE and a HMODULE are documented as containing the base address of the module they refer to.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    70
    So of the two statements...

    Code:
    struct HINSTANCE__ { int unused; };
    typedef struct HINSTANCE__ *HINSTANCE;
    ...the first declares the dummy type "HINSTANCE__" and the second declares "HINSTANCE" (without underscores) as a pointer to a variable of that type?

    In VC6 this is also okay to the preproc:

    Code:
    struct HINSTANCE__ { int unused; };
    typedef HINSTANCE__* HINSTANCE;
    I hope I figured it out right. The first looks much trickier, because there is the word "struct" after typedef (we've already made the struct), and no space between the original type (HINSTANCE__*) and it's alias HINSTANCE. Likewise it's also clearer with int* ip than int *ip in my opinion because it follows type variable format as in int i
    Last edited by willkoh; 04-06-2005 at 10:55 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > struct name##__
    The ## is the pre-processor token pasting operator. The left token is appended with the right token.

    > Likewise it's also clearer with int* ip than int *ip in my opinion
    Until you see this
    int* p, q;
    What type is q ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> ...the first declares the dummy type "HINSTANCE__" and the second declares "HINSTANCE" (without underscores) as a pointer to a variable of that type? <<

    Yep.

    >> In VC6 this is also okay to the preproc: <<

    Only in C++. In C, the struct keyword is required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  3. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  4. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM