Why Matlab executable cannot load .NET library? The DLL path is correct.

18 views (last 30 days)
I wrote a GUI in Matlab and it works fine. But after I compiled it to .exe. It causes error because it cannot load .NET library (I find it out by running .exe in command prompt).
I am testing the .exe on the same computer that compiles the .exe. And the right version of MCR is installed. The error occurs at this line of code: "A = NET.addAssembly(dll_directory);" where 'dll_directory' is the path of dll file I want to load. I checked the 'dll_directory' path during the run of .exe and it is correct.
When I run Line - "A = NET.addAssembly(dll_directory);" in Matlab, there is no error.
Any suggestion on this?
  • I have read this blog 'http://blogs.mathworks.com/loren/2008/08/11/path-management-in-deployed-applications/' which is very informative. But since the dll path is shown correctly during the run, I think the issue is not because of path management. It makes me to doubt if it is because a particular Matlab license? I tried to look documentation for 'ctfroot' and I don't have proper license to view content.

Answers (2)

Rohit Kudva
Rohit Kudva on 19 Oct 2015
Hi Jingliang,
One possible reason can be that while compiling the code to create a standalone executable, you may have not added the DLL file as a required file for your standalone application. Use the '-a' option to add the DLL file while compiling your code with the 'mcc' command
>> mcc -m mytest -a NetSample.dll
In case if you have already added the file using the '-a' option, the path of the DLL file from the view of the standalone application will be different from its actual path on your local file system. When you add files to the archive using '-a' that do not appear on the MATLAB path at the time of compilation, a path entry is added to the application's run-time path so that they appear on the path when the deployed code executes.
When you include files, the absolute path for the DLL and header files changes. The files are placed in the .\exe_mcr\ folder when the archive is expanded. The file is not placed in the local folder. This folder is created from the deployable archive the first time the application is executed. The 'isdeployed' function is provided to help you accommodate this difference in deployed mode.
To work around this issue, copy the DLL to the current MATLAB folder before compiling and don't specify the full path.
  2 Comments
KAE
KAE on 11 Mar 2024
Can I still use mcc in R2023 if I have Matlab compiler? I can't find more recent help on calling a .NET dll from an executable made from a Matlab function.
Walter Roberson
Walter Roberson on 11 Mar 2024
mcc is the command form of MATLAB Compiler, so Yes you can use it if you have MATLAB Compiler

Sign in to comment.


KAE
KAE on 13 Mar 2024
You may need to unblock the dlls in Window properties, see here.

Categories

Find more on Standalone Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!