2

Due to third-party software dependencies, I cannot go into details about, we need the ability to create a DotNet standard class object written in C# from within a C++ project compiled without the /clr flag and invoke methods on that object. Can this be done?

To be clear, the DotNet standard is a separate assembly which has objects and methods to be invoked from VC++ DLL compiled without the /clr flag. Basically, we need to deploy the assemblies on machines that don't have the full DotNet Framework Runtimes, but will have DotNet Core/Standard SDK installed (or will be deployed as self-contained deployment).

Please don't respond with "try this", unless you know that the interop approach actually works in the above scenario.

1 Answer 1

2

There are a variety of ways to interop with .NET from C++, some of which don't require /clr. However, none of them will help avoid

we need to deploy the assemblies on machines that don't have the full DotNet Framework Runtimes

If you have an assembly containing MSIL, you'll need .NET Framework installed (at least the Client Profile). That's because these assemblies do not contain executable code, only a portable description that serves as input to the .NET runtime's Just-in-Time (JIT) compiler.


There are a couple toolchains that can compile from C# to native code, with varying levels of required runtime support, but none that would be considered "production quality" when working with arbitrary .NET Framework libraries. (.NET Native would work for libraries targeted to WinRT). And there are significant limits to the code that these tools can process (for example, reflection is severely limited, only introspection, no runtime code generation or even composition of types). These limits would apply to not just your code but all your dependencies.


If you are deploying the version of .NET required by your assembly (Note: question was edited to say .NET Core, instead of just "don't have .NET"), then you can use the Hosting API which says "this tutorial will cover constructing a C++ application to host .NET Core"

In the process, you'll dynamically link (GetProcAddress) functions such as coreclr_initialize, coreclr_execute_assembly, and coreclr_create_delegate. The latter of these is described as "Creates a function pointer to a managed method."

It appears that this API does not support calling constructors, because the designers considered it trivial to write a factory function as a static method, which coreclr_create_delegate does support.

Sign up to request clarification or add additional context in comments.

2 Comments

@ben-voight, interesting you say the Full Framework is required. To my understanding system-wide .Net Core SDK should satisfy the JIT requirement when .Net Standard assemblies are deployed either as Framework-dependent or self-contained. I'm trying to understand this better, not contesting what you said.
@batta: Which profile is needed will depend on what classes you and your dependencies use. And SDK will not help, you need the runtime. If you want to use a .NET Core runtime, you have to target .NET Core during the build. .NET Standard assemblies depend on a version of mscorlib and System.dll that is different and incompatible with the one coming from Core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.