예제 #1
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));
        }
예제 #2
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(value, out h, out var cookie));

            Assert.AreEqual(value, h.DangerousGetHandle());
        }
예제 #3
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());
        }