Results 1 to 11 of 11

Thread: [RESOLVED] VARIANT_BOOL: short or boolean?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,711

    Resolved [RESOLVED] VARIANT_BOOL: short or boolean?

    So I'm working with some typelib interfaces that use VARIANT_BOOL, which comes up as unknown type when compiling. So I searched, and the official definition (WTypes.idl in the SDK) and an article by Raymond Chen all have it as 'typedef short VARIANT_BOOL' which of course then comes up in VB as an Integer. But then in the wild, in a system tlb using the interface, it's showing as a Boolean (and there's no typedef or dependency where a typedef might be, so I have no idea what it's aliased as). So which is it? Should I alias it to a short, or alias it to a boolean?

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: VARIANT_BOOL: short or boolean?

    Hi fafalone,

    I'm not sure if this helps or not, but a VB6 Boolean is just a special case of an Integer. It's two bytes.

    Now, in most cases, when you set (Let, assign) a Boolean, VB6 will do some behind-the-scenes work to turn it to an Integer -1 (i.e., all bits on). However, (and I'd have to play with it to remember how), you can get any bit-pattern (up to 16 bits) into it.

    Also, as I'm sure you also know, it differs from a C-type boolean in that C just turns the low-order bit on (leaving the rest off), resulting in C booleans being either 0 or 1 (rather than 0 or -1). Maybe that'll help, but not sure.

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: VARIANT_BOOL: short or boolean?

    Also, according to this page, a VARIANT_BOOL is a short.

    Technically, except for type mismatch errors, I wouldn't think there was much difference between a short, a Boolean(vb6), or an Integer(vb6). However, a c bool is typically only one byte, so that's different.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: VARIANT_BOOL: short or boolean?

    If you're using MIDL, it shouldn't have a problem recognizing VARIANT_BOOL. If you're using MKTYPLIB however, then yes, it doesn't recognize that type. In that case, you might want to try this:

    Code:
    typedef boolean VARIANT_BOOL;
    The above type mapping comes from this table from the book Inside COM+: Base Services.


    BTW, this might be of interest to someone: BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool
    Last edited by Victor Bravo VI; Jul 31st, 2018 at 06:34 PM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,711

    Re: VARIANT_BOOL: short or boolean?

    Yeah still using MKTYPLIB; it's entirely impossible for me to run midl, it endlessly requests files not involved with my project, and no matter what file versions get paired with what midl version, it does nothing but spew errors.

    I know it's an integer VB uses under the hood; just wanted to be sure the 'boolean' type in typelib compiler was compatible with that and in turn compatible with the interfaces (particularly because these are ones where you use a default implementation from Windows).

    So looks like it's safe to go ahead and alias it to Boolean. (and yeah, BOOL has been handled fine since day 1, good to have the info in the thread tho)

    Thanks guys.

  6. #6
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    yeah, since I stick to MIDL i thought VARIANT_BOOL _is_ VB6 Boolean. What do you use to make a VB6 Boolean in MKTYPLIB?

  7. #7
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    As I showed above, it's boolean (for those who are not aware, note that it must be small letter 'b', since IDL/ODL syntax is borrowed from C and is case-sensitive as well). I've actually never had to use that data type before since VB's Boolean data type seems to be rarely, if at all, used in Windows API functions. Apparently, it does get utilized in some (Shell?) interface, so that's something new for me.

    I did experimented with a newly created TLB containing a UDT and a function that uses that typedef and I found out that, yes, the type mapping does work as expected, except for one minor issue: when hitting the '=' key after the UDT's Boolean member, the IntelliSense list doesn't pop up. I don't know why, but I guess it doesn't really matter since everything else works as expected. VB6's Watch window sees it as a real Boolean and Boolean operations with it works just fine.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    Quote Originally Posted by Victor Bravo VI View Post
    Boolean operations
    Just a bit off-topic, but a pet-peeve of mine ... VB6 has no boolean operations except for the Let statement into a Boolean declared variable. All other operations (excluding the equal sign) are either arithmetic or bitwise. You have to think about it, but here's some code that proves it:

    Code:
    
        Dim b As Boolean
        b = True
        b = (b And 8) And 4
        Debug.Print b
    
    Throw that into Form_Load and see what happens. Now, just to think it through, the b is set to non-zero, and the value of 8 is non-zero, and the value of 4 is non-zero, so how'd we wind up at FALSE?

    And, just to show some equivalent C code:

    Code:
        bool b;
        b = 1;  // Or anything non-zero, even -1 if you like.
        b = (b && 8) && 4;
        printf("%B\n", b);
    Now, if you execute that, you will wind up at TRUE, because the && operator is a true boolean operator.

    And, just to be explicit about what's happening with that VB6 code, the (b And 8) And 4 expression is going to see b as -1, and then bitwise AND the 4 (resulting in 4), and then bitwise AND the 8 (resulting in 0).

    It'd be super cool if VB6 actually had boolean operators, but we make due, realizing the traps with what we have.

    Best Regards,
    Elroy
    Last edited by Elroy; Aug 2nd, 2018 at 08:34 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,711

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    Quote Originally Posted by DEXWERX View Post
    yeah, since I stick to MIDL i thought VARIANT_BOOL _is_ VB6 Boolean. What do you use to make a VB6 Boolean in MKTYPLIB?
    typedef boolean VARIANT_BOOL;

    If you want to retain the VARIANT_BOOL name but have VB still treat it like a native Boolean you can do typedef [public] boolean VARIANT_BOOL; like the BOOL type in olelib/oleexp, which is treated as a Long.



    @Victor Bravo VI yeah it's my first time encountering it as well, despite having worked with hundreds of shell interfaces and obscure APIs. It came up in the Task Scheduler 2.0 interfaces.
    Last edited by fafalone; Aug 2nd, 2018 at 10:23 PM.

  10. #10
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    @Elroy

    I'm actually aware that VB6 has no true Boolean operators, but I think most people understood what I meant by "Boolean operations" (i.e., expressions involving VB6's And, Or, Xor, etc. operators).

    If we want to get pedantic about it though, VB6's And, Or, Xor, etc. operators aren't really purely bitwise operators either. Consider these:

    Code:
    ? True And Null
    Null
    
    ? False Or Null
    Null
    The Null value is not a number, so bitwise operations can't be done on it.

  11. #11
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: [RESOLVED] VARIANT_BOOL: short or boolean?

    Null value is any value or no value and it's processed like other bitwise value.

    Code:
    (AnyValue) And False = False (0);
    (AnyValue) And True = AnyValue (Null);
    (AnyValue) Or False = AnyValue (Null);
    (AnyValue) Or True = True (-1);
    http://www.vbforums.com/showthread.p...=1#post4967489

    And is always works with bits (bitwise) even if you do CBool or something like this. When you use bollean variables it means true = 0b1111111111111111 = -1 (two bytes signed integer) and false = 0. Just an return value is being casted to bigger type when expression is performed.

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