Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# dll in C++ code

I need to integrate this C# dll in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.

like image 679
Sunrise Avatar asked Oct 02 '13 18:10

Sunrise


People also ask

How do I use C on my computer?

It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).

What is C used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

How do I start learning C?

Get started with C. Official C documentation - Might be hard to follow and understand for beginners. Visit official C Programming documentation. Write a lot of C programming code - The only way you can learn programming is by writing a lot of code.

Is C hard to learn?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

There are basically two cases to call a .NET DLL from unmanaged code:

  1. The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
  2. The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):

    2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
    2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.

I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.

PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

like image 127
Simon Mourier Avatar answered Oct 03 '22 18:10

Simon Mourier