I’m attempting to wrap some native C++ code in a C++/CLI (CLR) library (TestClrLibrary.NetCore
) in order to use it from a NET6 application. Everything built fine, and I was able to reference my library from another project (NET6 application) in the same solution. However, when I run the application, I always get the following error:
System.IO.FileNotFoundException: Could not load file or assembly 'TestClrLibrary.NetCore, Version=1.0.8252.18682, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'TestClrLibrary.NetCore, Version=1.0.8252.18682, Culture=neutral, PublicKeyToken=null'
at TestConsole.NetCore.Program.Main(String[] args)
I’m using VS2022, and created both projects with default settings for NET6. The header/source for the library look like this:
// TestClrLibrary.NetCore.h
#pragma once
using namespace System;
namespace TestClrLibraryNetCore {
public ref class TestClass
{
public:
void Open(System::String^ authority);
};
}
// TestClrLibrary.NetCore.cpp
#include "pch.h"
#include "TestClrLibrary.NetCore.h"
namespace TestClrLibraryNetCore {
void TestClass::Open(System::String^ authority)
{
}
}
The NET6 console application:
// Program.cs
namespace TestConsole.NetCore
{
internal class Program
{
static void Main(string[] args)
{
var test = new TestClrLibraryNetCore.TestClass();
test.Open("arg0");
Console.WriteLine($"Created test object.");
}
}
}
The library doesn’t actually do anything at this point, and it has no references beyond what are created when Visual Studio creates the project. The library build settings definitely use the /clr:netcore
switch, and TargetFramework
for both projects is net6.0
. Both are x64
, so I know that matches (the usual cause of errors like these).
This does seem to be a .Net Core / NET6 issue, because I can recreate both library & console exe projects targeting .Net Framework 4.8
and run the resulting application with no issues.
Is there something I’m missing? What does it take to get the simplest imaginable C++/CLR library built for use in a NET6 application?
2
Answers
Several months later, I came back to this project. After updating Visual Studio 2022 to version 17.5.2 and moving from net6 to net7, it started working.
This isn't a very satisfying answer, as I never did figure out why net6 didn't work. But, at least any of the dozen or so people using C++/CLR/CLI will know how to keep their work rolling forward.
EDIT: Just out of curiosity, I decided to install the latest net6 version (6.0.15) and take another run at c++/clr in net6. It worked.
I created a VS project with your source and a C# console project with your source. I added a reference to the library from the console app and hit a build error about being unable to mix different versions of MSIL.
I consulted a working project I have and the difference was in the .vcxproj settngs for the library. I needed to change the
<TargetFramework>
value fromnet6.0
tonet6.0-windows
. With this change, I was able to build and run your example:<TargetFramework>net6.0-windows</TargetFramework>
Note, that I could not build the Program.cs from the VS 2022 PowerShell b/c csc.exe there targets .NET Framework. I could maybe find an incantation with
dotnet build
but didn’t go there…Hope this helps.