Why does (MLApp.MLApp)Marshal.GetActiveObject("MATLAB.Desktop.Application") return NULL?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 4 de Sept. de 2018
Respondida: MathWorks Support Team
el 18 de Sept. de 2018
I am attempting to create a COM connection to a running MATLAB instance from a .NET assembly with the code:
class Program
{
static void Main(string[] args)
{
//Variable
Type matlab;
object[] parameter = new object[1];
object mlObject;
try
{
matlab = Type.GetTypeFromProgID("Matlab.Application");
mlObject = Activator.CreateInstance(matlab);
object[] command = { "matlabroot" };
object testResult = (object)matlab.InvokeMember("Execute", System.Reflection.BindingFlags.InvokeMethod, null, mlObject, command);
....
On some machines this works with any MATLAB version I try it with, where as on others it will not work at all. In these cases, the function simply returns "NULL". This is resolved by using the MATLAB command "regmatlabserver", at which point it will work for any MATLAB instance. I would like to avoid this, as this command requires Admin rights.
Respuesta aceptada
MathWorks Support Team
el 4 de Sept. de 2018
A couple of notes to make because we are connecting to an active session.
First, if the C# application has any references to MATLAB, for example R2017b, that MATAB should be registered. For GetActiveObject to work, MATLAB should be started as an automation server. Because we are connecting specifically to "matlab.desktop.application" we need to use "enableservice" to make it an Automation Server. In the MATLAB Desktop instance, enter the command:
>> enableservice('AutomationServer',true);
This command will set the existing MATLAB into an automation Server (Does not require admin). At this point, the code should work on all machines. More information can be found at:
Another alternative is the use the "matlab.applicaiton" progid (If your application does not require the full MATLAB desktop). This will connect it to any MATLAB started with the "-automation" flag. For example, open a windows CMD terminal and type:
>> matlab -automation
To open a MATLAB command window as an automation server.
A final suggestion is to create a new instance if C# fails to get an existing instance, as shown at:
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Write COM Applications to Work with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!