13
How to call code written in another language that is not part of . NET, for example the language C?
How this interaction takes place?
How to call native Windows functions?
13
How to call code written in another language that is not part of . NET, for example the language C?
How this interaction takes place?
How to call native Windows functions?
11
Yes. In fact much of . NET interoperates with Win32 or with the API of other operating systems that are provided in C. So it is possible for you to interoperate as well. We are usually talking about working with unmanaged code. Everything that occurs in it has no control of the CLR and in general memory allocations should be controlled by this code having no intervention of the Garbage Collector, so it’s a lot riskier. But there is a whole infrastructure to support communication in a simplified way between the managed and unmanaged part.
In fact it is possible to interact with much of the existing codes in any language as long as these codes meet certain requirements, which have ABI compatible. Any C code interacts well. C was thought about this and the operating system, whether Windows, Linux and most others are written or at least has its basic C API. It’s very easy to call C code from . NET and even make some parts of the code. NET can be called in C. An example is Sqlite which is written all in C and many people use it as an embedded database in the application . NET.
It is possible to call code in C++ too. And I’m not even talking about the C-compatible parts, I’m talking about the classes as well. There is the C++/CLI no. NET which is a managed C++ that interacts well with unmanaged C++ . Particularly if I can avoid I prefer, I find interaction with C much easier. So complicated that the UWP that is natively written in C++ uses an interaction with . NET a little different (CX).
Unmanaged code should be segregated into another DLL as they are very different binaries. In .NET Native no need. Then you need to load the DLL and create signatures for functions and structures available in unmanaged code for the C# compiler or other . NET language to recognize that function and detect errors. Something like that:
using System.Runtime.InteropServices;
public class Program {
[DllImport("teste.DLL", EntryPoint="print")]
static extern void print(string message);
public static void Main(string[] args) => print("Chamando C");
}
In C:
#include <stdio.h>
void print(const char *message) {
printf("%s", message);
}
Browser other questions tagged c# c .net windows clr
You are not signed in. Login or sign up in order to post.
I’m asking this question because I misunderstood the original version of https://answall.com/q/227301/101 and I answered this, so as not to lose the content I think is useful I’m posting here. If you have other questions post a new question.
– Maniero