Switch to SafeHandles for mono compatibility.

Mono 3.4 throws a MarshalDirectiveException: "Type System.Runtime.InteropServices.CriticalHandle which is passed to unmanaged code must have a StructLayout attribute." when trying to marshal a CriticalHandle.