Understanding Parameter Direction
The IVI specifications prescribe specific rules for output parameters in IVI-COM drivers. These rules often lead to confusion — both for the driver developer and for the end-user working with an IVI driver. This topic is dedicated to explaining output parameters in IVI-COM drivers.
IVI Requirements for Output Parameters
Section titled “IVI Requirements for Output Parameters”The IVI specifications require that all output parameters in IVI-COM drivers be specified as [in, out] when constructing the driver IDL file. An [in,out] parameter in IVI-COM is also referred to as a reference parameter (ref in C#) or a two-way parameter. The rule prohibiting [out]-only parameters holds even if the parameter is logically a pure output parameter — such as with the Fetch function in IviScope, which returns an array of data from the device. The only exception to this rule is when the parameter is a method return value and marked as [out, retval] in the IDL. Thus there are no “pure” output parameters in IVI-COM drivers.
The reason this rule exists is historical — to preserve compatibility with Visual Basic 6, which was the dominant Microsoft development tool at the time the original IVI-COM specifications were authored. Visual Basic 6 has a well-documented flaw whereby it leaks memory when using a COM component that has [out]-only parameters. The solution has always been to avoid [out]-only parameters in any component targeting Visual Basic 6, and to use [in, out] instead.
Using IVI Drivers with Output Parameters
Section titled “Using IVI Drivers with Output Parameters”The consequences of using [in, out] for all output parameters in IVI-COM drivers is quite visible. Consider the following IDL signature of the FetchWaveform method from the IviScope class-specification:
HRESULT FetchWaveform([in, out] SAFEARRAY(double)* WaveformArray, [in, out] double* InitialX, [in, out] double* XIncrement);As the name of the method implies, the parameters are all logically output parameters. However, as per the IVI specifications, each is specified as [in, out].
When using such a method from C#, each [in, out] parameter will appear as a ref parameter. Since C# requires that ref parameters be initialized prior to calling a method, this leads to a somewhat non-intuitive calling syntax where each parameter is “artificially” initialized to a dummy value simply to satisfy the C# compiler. The following code demonstrates how one would call the IviScope FetchWaveform method shown above.
double[] WaveformArray = { 0 };double InitialX = 0;double XIncrement = 0;
driver.Measurements.Item("").FetchWaveform(ref WaveformArray, ref InitialX, ref XIncrement);How Nimbus Handles Output Parameters
Section titled “How Nimbus Handles Output Parameters”The IVI-COM Parameter Editor offers three choices for parameter direction — Input, Output, and Input/Output. As per the IVI specification requirements, Nimbus always generates [in, out] in the IDL method signature, even if Output is specified in the editor.
However, it is important to use the direction setting to tell Nimbus what the logical direction of the parameter is, even though the generated code is the same for Output and Input/Output. Doing so allows Nimbus to generate more efficient code. For instance, if a parameter is specified as Output, then Nimbus will not attempt to copy any data from the parameter, since the user will not have provided any. Had the parameter been specified as Input/Output, then Nimbus assumes there may be useful data in the parameter and will perform internal copy operations in some circumstances. For array parameters with a significant number of elements, this can impact driver performance.