How to test at runtime if my dll is running inside Rhino or not?

How can I test at runtime if my dll is inside Rhino or not.
Basically I want to know if I can call into the unsafeNativeMethods (like most are) of RhinoCommon.dll or not.

I think that

bool Rhino.Runtime.HostUtils.RunningInRhino

should be used for this.

Unfortunately, if I run this outside Rhino it still returns true. @stevebaer may know why this is (possible bug?)

A better test is (I just found this)

if (null == RhinoApp.MainApplicationWindow)
{
    Console.WriteLine("Not running in Rhino");
}
else
{
    Console.WriteLine("Running in Rhino");
}

I do it via exception handeling now in f#:

let isRhinoRunning =
    try 
        Rhino.RhinoDoc.ActiveDoc |> ignore // this line will fail if not in RH
        true
    with 
        |e -> false

It depends which type of exception is thrown. An AccessViolationException can lead to instabilities and may not be caught.

Rhino.Runtime.HostUtils.RunningInRhino 

works correctly for me in F# Interactive in VS.

trying to access the ActiveDoc throws

System.BadImageFormatException

or

System.TypeInitializationException

How are you testing this? RunningInRhino is the correct property to check and it should be returning false when not inside the Rhino process.

I copy RhinoCommon and its 8 dependencies (rhcommon_c.dll, Rhino.exe, etc.) to my output folder and run it through NUnit integrated in VS2010 by ReSharper. (I hope this make sense :smile:)

Most of the RhinoCommon functionality is available, but some give AccessViolationException.

Ok; I’ll have to look into this a bit more to see how on earth this is returning true in that case.