Main Content

Create a Generic COM Component with MATLAB Code

Supported platform: Windows®

This example shows how to create a generic COM component using a MATLAB® function and integrate it into an application. The target system does not require a licensed copy of MATLAB.

Prerequisites

  • Verify that you have the Windows 10 SDK kit installed. For details, see Windows 10 SDK.

  • Verify that you have MinGW-w64 installed. To install it from the MathWorks File Exchange, see MATLAB Support for MinGW-w64 C/C++ Compiler.

    To ensure that MATLAB detects the Windows 10 SDK kit and MinGW-w64, use the following command:

    mbuild -setup -client mbuild_com

  • Verify that you have Microsoft® Visual Studio® installed.

  • End users must have an installation of MATLAB Runtime to run the application. For details, see Install and Configure MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want packaged. For this example, open makesquare.m located in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareComp.

function y = makesquare(x)
y = magic(x);

At the MATLAB command prompt, enter makesquare(5).

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

Create Generic COM Component Using Library Compiler App

Package the function into a COM component using the Library Compiler app. Alternatively, if you want to create a COM component from the MATLAB command window using a programmatic approach, see Create COM Component Using compiler.build.COMComponent.

  1. On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler. In the MATLAB Compiler project window, click Generic COM Component.

    Library Compiler App with the Generic COM Component type selected

    Alternately, you can open the Library Compiler app by entering libraryCompiler at the MATLAB prompt.

  2. In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

    2. In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.

    The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.

  3. In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:

    • Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the file name of the installer.

    • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.

      Note

      The first time you select this option, you are prompted to download the MATLAB Runtime installer.

  4. In the Library Name field, replace makesquare with MagicSquareComp.

  5. Verify that the function defined in makesquare.m is mapped into Class1.

    Class Name Class1 mapped to Method Name y = makesquare(x)

Customize the Application and Its Appearance

In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.

  • Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.

  • Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.

  • Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.

  • Files installed for your end user — Files that are installed with your application.

    See Specify Files to Install with Application.

Library information section with the Library Name set to MagicSquareComp

Package the Application

When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.

  1. Click Package.

    In the Save Project dialog box, specify the location to save the project.

  2. In the Package dialog box, verify that Open output folder when process completes is selected.

    When the packaging process is complete, examine the generated output in the target folder.

    • Three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.

    • The log file PackagingLog.html contains packaging results.

Create COM Component Using compiler.build.COMComponent

As an alternative to the Library Compiler app, you can create a COM component using a programmatic approach. If you have already created a component using the Library Compiler, see Integrate into COM Application.

  1. Save the path to the file makesquare.m located in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareComp. For example, if you are using Visual Studio version 15, type:

    appFile = fullfile(matlabroot,'toolbox','dotnetbuilder','Examples', ...
        'VS15','COM','MagicSquareExample','MagicSquareComp','makesquare.m');
  2. Build the COM component using the compiler.build.comComponent function. Use name-value arguments to specify the component name and class name.

    buildResults = compiler.build.comComponent(appFile, ...
    'ComponentName','MagicSquareComp', ...
    'ClassName','Class1');

    You can specify additional options in the compiler.build command by using name-value arguments. For details, see compiler.build.comComponent.

    The compiler.build.Results object buildResults contains information on the build type, generated files, included support packages, and build options.

    The function generates the following files within a folder named MagicSquareCompcomComponent in your current working directory:

    • magicsquare.def

    • magicsquare.rc

    • magicsquare_1_0.dll

    • readme.txt

    • requiredMCRProducts.txt

    • unresolvedSymbols.txt

    • Class1_com.cpp — C++ source code file that defines the class.

    • Class1_com.hpp — C++ header file that defines the class.

    • dlldata.c — C source code file that contains entry points and data structures required by the class factory for the DLL.

    • GettingStarted.html — HTML file that contains steps on installing COM components.

    • includedSupportPackages.txt — Text file that contains information on included support packages.

    • MagicSquareComp.def — Module definition file that defines which functions to include in the DLL export table.

    • MagicSquareComp.rc — Resource script file that describes the resources used by the component.

    • MagicSquareComp_1_0.dll — Dynamic-link library file.

    • MagicSquareComp_dll.cpp — C++ source code file that contains helper functions.

    • MagicSquareComp_idl.h — C++ header file.

    • MagicSquareComp_idl.idl — Interface definition language file.

    • MagicSquareComp_idl.tlb — Type library file that contains information about the COM object properties and methods.

    • MagicSquareComp_idl_i.c — C source code file that contains the IIDs and CLSIDs for the IDL interface.

    • MagicSquareComp_idl_p.c — C source code file that contains the proxy stub code for the IDL interface.

    • mccExcludedFiles.log — Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.

    • mwcomtypes.h — C++ header file that contains the definitions for the interfaces.

    • mwcomtypes_i.c — C source code file that contains the IIDs and CLSIDs.

    • mwcomtypes_p.c — C source code file that contains the proxy stub code.

    • readme.txt — Text file that contains deployment information.

    • requiredMCRProducts.txt — Text file that contains product IDs of products required by MATLAB Runtime to run the application.

    • unresolvedSymbols.txt — Text file that contains information on unresolved symbols.

    Note

    The generated component does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.installer.

Integrate into COM Application

To integrate your COM component into an application, see Creating the Microsoft Visual Basic Project.

See Also

| | |

Related Topics