Data Access Worldwide Knowledge Base

Article ID 1990
Article Title HOWTO: Work with Functions that Return COM Objects
Article URL http://www.dataaccess.com/kbasepublic/kbprint.asp?ArticleID=1990
KBase Category VDF (GENERAL)
Date Created 04/05/2004
Last Edit Date 07/12/2005


Article Text
QUESTION:
We installed this component - MaXC (Matrix ActiveX Component) www.bluebit.gr - and imported it into Visual DataFlex 9 using the "Import Automation Library" option from the Studio.

It works very well, but there are several methods (for instance: "Clone" - to make a copy from one matrix to another) that return a "Matrix" object. Example (in Visual Basic):

(VB) Dim Output_MxN as Matrix


In the package created by the Studio, the "Clone" method is declared as:

    // Creates a duplicate Matrix object.
    Function ComClone Returns Variant
        Handle hDispatchDriver
        Variant retVal
        Get phDispatchDriver to hDispatchDriver
        Get InvokeComMethod of hDispatchDriver 5 OLE_VT_DISPATCH to retVal
        Function_Return retVal
    End_Function


I was not able to use that method -- or others of the same kind -- in my application; the VDF runtime complains. What is the correct way of calling that method?


ANSWER:
The function returns a COM object wrapped in a Variant type, and you can then attach the COM object to a VDF object. This is explained in more detail in the Visual DataFlex documentation under

- FlexCOM Developer's Guide | Programming with COM - General Topics | Variant Data Types:

Using Variants to Store IDispatch* Values
It is important that COM object references (IDispatch* values) are controlled only by Variant types in order to maintain automatic reference counting of COM objects. Changing a Variant variable that contains a COM object reference or setting it to NULL (using the NullComObject function) automatically releases any reference to the COM object pointed to by the old value.

When Variant variables go out of scope they delete their contents automatically. In the case of a Variant containing an IDispatch*, the referenced COM object is released. Thus the reference count of the COM object is automatically decremented when a variant goes out of scope.

Variant variables containing IDispatch* values can not be converted to any other data type. The VDF runtime protects against this and will raise an error ("Illegal type conversion"). Such a conversion is meaningless and, if allowed, could subvert VDF's reference counting mechanism for COM objects.

The VDF runtime also protects against pointer arithmetic and comparison expressions for variant variables containing an IDispatch*. Doing so would also raise a runtime error. The helper functions IsNullComObject and IsSameComObject can be used to perform any operations that you would normally require on variants containing IDispatch* values.



- FlexCOM Developer's Guide | Programming with COM - General Topics | Using pvComObject:

pvComObject is a variant property which returns a reference to the external COM object in the form of an IDispatch pointer.

Setting this property to the correct IDispatch* value connects a VDF wrapper object to its corresponding external COM object and increments the COM object's reference count.



- FlexCOM Developer's Guide | Programming with Automation Objects | Aggregate Objects:

Many COM objects are actually composed of a structure of COM objects. In COM this is known as aggregation. Aggregation means that the containing (outer) object creates the contained (inner) object(s) as part of its creation process, and the interfaces of the inner object are exposed by the outer.



Basically you would need to do something like this:

Object oComMatrixOrg1 is a cComMatrix
End_Object

Object oComMatrixOrg2 is a cComMatrix
End_Object
...
Variant vMatrixClone
Get ComClone of oComMatrixOrg1 to vMatrixClone

Set pvComObject of oComMatrixOrg2 to vMatrixClone

Send xyz to oComMatrixOrg2

The ComClone function returns a Variant containg the COM object. So, you assign that to the Variant variable vMatrixClone, and then you can set the pvComObject property of a VDF object to the variable. After that, you should be able to work with the VDF object as usual.




Contributed By:
Sonny Falk
Company: Data Access Worldwide
Web Site: http://www.dataaccess.com


Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2010 Data Access Corporation. All rights reserved.

The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.