MATLAB Answers

How can I create and access a C# COM object from MATLAB?

13 views (last 30 days)
I want to create a COM object using C# and would like to call this COM object from MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Feb 2011
You can use MATLAB as a COM client to call COM objects using the ACTXSERVER or ACTXCONTROL functions. These COM objects do not need to be created in any special manner. For details about how to call a COM object from MATLAB, see the following chapter of the MATLAB External Interfaces manual.
The following is an example of how to create a simple COM object in C# and then call it from MATLAB. This example was created using Microsoft Visual Studio 2005. The exact steps may differ for other versions of Microsoft Visual Studio.
1. Open Microsoft Visual Studio 2005 and select File->New Project. Then select Visual C#->Class Library and enter the name myCOMObject. Click OK.
2. Replace the auto-generated code in Class1.cs with the following:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
using System.Diagnostics;
namespace myCOMObject
//COM interface
public interface ITestInterface
[DispId(1)] //
void test();
[DispId(2)] //
string Mess(string s);
[DispId(3)] //
double[] ArrayTest(int n);
// COM events for actxserver command
public class TestInterface : ITestInterface
public TestInterface()
public void test() //
Process p = null;
p = new Process();
p.StartInfo.FileName = "notepad";
MessageBox.Show("Here is notepad");
public string Mess(string s) //
return "Hello Jody";
public double[] ArrayTest(int n) //
return (new double[2] { 1, 5 });
3. Select Tools->Create GUID, click the Registry Format option and click the Copy button. Then paste this new GUID into the line
//COM interface
Be sure to remove the curly braces {} from the pasted string.
4. Next, click the New GUID button and copy-paste a second GUID into the line
5. Select Project->Add Reference. In the .NET tab select System.Windows.Form and click OK.
6. Open the AssemblyInfo.cs file and modify the following line from
[assembly: ComVisible(false)]
[assembly: ComVisible(true)
7. Right-click on the myCOMObject project and select Properties. Select the Build category and check the box for "Register for COM interop".
8. Select Build->Build Solution. This will build the application and auto-register the application with the ProgID Test.Library.
9. Now you can access the COM object from MATLAB. The following code creates a new instance of the COM object, displays the methods of that object and calls the test and Mess functions.
Note: MathWorks does not provide technical support for using Microsoft Visual Studio or offer guidance in the creation of COM objects outside of MATLAB.

More Answers (1)

Antoine Caillaud
Antoine Caillaud on 27 Mar 2020
I have the exact same need of accessing the methods and properties of a C# application but developed Out-Of-Process (different from a the library proposed in the example).
The methods and properties are not accessible directly.
h = COM.Test_Application
>>h. ---> nothing appears and it is not possible to invoke applications directly.
Indeed, if I type
>> h.Help
Cannot find an exact (case-sensitive) match for 'Help'
However, it works with invoke() funtion and application of the COM object are directly accessibles.
How should the procedure be adapted to work with program developed Out-Of-Process with their own applications and not only libraries?
Thank you.


No tags entered yet.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!