Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
KAE
KAE am 12 Mär. 2024
Bearbeitet: KAE am 13 Mär. 2024
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?

Akzeptierte Antwort

KAE
KAE am 12 Mär. 2024
Bearbeitet: KAE am 13 Mär. 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:

Weitere Antworten (0)

Kategorien

Mehr zu Deploy to .NET Applications Using MWArray API finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by