필터 지우기
필터 지우기

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

조회 수: 17 (최근 30일)
KAE
KAE 2024년 3월 12일
편집: KAE 2024년 3월 13일
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?

채택된 답변

KAE
KAE 2024년 3월 12일
편집: KAE 2024년 3월 13일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by