MATLAB Answers

Can I generate a .NET dll that imports other .NET assembly?

6 views (last 30 days)
This might sound weird, but I want to use a .NET assembly on Matlab (I already know how to do this), make some functions with it, and then generate a .NET .dll with the compile tool. My question is, will the compile tool complain about the usage of external code?
The situation is as follows: I was given a C# programmed API, which I have to implement. I have to create other functions, which consume the API. I would like to make these other functions in Matlab, since I have much more experience there than in C#. The thing is that these functions have to be exported to another program, via dll (this is not optional). So, I was thinking to compile these new functions into a new .NET assembly.
Thanks a lot in advance!


Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 3 Dec 2018
Yes, you can use external .NET dll in compiled MATLAB function.
For example, assume the name of external dll is 'dotNetExternalClass.dll'. In MATLAB function, you can load that dll with full path.
function out = compileOtherDll(in1, in2)
externalDllPath = fullfile(pwd, 'dotNetExternalClass.dll');
asm = NET.addAssembly(externalDllPath);
% do something
% ...
% ...
Then, compile this function as .NET assembly using deploytool. compileOtherDll.dll will be created in this case.
After that, in Visual Studio, it's needed to add compileOtherDll.dll and $MATLAB_INSTALL\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll as references and add "using" in C# codes.
using MathWorks.MATLAB.NET.Arrays;
using compileOtherDll;
After building this C# codes, you need to place the external dll (dotNetExternalClass.dll) in the same folder of exe file.


Alejandro Wechsler
Alejandro Wechsler on 3 Dec 2018
Great! Thanks a lot! It was exactly what I was planning to do, but I wanted to double check it.
I don't have to consume the new dll from C# later, but from a propietary software that accepts dlls. I don't think this will be an issue. I was just worried about the deploytool complaining about the use of the addAssembly function.
Finally, is it necesarry to have the matlab runtine installed on the consumer PC? Or the only dependency is MWArray,dll?
This is great news, I will surely procede with this plan.
Again, thanks a lot for the reply.
Kojiro Saito
Kojiro Saito on 3 Dec 2018
Yes, I don't think there's a concern of using compiled dll from a propietary software, too. What messages did you get by deploytool?
On the end users' PCs, installation of MATLAB Runtime is necessary. Instead of MATLAB, MATLAB Runtime works with compiled applications/libararies.
Alejandro Wechsler
Alejandro Wechsler on 3 Dec 2018
Nono, I haven't tried it yet.
Thanks for replying, it has been extremely usefull.

Sign in to comment.

More Answers (0)

Translated by