Paul Knopf
Paul Knopf

Reputation: 9786

How to convert VARIANT_BOOL* to bool*

I found this question, but they aren't using a pointer.

I have a method that my COM method calls that requires a pointer to a bool. This bool is used to cancel the long running process (video recording, if you must know) from the caller.

Can I cast it somehow, but still have VARIANT_BOOL equate to FALSE and vice-versa?

Code

HRESULT YourObject::YourComMethod(VARIANT_BOOL* pVarBool)
{
    callYourNonComMethodHere((bool*)pVarBool);
    return S_OK;
}

UPDATE

So, I learned that, although you can have COM parameters as pointers, but they serve no purpose. That don't actually reference the pointer from the caller.

So, what I had to do was make a totally separate COM object that is my CancellationToken. Then, in my implementation, I can call token.Cancel(), and then the YourComMethod will have the updated bool, indicating that the long running method should cancel.

I created a sample project to demonstrate how I was able to pass a pointer to a bool, using a wrapping COM object.

https://bitbucket.org/theonlylawislove/so-blocking-com-call/src

Upvotes: 1

Views: 1521

Answers (1)

WhozCraig
WhozCraig

Reputation: 66234

VARIANT_BOOL is supposed to be one of two values if done right. VARIANT_TRUE or VARIANT_FALSE. On the off chance some neanderthal doesn't understand that with all the buggy COM code out there I generally assume if it isn't VARIANT_FALSE, then it must be true.

So:

HRESULT YourObject::YourComMethod(VARIANT_BOOL* pVarBool)
{
    bool bval = (*pVarBool != VARIANT_FALSE); // do this if pVarBool is [in,out]
    // bool bval = false; // otherwise do this if pVarBool is [out] only.

    callYourNonComMethodHere(&bval);
    *pVarBool = bval ? VARIANT_TRUE : VARIANT_FALSE;
    return S_OK;
}

After a little clarification from Matt, I think that is what you were trying to do. Or something likely close to it.

Upvotes: 4

Related Questions