1

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

Community
  • 1
  • 1
Paul Knopf
  • 9,568
  • 23
  • 77
  • 142
  • Could you elaborate with a simple code sample please? – πάντα ῥεῖ May 03 '14 at 23:39
  • 1
    I hate `VARIANT_BOOL`, because template code can't distinguish it from `unsigned short` , so auto-generated COM implementations end up sending `VT_I2` as the type instead of `VT_BOOL` and some clients (e.g. VB6) reject this as an incompatible type – M.M May 04 '14 at 02:32
  • Compare what you get back to 0, NULL or any equivalent. Never compare to something else (1, -1, not NULL, not zero, etc.). In binary form, I would say what you get back, whatever its type of size, must not have any binary digit set to 1. – Simon Mourier May 04 '14 at 06:33
  • Next time you ask anything like that a short code snippet is really welcome - simplified declarations of the two methods in question would make the question much clearer. – sharptooth May 05 '14 at 06:53

1 Answers1

4

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.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • I think you understood the question correctly, except that his function is `T func(VARIANT_BOOL *pvb)` – M.M May 04 '14 at 02:32
  • @MattMcNabb It was the "method that my COM method calls..." which made me think "ok, he's calling a non-COM method from a COM method that was given a VARIANT_BOOL and the non-COM method being called takes a `bool` by-address." Still not entirely sure at this point. but I think I get it; I left out swapping back the result. Will update. thanks for helping clarify in my head. – WhozCraig May 04 '14 at 02:41
  • Based on his description it should be an `[in]` parameter so you wouldn't need to swap back the value afterwards. But it couldn't hurt all the same. – M.M May 04 '14 at 02:43
  • @MattMcNabb then why the f' would it be passed by-ref ?? odd. Anyway, updating. thanks so much for the help. – WhozCraig May 04 '14 at 02:47
  • Check my updated question, it describes how I was able to pass a reference bool to a COM method, using a wrapping COM object instance. – Paul Knopf May 05 '14 at 17:18