| MATLAB® | ![]() |
| On this page… |
|---|
The Microsoft® Component Object Model (COM) provides a framework for integrating reusable, binary software components into an application. Because components are implemented with compiled code, the source code can be written in any of the many programming languages that support COM. Upgrades to applications are simplified, as components can simply be swapped without the need to recompile the entire application. In addition, a component's location is transparent to the application, so components can be relocated to a separate process or even a remote system without having to modify the application.
Using COM, developers and end users can select application-specific components produced by different vendors and integrate them into a complete application solution. For example, a single application might require database access, mathematical analysis, and presentation-quality business graphs. Using COM, a developer can choose a database-access component by one vendor, a business graph component by another, and integrate these into a mathematical analysis package produced by yet a third.
MATLAB® software supports COM integration on the Microsoft Windows® platform only.
While the ideas behind COM technology are straightforward, the terminology is not. The meaning of COM terms has changed over time and few concise definitions exist. Here are some terms that you should be familiar with before reading this chapter. These are not comprehensive definitions. For a complete description of COM, you'll need to consult outside resources.
A COM object is a software component that conforms to the Component Object Model. COM enforces encapsulation of the object, preventing direct access of its data and implementation. COM objects expose Interfaces, which consist of properties, methods and events.
A COM client is a program that makes use of COM objects. COM objects that expose functionality for use are called COM servers. COM servers can be in-process or out-of-process. An example of an out-of-process server is Microsoft® Excel® spreadsheet program. These configurations are described in In-Process and Out-of-Process Servers.
A Microsoft ActiveX® control is a type of in-process COM server that requires a control container. ActiveX controls typically have a user interface. An example is the Microsoft Calendar control. A control container is an application capable of hosting ActiveX controls. A MATLAB figure window or a Simulink® model are examples of control containers.
MATLAB can be used as either a COM client or COM server.
The functionality of a component is defined by one or more interfaces. To use a COM component, you must learn about its interfaces, and the methods, properties, and events implemented by the component. The component vendor provides this information.
There are two standard COM interfaces:
IUnknown — An interface required by all COM components. All other COM interfaces are derived from IUnknown.
IDispatch — An interface that exposes objects, methods and properties to applications that support Automation.
There are three types of COM servers:
Automation — A server that supports the OLE Automation standard. Automation servers are based on the IDispatch interface. Automation servers can be accessed by clients of all types, including scripting clients.
Custom — A server that implements an interface directly derived from IUnknown. Custom servers are preferred when faster client access is critical.
Dual — A server that implements a combination of Automation and Custom interfaces.
To create an instance of a COM object, you use its programmatic identifier, or ProgID. The ProgID is a unique string defined by the component vendor to identify the COM object. You obtain a ProgID from the vendor's documentation.
The MATLAB ProgIDs are
Matlab.Application — Starts a command window Automation server with the version of MATLAB that was most recently used as an Automation server (might not be the latest installed version of MATLAB).
Matlab.Autoserver — Starts a command window Automation server using the most recent version of MATLAB.
Matlab.Desktop.Application — Starts the full desktop MATLAB as an Automation server using the most recent version of MATLAB.
You can configure a server three ways. MATLAB supports all of these configurations.
In-Process Server. An in-process server is a component implemented as a dynamic link library (DLL) or ActiveX control that runs in the same process as the client application, sharing the same address space. Communication between client and server is relatively fast and simple.
Local Out-of-Process Server. A local out-of-process server is a component implemented as an executable (EXE) file that runs in a separate process from the client application. The client and server processes are on the same computer system. This configuration is somewhat slower due to the overhead required when transferring data across process boundaries.
Remote Out-of Process Server. This is a type of out-of-process server; however, the client and server processes are on different systems and communicate over a network. Network communications, in addition to the overhead required for data transfer, can make this configuration slower than the local out-of-process configuration. This configuration runs only on systems that support the Distributed Component Object Model (DCOM).
Using MATLAB as a COM client provides two techniques for developing programs in MATLAB:
You can include COM components in your MATLAB application (for example, a spreadsheet).
You can access existing applications that expose objects via Automation.
In a typical scenario, MATLAB creates ActiveX controls in figure windows, which are manipulated by MATLAB through the controls' properties, methods, and events. This is useful because there exists a wide variety of graphical user interface components implemented as ActiveX controls. For example, the Microsoft Internet Explorer® program exposes objects that you can include in a figure to display an HTML file. There also are treeviews, spreadsheets, and calendars available from a variety of sources.
MATLAB COM clients can access applications that support Automation, such as the Excel® spreadsheet program. In this case, MATLAB creates an Automation server in which to run the application and returns a handle to the primary interface for the object created.
Information about creating and using COM controls and server objects in MATLAB can be found in Creating COM Objects.
Automation provides an infrastructure whereby applications called automation controllers can access and manipulate (i.e. set properties of or call methods on) shared automation objects that are exported by other applications, called Automation servers. Any Windows program that can be configured as an Automation controller can control MATLAB.
For example, using Microsoft® Visual Basic® programming language, you can run a MATLAB demo in a Microsoft® PowerPoint® presentation. In this case, PowerPoint® is the controller and MATLAB is the server.
Information for creating and connecting to a MATLAB Automation server running MATLAB can be found in MATLAB® COM Automation Server Support.
Before using COM objects, you must register their controls and servers. Most are registered by default. However, if you get a new .ocx, .dll, or other object file for the control or server, you must register the file manually in the Windows registry.
Use the DOS regsvr32 command to register your file. From the DOS prompt, use the cd function to go to the directory where the object file is located. If your object file is an .ocx file, type:
regsvr32 filename.ocx
For example, to register the MATLAB control mwsamp2.ocx, type:
cd matlabroot\toolbox\matlab\winfun\win32 regsvr32 mwsamp2.ocx
If you encounter problems with this procedure, please consult a Windows manual or contact your local system administrator.
Here are several ways to verify that a control or server is registered. These examples use the MATLAB mwsamp control. Refer to your Microsoft product documentation for information about using Microsoft® Visual Studio® or the Microsoft Registry Editor programs.
Go to the Visual Studio® .NET 2003 Tools menu and execute the ActiveX control test container. Click Edit, insert a new control, and select MwSamp Control. If you are able to insert the control without any problems, the control is successfully registered. Note that this method only works on controls.
Open the Registry Editor by typing regedit at the DOS prompt. Search for your control or server object by selecting Find from the Edit menu. It will likely be in the following structure:
HKEY_CLASSES_ROOT/progid
Open OLEViewer from the Visual Studio .NET 2003 Tools menu. Look in the following structure for your Control object:
Object Classes : Grouped by Component Category : Control : Your_Control_Object_Name (i.e. Object Classes : Grouped by Component Category : Control : Mwsamp Control)
![]() | COM Support for MATLAB® Software | Getting Started with COM | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |