C# dll using C++ app (crossplatform)

Code Wanderer 396 Reputation points
2022-02-07T18:13:25.113+00:00

Hello,

I want use c# dll inside C++ program. I found several methods how to do it on internet, but there is no info (or I can't find it) how to do it crossplatform.

  • no C++/CLI, because it is only for Windows
  • maybe COM? but I am not sure if it is crossplatform

if possible I want export c# class and use it in C++ similar to C++ inside C#. https://learn.microsoft.com/en-us/xamarin/cross-platform/cpp/

also, can I provide functions to the C# dll from C++ and call it inside C#?
I also like to add some pointers for C# and use it inside C#.

I know there is Net. Core and If I know, it can produce native code? If yes, can I create with Net Core native dll and use it inside crossplatform C++ program?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,271 Reputation points Microsoft Vendor
    2022-02-08T08:59:40.413+00:00

    Hi @Code Wanderer ,

    For calling C# DLL you can refer to the COM method in the link.The tlb file is a com type library file, which contains interface-related information and is called by "#import xxx.tlb".

    1. You need to set the C# DLL to be registered for com interop.
    2. Copy the regasm.exe file to the dll directory.
    3. Open CMD, cd into the directory where the dll is located, and enter the command: regasm XXX.dll /tlb
    4. Import it into the C++ project with #import directive. ( #import "../../XXX/debug/XXX.tlb")

    Other questions about you:
    Using the CLR with reference to this link is a good approach, but it won't work cross-platform.
    You could write classes in a C# DLL and call them in C++, similarly, you could write functions in a C++ DLL and call them in C#, but I think you could write in C++ what needs to be called from C++, And write what needs to be called in C# in C#.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 56,021 Reputation points
    2022-02-09T16:41:03.937+00:00
    0 comments No comments