How do I call a .NET dll function from MATLAB?
45 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dmitry Labukhin
el 6 de Mzo. de 2019
Respondida: Rajeev Yadav
el 21 de Sept. de 2021
I want to see how I can call a MATLAB-created .NET dll function from MATLAB, if it's possible at all.
I thought it would be straightforward, but I’m running into an error message.
Here’s what I did:
1. Created a test function
function out = myfunc1(inp)
out = 2*inp;
2. Compiled it into a .NET dll (using a APPS->APPLICATION COMPILER, etc with the option ‘Runtime included in package’ on): the dll name is myfunc1.dll and the class name is ClassName1
3. In the MATLAB command window, ran addAssembly to add this library
library = NET.addAssembly('[path to the created dll]\for_testing\myfunc1.dll');
4. Checked what methods it has (to make sure everything worked as expected)
methods myfunc1.ClassName1
Methods for class myfunc1.ClassName1:
ClassName1 Equals GetType WaitForFiguresToDie
Dispose GetHashCode ToString myfunc1
Static methods:
ReferenceEquals
so I could see that the method myfunc1 is there
5. Tried to instantiate an object of this class
obj = myfunc1.ClassName1;
(with the idea to later run the actual function: obj.myfunc1(inp);)
6. At the instantiation stage got the following error message
Message: The type initializer for 'myfunc1.ClassName1' threw an exception.
Source: mscorlib
HelpLink:
7. I tested the same dll in a C# project and it worked fine, so I don't think it's the same issue as https://www.mathworks.com/matlabcentral/answers/384932-why-do-i-get-type-initializer-throws-an-exception-error-every-time-the-compiled-class-is-instantia
Many thanks for any help!
2 comentarios
Guillaume
el 6 de Mzo. de 2019
It could be the same issue.If your C# projec has the same bitness as the dll, then you won't see an error there. But if that bitness is not the same as matlab, yes, you'd get an error in matlab.
I don't have matlab Compiler so don't know, but isn't there a setting in your matlab project for the target platform? If it's set to 32-bit (or any) and assuming that you're using 64-bit matlab, then you will get an error.
Respuesta aceptada
Guillaume
el 6 de Mzo. de 2019
While looking for a setting to configure the bitness in the online documentation, I came across these prerequisites:
.NET assemblies or DLLs built with MATLAB Compiler SDK cannot be loaded back into MATLAB with the .NET External Interface method addAssembly.
So, looks like what you're trying to do is not supported and is never going to work. Admittedly, it's an odd thing to want to do. Why not use the m file directly?
Más respuestas (1)
Rajeev Yadav
el 21 de Sept. de 2021
Why not??? Matlab license are so expensive that we have to look for solutions like this or completely abandon matlab and that's what is gonna happen in coming years ...
0 comentarios
Ver también
Categorías
Más información sobre .NET Assembly Integration 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!