Passing Structures in OLE Automation

This article was previously published under Q122289
This article has been archived. It is offered "as is" and will no longer be updated.
OLE Automation does not allow structures to be passed as parameters.Automation servers that work with standard controllers like Visual Basiccan model a structure as an automation object or pass simple structures asa safearray of VT_UI1. Automation servers that work with custom controllerscan pass structures as a safearray of VT_UI1 or by passing a data transferobject that supports IDataObject.
OLE Automation does not support passing structures as parameters toautomation methods and properties. Here are some ways to work around this:

NOTE: Standard controllers are products such as Visual Basic that cancontrol any automation server. A custom controller is written tocontrol a specific automation server.

If the Automation server is designed to work with standard controllers suchas Visual Basic, the structure could be modeled as an automation object.For example, consider this structure:
struct point {    short x;    short y;};				
This could modeled as an automation object with two properties, x and y.Visual Basic code such as the following could be used to get and set thevalues of the properties.

NOTE: The CreateLine method, which requiredtwo points as parameters, is implemented as taking references to twoautomation objects.
Dim Application As ObjectDim P1 As ObjectDim P2 As ObjectDim Line As Object:Set P1 = Application.CreatePoint()P1.x = 3P1.y = 4Set P2 = Application.CreatePoint()P2.x = 20P2.y = 21Set Line = Application.CreateLine(P1, P2)				
The CreatePoint method returns a Point automation object whose x & yproperties are set. The type of the value returned by CreatePoint isIDispatch* or a pointer to an object that supports automation. TheCreateLine method takes two parameters of type IDispatch* or a pointerto an object that supports automation.

The disadvantage of this solution is that if the automation server isnot an inproc server, each property access will result in the overheadof an LRPC/RPC call.

If the automation server is designed to work with custom controllers, thestructure could be serialized into a safearray of VT_UI1 and the resultantbinary data could be passed as a parameter of type SAFEARRAY(unsignedchar). Another solution is to create a data transfer object that supportsIDataObject. The IUnknown of this data transfer object could be passed in aparameter of type IUnknown*. The server could then use IDataObject::GetDatawith a private clipboard format to get the storage medium in which thestructure was serialized. Simple structures can be passed from Visual Basicusing a safearray of VT_UI1 as described in Don Box's OLE column in theJune 1996 issue of the Microsoft Systems Journal.

NOTE: Serializing the structure into a BSTR will not work becausethe Unicode-ANSI conversions done by OLE's 16:32 bit interoperablitylayer assumes that BSTRs contain strings and not binary data. Consequently,binary data passed in BSTRs can be corrupted by such conversions. However,serializing binary data into BSTRs will work if both controller and serverare 16-bit. It will also work if both the controller and server are 32-bitand both support Unicode. This is because 32-bit OLE supports only Unicode,not ANSI. A safearray of VT_UI is preferred because of these limitationsof passing binary data through BSTRs.
2.00 3.00

Article ID: 122289 - Last Review: 01/09/2015 05:07:24 - Revision: 2.1

Microsoft OLE 2.02, Microsoft Visual Basic 3.0 Professional Edition

  • kbnosurvey kbarchive kbautomation kbcode KB122289