public static IntPtr GetPointer(object value) { if (value is int) { int iVal = (int)value; if (iVal >= 0) { return(new IntPtr(iVal)); } } if (value is BigInteger) { return(new IntPtr((long)(BigInteger)value)); } if (value is Int64) { return(new IntPtr((Int64)value)); } if (value == null) { return(IntPtr.Zero); } object asParam; if (PythonOps.TryGetBoundAttr(value, "_as_parameter_", out asParam)) { return(GetPointer(asParam)); } CTypes.SimpleCData sd = value as CTypes.SimpleCData; if (sd != null) { CTypes.SimpleType simpType = (CTypes.SimpleType)sd.NativeType; if (simpType._type == CTypes.SimpleTypeKind.WCharPointer || simpType._type == CTypes.SimpleTypeKind.CharPointer) { return(sd.UnsafeAddress); } else if (simpType._type == CTypes.SimpleTypeKind.Pointer) { return(sd._memHolder.ReadIntPtr(0)); } } CTypes._Array arr = value as CTypes._Array; if (arr != null) { return(arr.UnsafeAddress); } CTypes._CFuncPtr func = value as CTypes._CFuncPtr; if (func != null) { return(func.UnsafeAddress); } CTypes.Pointer pointer = value as CTypes.Pointer; if (pointer != null) { return(pointer.UnsafeAddress); } throw PythonOps.TypeErrorForTypeMismatch("pointer", value); }
public static IntPtr GetPointer(object value) { if (value == null) { return(IntPtr.Zero); } if (value is int iVal) { if (iVal > int.MaxValue) { iVal = -1; } return(new IntPtr(iVal)); } if (value is BigInteger bigVal) { if (bigVal > long.MaxValue) { bigVal = -1; } return(new IntPtr((long)bigVal)); } if (value is long lVal) { return(new IntPtr(lVal)); } if (PythonOps.TryGetBoundAttr(value, "_as_parameter_", out object asParam)) { return(GetPointer(asParam)); } if (value is CTypes.SimpleCData sd) { CTypes.SimpleType simpType = (CTypes.SimpleType)sd.NativeType; if (simpType._type == CTypes.SimpleTypeKind.WCharPointer || simpType._type == CTypes.SimpleTypeKind.CharPointer) { return(sd.UnsafeAddress); } else if (simpType._type == CTypes.SimpleTypeKind.Pointer) { return(sd._memHolder.ReadIntPtr(0)); } } if (value is CTypes._Array arr) { return(arr.UnsafeAddress); } if (value is CTypes._CFuncPtr func) { return(func.UnsafeAddress); } if (value is CTypes.Pointer pointer) { return(pointer.UnsafeAddress); } throw PythonOps.TypeErrorForTypeMismatch("pointer", value); }