[Mono-list] How best to detect if running in mono from an unmanaged library?

Robert Jordan robertj at gmx.net
Wed Apr 30 19:19:38 EDT 2008


Maser, Dan wrote:
>   I've got a situation where my managed app p/invokes to my unmanaged
> library.  The unmanaged library also gets used in regular unmanaged
> processes.
> 
>   The library has some logic that uses the name of the executable.
> Which doesn't what I want when it's used via the interop.  For example
> when the library is used from "my_regular_program.exe" it generates an
> output file of "my_regular_program.output".  When I interop to that same
> library and run under mono using the command line "mono my_assembly.exe"
> the library generates "mono.output" when naturally I'd prefer
> "my_assembly.output".
> 
>   Is there a preferred way to determine whether the process is running
> under mono?  I could string-match the executable name but that won't
> catch places where people compile mono with a more descriptive name such
> as "/usr/local/bin/mono-v1.9" or something like that.

For unix:

#include <dlfcn.h>

/* let's assume this is a function exposed to mono: */
void test ()
{
	/* get the address of one of mono's API functions */
	void *p = dlsym (NULL, "mono_runtime_invoke");
	if (p) {
		inside mono ...
	}
}

The code for Windows is slightly complicated. See
mono/utils/mono-dl.c:w32_find_symbol().

Robert



More information about the Mono-list mailing list