英文:
Call nested .NET dll from C++/CLI Visual Studio project
问题
I'm trying to call a DLL-method from my C++/CLI code in Visual Studio a .NET-DLL (v4.5) called "LicenseCheck.dll". This works fine until this DLL is trying to access another .NET-DLL called "SKCLNET.dll" (.NET v1.0.3705). Meaning the "LicenseCheck.dll" depends on "SKCLNET.dll".
When I try to call the Licensecheck.dll-method "ValidateLicense::GetLicenseStatus()" from a .NET-project it all works fine.
Here is the error I get when running the C++/CLI code:
> Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'SKCLNET.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)
at Seat.Core.LicenseProvider..ctor()
at Seat.Core.LicenseProvider.get_Instance()
at LicenseCheck.ValidateLicense.GetLicenseStatus()
at main(String[] args) in C:\Users\deckenf\Desktop\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:line 13
at mainCRTStartupStrArray(String[] arguments) in D:\a_work\1\s\src\vctools\crt\crtw32\msilcrt\mcrtexe.cpp:line 241
C:\Users\deckenf\Desktop\ConsoleApplication1\x64\Release\ConsoleApplication1.exe (process 33364) exited with code -532462766.
英文:
I'm trying to call a DLL-method from my C++/CLI code in Visual Studio a .NET-DLL (v4.5) called "LicenseCheck.dll". This works fine until this DLL is trying to access another .NET-DLL called "SKCLNET.dll" (.NET v1.0.3705). Meaning the "LicenseCheck.dll" depends on "SKCLNET.dll".
When I try to call the Licensecheck.dll-method "ValidateLicense::GetLicenseStatus()" from a .NET-project it all works fine.
Here is the error I get when running the C++/CLI code:
> Unhandled Exception: System.BadImageFormatException: Could not load file or assembly
'SKCLNET.dll' or one of its dependencies. is not a valid Win32 application. (Exception
from HRESULT: 0x800700C1)
at Seat.Core.LicenseProvider..ctor()
at Seat.Core.LicenseProvider.get_Instance()
at LicenseCheck.ValidateLicense.GetLicenseStatus()
at main(String[] args) in C:\Users\deckenf\Desktop\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:line 13
at mainCRTStartupStrArray(String[] arguments) in
D:\a_work\1\s\src\vctools\crt\crtw32\msilcrt\mcrtexe.cpp:line 241
C:\Users\deckenf\Desktop\ConsoleApplication1\x64\Release\ConsoleApplication1.exe
(process 33364) exited with code -532462766.
Here the structure of the used dll:
Here the ValidateLicense class in the .NET dll with the function GetLicenseStatus(). This function is being invoked by C++/CLI.
public static class ValidateLicense
{
public static string GetLicenseStatus()
{
return LicenseProvider.Instance.CheckLicense().ToString();
}
}
Here some more details of the SKCLNET assembly:
答案1
得分: -1
经过两天的调查,我终于找到了问题。
正如@Hans Passant在评论中所说,问题是因为我将.exe构建为64位,尽管SKCLNET.dll需要32位进程而引起的。
由于SKCLNET.dll面向.NET v1.0.3705,还需要在C++/CLI项目中添加一个app.config文件,并添加useLegacyV2RuntimeActivationPolicy
标签。这是.NET Framework中的一个配置选项,用于控制使用混合模式程序集的应用程序的运行时激活策略。在.NET Framework 4及更高版本中,运行时激活策略已更改为新的默认行为。这个默认行为可能会导致一些旧应用程序无法正常工作,因为它们是为较早版本的.NET Framework设计的。
最后一个重要步骤是在构建后将这个app.config文件复制到目标路径。为此,右键单击C++/CLI项目 > 属性 > 构建 - 事件 > 构建后事件,然后在“命令行”参数中粘贴以下行:
copy app.config "$(TargetPath).config"
这篇文章对解决我的问题很有帮助。
英文:
After two days of investigation, I finally found the problem.
As @Hans Passant worte in the comments, the error in the question is caused because I built the .exe against 64-bit even though SKCLNET.dll requires 32-bit process.
Due to SKCLNET.dll targets .NET v1.0.3705 it was also necessary to add a app.config file to the C++/CLI project and add the useLegacyV2RuntimeActivationPolicy
-tag. It is a configuration option in .NET Framework that is used to control the runtime activation policy for applications that use mixed-mode assemblies. In .NET Framework 4 and later versions, the runtime activation policy was changed to a new default behavior. This default behavior can cause some older applications that were designed to work with earlier versions of the .NET Framework to fail.
The last important step was to copy this app.config file after the build in the target path. To do this right click on the C++/CLI project > Properties > Build -Events > Post-Build Event and for the Command Line
parameter you paste the following line:
copy app.config "$(TargetPath).config"
This article was helpful to solve my issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论