Contents

Creating COM Objects

Creating the Server Process — An Overview

MATLAB® software provides two functions to create a COM object:

  • actxcontrol — Creates a Microsoft® ActiveX® control in a MATLAB figure window.

  • actxserver — Creates an in-process server for a dynamic link library (DLL) component or an out-of-process server for an executable (EXE) component.

The following diagram shows the basic steps in creating the server process. For more information on how the MATLAB software establishes interfaces to the resultant COM object, see Getting Interfaces to COM Object.

Creating an ActiveX Control

You can create an instance of an ActiveX control from the MATLAB client using either a graphical user interface (GUI) or the actxcontrol function from the command line. Either of these methods creates an instance of the control in the MATLAB client process and returns a handle to the primary interface to the COM object. Through this interface, you can access the object's public properties or methods. You can also establish more interfaces to the object, including interfaces that use IDispatch, and any custom interfaces that might exist.

This section describes how to create an instance of the control and how to position it in the MATLAB figure window.

Listing Installed Controls

The actxcontrollist function shows you what COM controls are currently installed on your system. Type:

list = actxcontrollist

MATLAB displays a cell array listing each control, including its name, programmatic identifier (ProgID), and file name.

This example shows information for several controls (your results might be different):

list = actxcontrollist;
s=sprintf(' Name = %s\n ProgID = %s\n File = %s\n', list{114:115,:})

MATLAB displays:

s =
 Name = OleInstall Class
 ProgID = Outlook Express Mime Editor
 File = OlePrn.OleInstall.1
 Name = OutlookExpress.MimeEdit.1
 ProgID = C:\WINNT\System32\oleprn.dll
 File = C:\WINNT\System32\inetcomm.dll

Finding a Particular Control

If you know the name of a control, you can find it in the list and display its ProgID and the path of the folder containing it. For example, some of the examples in this documentation use the Mwsamp2 control. You can find it with the following code:

list = actxcontrollist;
for ii = 1:length(list)
   if ~isempty(strfind([list{ii,:}],'Mwsamp2')) 
      s = sprintf(' Name = %s\n ProgID = %s\n File = %s\n', ...
             list{ii,:})
   end
end

MATLAB displays:

s =
   Name = Mwsamp2 Control
   ProgID = MWSAMP.MwsampCtrl.2
   File =
   D:\Apps\MATLAB\R2006a\toolbox\matlab\winfun\win32\mwsamp2.ocx

The location of this file might be different on your system.

Creating Control Objects Using a GUI

Using the actxcontrolselect function is the simplest way to create an instance of a control object. This function displays a GUI listing all controls installed on your system. When you select an item from the list and click the Create button, MATLAB creates the control and returns a handle to it. Type:

h = actxcontrolselect

MATLAB displays the Select an ActiveX Control dialog box.

The interface has an ActiveX Control List selection pane on the left and a Preview pane on the right. To see a preview of the control, click one of the control names in the selection pane. (A blank preview pane means that the control does not have a preview.) An error message appears in the preview pane if MATLAB cannot create the instance.

Setting Properties with actxcontrolselect.  To change property values when creating the control, click the Properties button in the Preview pane. You can select which figure window to put the control in (Parent field), where to position it in the window (X and Y fields), and what size to make the control (Width and Height).

You can register events you want the control to respond to in this window. (For an explanation of event registration, see Responding to Events — an Overview.) Register an event and the callback routine to handle that event by entering the name of the routine to the right of the event under Event Handler.

You can select callback routines by clicking a name in the Event column, and then clicking the Browse button. To assign a callback routine to more than one event, first press the Ctrl key and click individual event names, or drag the mouse over consecutive event names, and then click Browse to select the callback routine.

MATLAB only responds to registered events, so if you do not specify a callback, the event is ignored.

For example, in the ActiveX Control List pane, select Calendar Control 10.0 (the version on your system might be different) and click Properties. MATLAB displays the Choose ActiveX Control Creation Parameter dialog box. To change the default size for the control, enter a Width of 500 and a Height of 350. Click OK in this window, and click Create in the next window to create an instance of the Calendar control.

You can also set control parameters using the actxcontrol function. One parameter you can set with actxcontrol, but not with actxcontrolselect, is the name of an initialization file. When you specify this file name, MATLAB sets the initial state of the control to that of a previously saved control.

Information Returned by actxcontrolselect.  The actxcontrolselect function creates an object that is an instance of the MATLAB COM class. The function returns up to two arguments: a handle for the object, h, and a 1-by-3 cell array, info, containing information about the control. To get this information, type:

[h, info] = actxcontrolselect

The cell array info shows the name, ProgID, and file name for the control.

If you select the Calendar Control, and then click Create, MATLAB displays information like:

h =
   COM.mscal.calendar.7
info = 
    [1x20 char]    'MSCAL.Calendar.7'    [1x41 char]

To expand the info cell array, type:

info{:}

MATLAB displays:

ans =
   Calendar Control 9.0
ans =
   MSCAL.Calendar.7
ans =
   D:\Applications\MSOffice\Office\MSCAL.OCX

Creating Control Objects from the Command Line

If you already know which control you want and you know its ProgID, you can bypass the GUI by using the actxcontrol function to create an instance of it.

The ProgID is the only required input to this function. However, as with actxcontrolselect, you can supply more inputs that enable you to select which figure window to put the control in, where to position it in the window, and what size to make it. You can also register any events you want the control to respond to, or set the initial state of the control by reading that state from a file. See the actxcontrol reference page for a full explanation of its input arguments.

The actxcontrol function returns a handle to the primary interface to the object. Use this handle to reference the object in other COM function calls. You can also use the handle to obtain more interfaces to the object. For more information on using interfaces, see Getting Interfaces to COM Object.

Repositioning the Control in a Figure Window

After creating a control, you can change its shape and position in the window with the move function.

Observe what happens to the object created in the last section when you specify new origin coordinates (70, 120) and new width and height dimensions of 400 and 350:

move(h,[70 120 400 350]);

Limitations to ActiveX Support

A MATLAB COM ActiveX control container does not in-place activate controls until they are visible.

Creating a COM Server

Instantiating a DLL Component

To create a server for a component implemented as a dynamic link library (DLL), use the actxserver function. MATLAB creates an instance of the component in the same process that contains the client application.

The syntax for actxserver, when used with a DLL component, is actxserver(ProgID), where ProgID is the programmatic identifier for the component.

actxserver returns a handle to the primary interface to the object. Use this handle to reference the object in other COM function calls. You can also use the handle to obtain more interfaces to the object. For more information on using interfaces, see Getting Interfaces to COM Object.

Unlike Microsoft ActiveX controls, any user interface displayed by the server appears in a separate window.

You cannot use a 32-bit in-process DLL COM object in a 64-bit MATLAB application. For information about this restriction, see http://www.mathworks.com/matlabcentral/answers/95116-why-am-i-not-able-to-use-32-bit-dll-com-objects-in-64-bit-matlab-7-3-r2006b.

Instantiating an EXE Component

You can use the actxserver function to create a server for a component implemented as an executable (EXE). In this case, MATLAB instantiates the component in an out-of-process server.

The syntax for actxserver, when used to create an executable, is actxserver(ProgID, sysname), where ProgID is the programmatic identifier for the component, and sysname is an optional argument used in configuring a distributed COM (DCOM) system.

actxserver returns a handle to the primary interface to the COM object. Use this handle to reference the object in other COM function calls. You can also use the handle to obtain more interfaces to the object. For more information on using interfaces, see Getting Interfaces to COM Object.

Any user interface displayed by the server appears in a separate window.

This example creates a COM server application running the Microsoft Excel® spreadsheet program. The handle is assigned to h.

h = actxserver('Excel.Application')

MATLAB displays:

h =
    COM.excel.application

MATLAB can programmatically connect to an instance of a COM Automation server application that is already running on your computer. To get a reference to such an application, use the actxGetRunningServer function.

This example gets a reference to the Excel program, which must already be running on your system. The returned handle is assigned to h.

h = actxGetRunningServer('Excel.Application')

MATLAB displays:

h =
    COM.excel.application
Was this topic helpful?