How to compile so exe calls a .NET dll correctly?

6 views (last 30 days)
I have a mfile mfileContainingDllCall.m that calls several .NET dlls succesfully in Matlab with these lines,
assemblyFile = 'firstNetDll.dll'; % This dll calls secondNetDll.dll
assert(exist(assemblyFile, 'file') == 2, 'File not found.' );
asm = NET.addAssembly(assemblyFile);
import firstNetDll.*
To make an executable version of the mfile, I compiled it as,
% Use -a to add the dlls, https://www.mathworks.com/help/compiler/mcc.html#mw_1a52a1c0-e230-4e98-be45-fad5d458fdec
mcc -m mfileContainingDllCall.m -a firstNetDll.dll -a secondNetDll.dll
When I run .\mfileContainingDll.exe from the Windows 11 command line, I get
'firstNetDll.dll' could not be found in the .NET Global Assembly Cache (GAC).
And if I put the following command in a try/catch structure,
try
NET.addAssembly(assemblyFile);
catch
disp('Failed NET.addAssembly(assemblyFile);')
end
it throws the catch error, so that's the command that is causing the issue.
The directory containing mfileContainingDllCall.m contains firstNetDll.dll and secondNetDll.dll because I think they have to be in the same directory and added with the -a flags.
I saw that Gacutil.exe can be used to resolve errors with the Global Assembly Cache, but the mfile doesn't give the GAC error, so I assume the error is in how I am calling mcc. I have Matlab compiler and R2023b.
What changes to the mcc command will prevent the GAC error?

Accepted Answer

KAE
KAE on 12 Mar 2024
Edited: KAE on 13 Mar 2024
It turned out that Windows was blocking the dlls due to a security mechanism: if a DLL is copied to the file system from unknown sources, it gets blocked.
To unblock it: in Windows Explorer, right click on the dll, choose Properties, and at the bottom click 'Unblock'. Do this for all the dlls that are used. It worked fine afterward. Thanks to Mathworks support for this solution. Some more details from them:
A Windows permission issue was the root cause of "NET.addAssembly" not working in the compiled application. The solution was to unblock the DLLs by manually adjusting their properties, as in the following resource:

More Answers (0)

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!