8

I am trying to build an application having below 3 layers

  1. C# Layer
  2. C++/CLI Wrapper layer
  3. C++ Layer

I have build C# and C++/CLI layers in .Net Core 3.1. But I am not able to publish this in Linux platform.

From the Microsoft press release for .Net Core 3.1, its saying like .Net core C++/CLI will support only for Windows.

Is there any other way that I can achieve platform independency at C++/CLI layer?

or is there any other way that I can communicate with C++ Layer directly from C#?

I tried to use pInvoke methode. But it cannot transfer C# object to C++.

Target to achieve is that with the same source code it should able to build in Windows, Linux and Mac OS without using any third party services.

Note: For a C# application build in .NetCore we can achieve this. Only thing we need to do is publish the project for different platform.

  • 2
    Yes you can, by docker – marsahllDTitch Jan 03 '20 at 10:03
  • Hi @mehdizahrane , docker is like a Linux container right? Without using any other third party services need to achieve this platform independency. The application should work in Linux, Windows and Mac with same source code. –  Jan 03 '20 at 10:16
  • 2
    PInvoke is your only option. `C++/CLI` can only be used in Windows. Can you post some code to show us what you're trying to do? What functions are you attempting to call from `C#` to `C++` – WBuck Jan 03 '20 at 10:16
  • Hi @WBuck, there will be some classes with some functionality in C++ layer. I am creating the instance of C++ class and calling those functionality from C# via CLI layer –  Jan 03 '20 at 10:29
  • 1
    No. https://stackoverflow.com/questions/39140014/c-cli-support-in-net-core/58071675#58071675 – lars Jan 03 '20 at 14:44
  • 1
    @marsahllDTitch docker for windows is different than docker for linux. its a NO. – Shahid Roofi Khan Sep 03 '22 at 15:25

1 Answers1

0

You can transfer C# objects to C++ trough GCHandle. You also have to transfer pointers to C# functions into unmanaged code (get them via GetFunctionPointerForDelegate).

Then in your unmanaged code you can call that function and pass the object handle as one of the arguments.

An example of C# code

public static unsafe void Invoke(System.IntPtr instancePtr, int param)
{
    var instanceHandle = GCHandle.FromIntPtr(instancePtr);
    var instance = ((ClassInstance) instanceHandle.Target);

    instance.method(param);
}

So you have to transfer a pointer to Invoke function and an IntPtr to GCHandle (GCHandle.ToIntPtr) into C++ code.

Artyom Chirkov
  • 233
  • 1
  • 10