Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto call c# (mono , .net) methods, delegates from native c

is it possible to call c# methods written in managed code (maybe in a class or a library) from a native c code (and how)?

thx

edit: with "c#" i mostly refer to mono or even portable.net and the OS is Linux

like image 266
Gobliins Avatar asked Jan 21 '23 01:01

Gobliins


2 Answers

Your C code can define functions to register callbacks. The C# code can P/Invoke these functions, and pass managed delegates as arguments. The marshalling code will transparently transform these to C function pointers.

Alternatively, approaching it from the C side, you can use the Mono embedding API to load assemblies, look up MonoMethods, and invoke them.

Using the embedding API is much more complicated. If your entry point is in C, you'll have to use the embedding API, but it's likely easier to simply write a managed method to do the callback registration and any other managed setup, then you only have to load and invoke that single method from C code.

like image 150
Mikayla Hutchinson Avatar answered Jan 23 '23 14:01

Mikayla Hutchinson


There's an Overview of Managed/Unmanaged Code Interoperability on the MSDN site that might shed some light for you. An excerpt below:

Directly Accessing a Managed API

If an unmanaged client is written in C++, it can be compiled with the Visual Studio .NET C++ compiler as a "mixed mode image." After this is done, the unmanaged client can directly access any managed API. However, some coding rules do apply to accessing managed objects from unmanaged code; check the C++ documentation for more details.

Direct access is the preferred option since it does not require any special consideration from managed API developers. They can design their managed API according to managed API design guidelines (DG) and be confident that the API will still be accessible to unmanaged callers.

Exposing a Managed API as a COM API

Every public managed class can be exposed to unmanaged clients through COM interop. This process is very easy to implement, because the COM interop layer takes care of all the COM plumbing. Thus, for example, every managed class appears to implement IUnknown, IDispatch, ISupportErrorInfo, and a few other standard COM interfaces.

Despite the fact that exposing managed APIs as COM APIs is easy, managed and COM object models are very different. Therefore, exposing managed API to COM should always be an explicit design decision. Some features available in the managed world have no equivalent in the COM world and will not be usable from COM clients. Because of this, there is often tension between managed API design guidelines (DG) and compatibility with COM.

If COM clients are important, write your managed API according to the managed API design guidelines, and then write a thin COM-friendly managed wrapper around your managed API that will be exposed to COM.

Exposing a Managed API as a Flat API

Sometimes unmanaged clients cannot use COM. For example, they might already be written to use flat APIs and cannot be changed or recompiled. C++ is the only high-level language that allows you to expose managed APIs as flat APIs. Doing this is not as straightforward as exposing a managed API as a COM API. It is a very advanced technique that requires advanced knowledge of C++ interop and the differences between the managed and unmanaged worlds.

Expose your managed API as a flat API only if absolutely necessary. If you have no choice, be sure to check the C++ documentation and be fully aware of all the limitations.

like image 37
Lazarus Avatar answered Jan 23 '23 14:01

Lazarus