user565660
user565660

Reputation: 1191

How to pass address of BOOL to a method accepting address of VARIANT_BOOL?

I have a method which takes a parameter of VARIANT_BOOL pointer

STDMETHOD(get_Test)(VARIANT_BOOL* result) 

I also have a BOOL test variable I want to pass into that method. It has to be BOOL and not VARIANT_BOOL.

BOOL test;

Is there any way pass address of BOOL in place of address of VARIANT_BOOL? I tried

get_Test( &((VARIANT_BOOL)test));

but that did not work.

Upvotes: 2

Views: 365

Answers (2)

Hans Passant
Hans Passant

Reputation: 942020

Those types have different sizes and use different values for TRUE. The cast could work if you initialize the value so the upper word is zero. But just don't and do it like this instead:

VARIANT_BOOL temp;
HRESULT hr = get_Test(&temp);
if (SUCCEEDED(hr)) {
    BOOL test = temp != VARIANT_FALSE;
    // etc...
}

Upvotes: 4

sharptooth
sharptooth

Reputation: 170509

The solution is that you should not use a cast for this because BOOL is typedefed to int and VARIANT_BOOL is typedefed to short and so they most likely will have different sizes. Declare a VARIANT_BOOL variable and pass its address, then interpret the result.

Upvotes: 3

Related Questions