Search code examples
c#.netdllc++-cli

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.

Here the structure of the used dll:

enter image description here

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:

enter image description here


Solution

  • 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"

    Picture shows how to automate copying the app.config file to target path.

    This article was helpful to solve my issue.