Friday, February 13, 2004

VARIANT_BOOL hint

When you are writing COM objects with support for OLE Automation types, you may have to deal with VARIANT_BOOL types. They are boolean values but unlink BOOL and bool its legal values are -1 (VARIANT_TRUE) and 0 (VARIANT_FALSE).

That means that if you call by mistake one of those methode with a bool or BOOL value, it will be implicitily converted to 0 and 1 values making it a perfectly legal call, but if you do have something like:

 

HRESULT foocall(VARIANT_BOOL boolParam)

{

    if( boolParam == VARIANT_TRUE )

        return S_OK;

    return S_FALSE;

}

 

Than you are in big trouble if you mismatch what you pass to the method. When you are dealing with VARIANT_BOOL types is always better to check for VARIANT_FALSE which is always 0 as for bool and BOOL types, so you should write the previous method like:

 

HRESULT foocall(VARIANT_BOOL boolParam)

{

    if( boolParam == VARIANT_FALSE )

        return S_FALSE;

    return S_OK;

}

 

No comments: