-1

I have a third party provider who provide me a set of DLL files as part of their .NET API. They enclose a Python and C# example. I have been using their API via Python (using pythonnet) for a while.

The C# workflow in comparison is dotnet build and dotnet run. This appears to generate a .dll and execute that.

Now, for performance reasons, I want to use their API in Native C++. I am trying to build a C++/CLI project in Visual Studio 2022. I follow these steps

  1. Create a C++/CLI project target a .NET Core 5.0 (because the C# example does that)
  2. Add their .dll files as Reference

I am able to refer to their objects, with appropriate definitions, and the Intellisense typechecker is satisfied. But when I try to execute the code, it gives me an error

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

I do not think the exact error is relevant, I am simply out of depth about how .net works and if it even makes to do what I am trying to do.

How do I achieve an equivalent C++ project setup with interop?

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Cheeku
  • 833
  • 1
  • 8
  • 22
  • "I want to use their API in Native C++. I am trying to build a C++/CLI project" - How is C++/CLI in any way "native C++"? It is a separate language. – Jesper Juhl Apr 27 '23 at 13:45
  • 1
    That should not be a problem. A typical install location for an x64 program is C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.11\netstandard.dll, check if it is still there. It is an expected failure if you target the legacy framework (4.x), quite likely to happen when you selected the Console App project template to get started since creating such an app is no longer directly supported for .net5+ in recent VS versions. Double-check Project > Properties > Advanced > ".NET Target Framework Version". – Hans Passant Apr 27 '23 at 13:57
  • @JesperJuhl I was coming from the perspective that I can write native C++ "alongside" using their code. The idea is to maintain modularity of my own modules. If I have my own C++ modules, what is the ideal path to work with a .net API? – Cheeku Apr 27 '23 at 14:02
  • @HansPassant Thank you, this took me one step further, but then a new error "Could not load type 'System.Object' from because the parent does not exist." At this point, I am browsing the general best approach for my situation, that is, I want to write native C++ code but want to interact with a .NET API – Cheeku Apr 27 '23 at 14:44

1 Answers1

0

Try creating the C++/CLI project as a .NET Framework v4.8 project.

I assume there is a C# project that consumes your C++/CLI project/assembly as a dependency; if so it shouldn't matter if the C# project is .NET 5, it should still be able to consume the C++/CLI project.

On my current project I use cmake the target is set up like this

set_target_properties(${TARGET} PROPERTIES
    OUTPUT_NAME ${TARGET}
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
    COMMON_LANGUAGE_RUNTIME ""
    VS_GLOBAL_CLRSupport "true"
    VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.8"
)

If it's not possible to use .NET Framework v4.8 then this answer will help: https://stackoverflow.com/a/73332218/783051

Peter Tran
  • 1,626
  • 1
  • 17
  • 26