MATLAB COM Builder    

Capabilities

MATLAB COM Builder enables you to integrate compiled MATLAB models into Visual Basic, C++, or any other language that supports COM. Each MATLAB COM Builder component is built as a stand-alone COM object. (COM is an acronym for Component Object Model, Microsoft's binary standard for object interoperability. COM is the widely accepted standard for integration of external functionality into Microsoft Office applications, such as Excel.) Each MATLAB function included in a given component appears as a method of the created COM class. The resulting call syntax from Visual Basic is systematically mapped to the syntax of the original MATLAB function. This mapping provides an intuitive bridge from MATLAB, where the functions are created, to Visual Basic, where the functions are ultimately called.

MATLAB COM Builder provides robust data conversion and array formatting to preserve the flexibility of MATLAB when calling from Visual Basic. Also provided is custom error processing so that errors originating from MATLAB functions are automatically manifested as Visual Basic exceptions. The information returned with the error always references the original MATLAB code, making debugging easy.

A simple versioning mechanism is also built into each component to help manage deployment of multiple versions of the same component. Figure A-1 provides an overview of the process of creating a stand-alone COM object from compiled MATLAB M-files.

Figure A-1: Creating a Stand-Alone COM Object with the MATLAB Compiler

The process of creating a MATLAB COM Builder component is completely automatic. The user supplies a list of M-files to process and a few additional pieces of information, i.e., the component name, the class names, and the version number. The build process that follows involves code generation, compiling, linking, and registration of the finished component.

Figure A-2 shows the files created at each step in the entire process, from compilation to registration of the final DLL.

Figure A-2: M-Build Steps and Intermediate Files Created

Step 1. Code Generation

The first step in the build process generates all source code and other supporting files needed to create the component.

The compiler first produces .c and .h files (foo.h, foo.c, bar.h, and bar.c), representing the C-language translation of the M-code in the original M-files (foo.m and bar.m). It also creates the main source file (mycomponent_dll.cpp) containing the implementation of each exported function of the DLL. The compiler additionally produces an Interface Description Language (IDL) file (mycomponent_idl.idl), containing the specifications for the component's type library, interface, and class, with associated GUIDs. (GUID is an acronym for Globally Unique Identifier, a 128-bit integer guaranteed always to be unique.)

Created next are the C++ class definition and implementation files (mycomponent_com.hpp and mycomponent_com.cpp). In addition to these source files, the compiler generates a DLL exports file (mycomponent.def), and a resource script (mycomponent.rc).

Step 2. Create Interface Definitions

The second step of the build process invokes the IDL compiler on the IDL file generated in step 1 (mycomponent_idl.idl), creating the interface header file (mycomponent_idl.h), the interface GUID file (mycomponent_idl_i.c), and the component type library file (mycomponent_idl.tlb). The interface header file contains type definitions and function declarations based on the interface definition in the IDL file. The interface GUID file contains the definitions of the GUIDs from all interfaces in the IDL file. The component type library file contains a binary representation of all types and objects exposed by the component.

Step 3. C++ Compilation

The third step compiles all C/C++ source files generated in steps 1 and 2 into object code. One additional file containing a set of C++ template classes (mclcomclass.h) is included at this point. This file contains template implementations of all necessary COM base classes, as well as error handling and registration code. See Compiler Requirements for a list of supported C++ compilers.

Step 4. Linking and Resource Binding

The fourth step produces the finished DLL for the component. This step invokes the linker on the object files generated in step 3 and the necessary MATLAB libraries to produce a DLL component (mycomponent_1_0.dll). The resource compiler is then invoked on the DLL, along with the resource script generated in step 1, to bind the type library file generated in step 2 into the completed DLL.

Step 5. Component Registration

The final build step registers the DLL on the system. See Component Registration for information about this process.


  Producing a COM Object from MATLAB Calling Conventions