Main Content

Calling Conventions

Producing a COM Class

Producing a COM class requires the generation of

  • A class definition file in Interface Description Language (IDL)

  • One or more associated C++ class definition/implementation files

MATLAB® Compiler SDK™ automatically produces the necessary IDL and C/C++ code to build each COM class in the component. This process is generally transparent to you when you use the compiler to generate a COM component, and to users of the COM component when they program with it.

For information about IDL and C++ coding rules for building COM objects and for mappings to other languages, see articles in the MSDN Library.

The following table shows the mapping of a generic MATLAB function to IDL code and to Microsoft® Visual Basic®.

CodeSample
Generic MATLAB Code
function [Y1, Y2, ..., varargout] = foo(X1, X2, ..., varargin)
IDL Code
HRESULT foo([in] long nargout,
            [in,out] VARIANT* Y1,
            [in,out] VARIANT* Y2,
            .
            .
            [in,out] VARIANT* varargout,
            [in] VARIANT X1,
            [in] VARIANT X2,
            .
            .
            [in] VARIANT varargin);
Visual Basic Code
Sub foo(nargout As Long, _
        Y1 As Variant, _
        Y2 As Variant, _
        .
        .
        varargout As Variant, _
        X1 As Variant, _
        X2 As Variant, _
        .
        .
        varargin As Variant)

IDL Mapping

The IDL function definition is generated by producing a function with the same name as the original MATLAB function and an argument list containing all inputs and outputs of the original plus one additional parameter, nargout.

When present, the nargout parameter is an [in] parameter of type long. It is always the first argument in the list. This parameter allows correct passage of the MATLAB nargout parameter to the compiled MATLAB code. The nargout parameter is not produced if you encapsulate a MATLAB function containing no outputs.

Following the nargout parameter, the outputs are listed in the order they appear on the left side of the MATLAB function, and are tagged as [in,out], meaning that they are passed in both directions.

The function inputs are listed next, appearing in the same order as they do on the right side of the original function. All inputs are tagged as [in] parameters.

When present, the optional varargin/varargout parameters are always listed as the last input parameters and the last output parameters. All parameters other than nargout are passed as COM VARIANT types. For a list of rules for conversion between MATLAB arrays and COM VARIANTs, see Data Conversion.

Microsoft Visual Basic Mapping

Microsoft Visual Basic provides native support for COM Variants with the Variant type, as well as implicit conversions for all Visual Basic primitive types to and from Variants. In general, arrays/scalars of any Visual Basic primitive type, as well as arrays/scalars of Variant types, can be passed as arguments.

MATLAB Compiler SDK COM components also provide direct support for the Excel® Range object, used by Visual Basic for Applications to represent a range of cells in an Excel worksheet.

See the Visual Basic for Applications documentation included with Microsoft Excel for more information on Visual Basic data types.

See the MSDN Library for more information about Visual Basic and about Excel Range manipulation.