//------------------------------------------------------ // // Internal Methods // //------------------------------------------------------ #region Internal Methods internal void SetMarshalData <T>(T uiaCondition) where T : struct { // Takes one of the interop UiaCondition classes (from UiaCoreApi.cs), and allocs // a SafeHandle with associated unmanaged memory - can then pass that to the UIA APIs. _safeHandle = SafeConditionMemoryHandle.AllocateConditionHandle(uiaCondition); }
// used by And/Or conditions to allocate an array of pointers to other conditions internal static SafeConditionMemoryHandle AllocateConditionArrayHandle(Condition [] conditions) { // Allocate SafeHandle first to avoid failure later. SafeConditionMemoryHandle sh = new SafeConditionMemoryHandle(); int intPtrSize = Marshal.SizeOf(typeof(IntPtr)); try { } finally { IntPtr mem = Marshal.AllocCoTaskMem(conditions.Length * intPtrSize); sh.SetHandle(mem); } unsafe // Suppress "Exposing unsafe code thru public interface" UiaCoreApi is trusted #pragma warning suppress 56505 { IntPtr *pdata = (IntPtr *)sh.handle; for (int i = 0; i < conditions.Length; i++) { *pdata++ = conditions[i]._safeHandle.handle; } } return(sh); }
// used by And/Or conditions to allocate an array of pointers to other conditions internal static SafeConditionMemoryHandle AllocateConditionArrayHandle(Condition [] conditions) { // Allocate SafeHandle first to avoid failure later. SafeConditionMemoryHandle sh = new SafeConditionMemoryHandle(); int intPtrSize = Marshal.SizeOf(typeof(IntPtr)); #pragma warning disable SYSLIB0004 // The Constrained Execution Region (CER) feature is not supported. RuntimeHelpers.PrepareConstrainedRegions(); // ensures that the following finally block is atomic #pragma warning restore SYSLIB0004 // The Constrained Execution Region (CER) feature is not supported. try { } finally { IntPtr mem = Marshal.AllocCoTaskMem(conditions.Length * intPtrSize); sh.SetHandle(mem); } unsafe // Suppress "Exposing unsafe code thru public interface" UiaCoreApi is trusted #pragma warning suppress 56505 { IntPtr *pdata = (IntPtr *)sh.handle; for (int i = 0; i < conditions.Length; i++) { *pdata++ = conditions[i]._safeHandle.handle; } } return(sh); }
// uiaCondition is one of the Uia condition structs - eg UiaCoreApi.UiaAndOrCondition internal static SafeConditionMemoryHandle AllocateConditionHandle(object uiaCondition) { // Allocate SafeHandle first to avoid failure later. SafeConditionMemoryHandle sh = new SafeConditionMemoryHandle(); int size = Marshal.SizeOf(uiaCondition); try { } finally { IntPtr mem = Marshal.AllocCoTaskMem(size); sh.SetHandle(mem); } Marshal.StructureToPtr(uiaCondition, sh.handle, false); return(sh); }
// uiaCondition is one of the Uia condition structs - eg UiaCoreApi.UiaAndOrCondition internal static SafeConditionMemoryHandle AllocateConditionHandle(object uiaCondition) { // Allocate SafeHandle first to avoid failure later. SafeConditionMemoryHandle sh = new SafeConditionMemoryHandle(); int size = Marshal.SizeOf(uiaCondition); RuntimeHelpers.PrepareConstrainedRegions(); // ensures that the following finally block is atomic try { } finally { IntPtr mem = Marshal.AllocCoTaskMem(size); sh.SetHandle(mem); } Marshal.StructureToPtr(uiaCondition, sh.handle, false); return(sh); }
//------------------------------------------------------ // // Constructors // //------------------------------------------------------ #region Constructors /// <summary> /// Constructor to create a condition that is true if all of the sub-conditions are true /// </summary> /// <param name="conditions">One or more sub-condition</param> public AndCondition(params Condition [] conditions) { Misc.ValidateArgumentNonNull(conditions, "conditions"); Misc.ValidateArgument(conditions.Length >= 2, SRID.MustBeAtLeastTwoConditions); foreach (Condition condition in conditions) { Misc.ValidateArgumentNonNull(condition, "conditions"); } // clone array to prevent accidental tampering _conditions = (Condition [] )conditions.Clone(); _conditionArrayHandle = SafeConditionMemoryHandle.AllocateConditionArrayHandle(_conditions); // DangerousGetHandle() reminds us that the IntPtr we get back could be collected/released/recycled. We're safe here, // because the Conditions are structured in a tree, with the root one (which gets passed to the Uia API) keeping all // others - and their associated data - alive. (Recycling isn't an issue as these are immutable classes.) SetMarshalData(new UiaCoreApi.UiaAndOrCondition(UiaCoreApi.ConditionType.And, _conditionArrayHandle.DangerousGetHandle(), _conditions.Length)); }
// uiaCondition is one of the Uia condition structs - eg UiaCoreApi.UiaAndOrCondition internal static SafeConditionMemoryHandle AllocateConditionHandle(object uiaCondition) { // Allocate SafeHandle first to avoid failure later. SafeConditionMemoryHandle sh = new SafeConditionMemoryHandle(); int size = Marshal.SizeOf(uiaCondition); #pragma warning disable SYSLIB0004 // The Constrained Execution Region (CER) feature is not supported. RuntimeHelpers.PrepareConstrainedRegions(); // ensures that the following finally block is atomic #pragma warning restore SYSLIB0004 // The Constrained Execution Region (CER) feature is not supported. try { } finally { IntPtr mem = Marshal.AllocCoTaskMem(size); sh.SetHandle(mem); } Marshal.StructureToPtr(uiaCondition, sh.handle, false); return(sh); }