フィルターのクリア

How do I call a .NET dll function from MATLAB?

51 ビュー (過去 30 日間)
Dmitry Labukhin
Dmitry Labukhin 2019 年 3 月 6 日
回答済み: Rajeev Yadav 2021 年 9 月 21 日
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 件のコメント
Guillaume
Guillaume 2019 年 3 月 6 日
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.
Dmitry Labukhin
Dmitry Labukhin 2019 年 3 月 6 日
Yes, they are both set to 64-bit. That's the thing: I don't see any error when I use that library in C#. It's only when I try to use it in MATLAB (with which I created the dll) that the error occurs.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2019 年 3 月 6 日
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?
  1 件のコメント
Dmitry Labukhin
Dmitry Labukhin 2019 年 3 月 6 日
Oh, I see. Thank you very much, that is basically the answer.
Admittedly, it's an odd thing to want to do. Why not use the m file directly?
Agreed. It's just in my particular case it would make certain things easier, but since it's not an option I'll have to stick to direct m-funciton calls.
Many thanks, Guillaume!

サインインしてコメントする。

その他の回答 (1 件)

Rajeev Yadav
Rajeev Yadav 2021 年 9 月 21 日
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 ...

カテゴリ

Help Center および File Exchange.NET Assembly Integration についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by