Exemple #1
0
        public static void RunTest()
        {
            var testHandle = new TestSafeHandle(initialValue);

            Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue));
            Assert.False(testHandle.IsClosed);

            Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue));
            Assert.False(testHandle.IsClosed);

            testHandle = null;
            SafeHandleNative.SafeHandleOut(out testHandle, initialValue);
            Assert.False(testHandle.IsClosed);

            testHandle = SafeHandleNative.SafeHandleReturn(newValue);
            Assert.False(testHandle.IsClosed);

            testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue);
            Assert.False(testHandle.IsClosed);

            var str = new SafeHandleNative.StructWithHandle
            {
                handle = new TestSafeHandle(initialValue)
            };

            SafeHandleNative.StructWithSafeHandleByValue(str, initialValue);
            Assert.False(str.handle.IsClosed);

            SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue);
            Assert.False(str.handle.IsClosed);
        }
        public static void RunTest()
        {
            // Test that our SafeHandle-derived object has its underlying handle set after a P/Invoke
            // even if there's an exception during the unmarshal phase.
            IntPtr         value = (IntPtr)123;
            TestSafeHandle h     = new TestSafeHandle();

            Assert.Throws <InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(value, out h, out var cookie));

            Assert.AreEqual(value, h.DangerousGetHandle());
        }
Exemple #3
0
        public static void RunTest()
        {
            var testHandle = new TestSafeHandle(initialValue);

            Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue));

            Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue));
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            AbstractDerivedSafeHandle abstrHandle = new AbstractDerivedSafeHandleImplementation(initialValue);

            Assert.True(SafeHandleNative.SafeHandleInByRef(abstrHandle, initialValue));
            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleByRef(ref abstrHandle, initialValue, newValue));

            NoDefaultConstructorSafeHandle noDefaultCtorHandle = new NoDefaultConstructorSafeHandle(initialValue);

            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleByRef(ref noDefaultCtorHandle, initialValue, newValue));

            testHandle = null;
            SafeHandleNative.SafeHandleOut(out testHandle, initialValue);
            Assert.Equal(initialValue, testHandle.DangerousGetHandle());

            testHandle = SafeHandleNative.SafeHandleReturn(newValue);
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_AbstractDerived(initialValue));
            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_NoDefaultConstructor(initialValue));

            var abstractDerivedImplementationHandle = SafeHandleNative.SafeHandleReturn_AbstractDerivedImplementation(initialValue);

            Assert.Equal(initialValue, abstractDerivedImplementationHandle.DangerousGetHandle());

            testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue);
            Assert.Equal(newValue, testHandle.DangerousGetHandle());

            Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_Swapped_AbstractDerived(initialValue));
            Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_Swapped_NoDefaultConstructor(initialValue));

            var str = new SafeHandleNative.StructWithHandle
            {
                handle = new TestSafeHandle(initialValue)
            };

            Assert.True(SafeHandleNative.StructWithSafeHandleByValue(str, initialValue));

            Assert.True(SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue));

            // Cannot change the value of a SafeHandle-derived field in a struct when marshalling byref.
            Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, newValue));

            // Cannot create a SafeHandle-derived field value.
            Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleOut(out var defaultOutStruct, initialValue));
        }
Exemple #4
0
        public static void RunTest()
        {
            // Test that our SafeHandle-derived object has its underlying handle set after a P/Invoke
            // even if there's an exception during the unmarshal phase.
            IntPtr         value = (IntPtr)123;
            TestSafeHandle h     = new TestSafeHandle();

            Assert.Throws <InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(out _, value, out h));

            Assert.AreEqual(value, h.DangerousGetHandle());

            // Try again, this time triggering unmarshal failure with an array.
            value = (IntPtr)456;
            h     = new TestSafeHandle();

            Assert.Throws <OverflowException>(() => SafeHandleNative.GetHandleAndArray(out _, out _, value, out h));

            Assert.AreEqual(value, h.DangerousGetHandle());
        }
Exemple #5
0
 public static extern void GetHandleAndArray(out short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out short[] arrShort, IntPtr value, out TestSafeHandle handle);
Exemple #6
0
 public static extern void GetHandleAndCookie([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ThrowingCustomMarshaler), MarshalCookie = "")] out object cookie, IntPtr value, out TestSafeHandle handle);
Exemple #7
0
 public static extern void SafeHandleOut(out TestSafeHandle handle, IntPtr expectedValue);
Exemple #8
0
 public static extern bool SafeHandleByRef(ref TestSafeHandle handle, IntPtr expectedValue, IntPtr newValue);
Exemple #9
0
 public static extern bool SafeHandleByValue(TestSafeHandle handle, IntPtr expectedValue);
Exemple #10
0
 public static extern void SafeHandle_Invalid([MarshalAs(UnmanagedType.Interface)] TestSafeHandle handle);