[Mono-list] How to call managed from unmanaged code?

Dietmar Maurer dietmar@ximian.com
02 Sep 2002 20:00:21 +0200


On Mon, 2002-09-02 at 16:04, Holger Arnold wrote:
> Hello,
> 
> I know that P/Invoke is the standard way to call unmanaged functions from 
> managed code. But what is the standard way to do call managed code from 
> unmanaged code? 
> 
> (1) The MS.NET documentation describes how I can use a delegate as a 
> parameter to a P/Invoke call, which is then marshalled to a native function 
> pointer on the unmanaged side? Is this functionality already fully 
> implemented in Mono? 

It is implemented for most types.
 
> (2) What if a marshalled delegate is called asynchronously, after the call 
> which originally transported the delegate to the unmanaged side returned? Is 
> it safe to call into a running Mono runtime from a different thread?

You need to hold a reference to the delegate in managed code, else the
GC will free the delegate. If you need synchronisation you need to sync
as usual.

> 
> (3) Are there any other (possibly Mono-specific) mechanisms to call from 
> unmanaged to managed code, assuming that both are running within a single 
> process?

yes, you can use internal calls. See mono/samples/embed/teste.c for an
example.

- Dietmar