コード例 #1
0
        public static void Run()
        {
            Console.WriteLine("----- String marshaling samples -----");

            MarshalingSampleNative.CountBytesInString(null);          // returns -1
            MarshalingSampleNative.CountBytesInString("some string"); // returns 11
        }
コード例 #2
0
        public static void Run()
        {
            Console.WriteLine("----- Int32 marshaling samples -----");

            // Always start with the same value.
            const int initialValue = 7;
            int       value;

            // Pass Int32 argument by value.
            value = initialValue;
            MarshalingSampleNative.AcceptInt32Argument(value); // returns 7

            // Pass Int32 argument by refernece.
            value = initialValue;
            MarshalingSampleNative.AcceptInt32ByRefArgument(ref value); // returns 7

            // Get Int32 out parameter.
            MarshalingSampleNative.GetInt32OutArgument(out value); // sets value to 9

            // Pass Int32 in-out argument by reference.
            value = initialValue;
            MarshalingSampleNative.ModifyInt32InOutArgument(ref value);

            // Return Int32 value.
            value = MarshalingSampleNative.ReturnInt32Argument(initialValue);
        }
コード例 #3
0
        public static void Run()
        {
            Console.WriteLine("----- Enum marshaling samples -----");

            // return 2
            int count = MarshalingSampleNative.CountEnumFlags(
                MarshalingSampleNative.EnumFlags.A | MarshalingSampleNative.EnumFlags.C);
        }
コード例 #4
0
        public static void Run()
        {
            Console.WriteLine("----- GUID marshaling samples -----");

            Guid a = new Guid("11111111-2222-3333-4444-556677889900");
            Guid b = new Guid("aaaaaaaa-bbbb-cccc-dddd-eeff77889900");

            MarshalingSampleNative.CompareGuids(a, b); // returns 0
            MarshalingSampleNative.CompareGuids(a, a); // returns 1

            Guid inOutRef = Guid.Empty;
            Guid outRef;
            Guid result = MarshalingSampleNative.CountZeroGuids(a, Guid.Empty, ref b, ref inOutRef, out outRef);
        }
コード例 #5
0
        public static void Run()
        {
            Console.WriteLine("----- Boolean marshaling samples -----");

            const bool initialValue = true;
            bool       value;

            // Pass boolean by value and masrshal it as "Windows" BOOL - which is 32bit int.
            value = initialValue;
            MarshalingSampleNative.AcceptBOOLArgument(value);  // returns 1
            MarshalingSampleNative.AcceptBOOLArgument(false);  // returns 0

            // Pass boolean by reference
            value = initialValue;
            MarshalingSampleNative.AcceptBOOLByRefArgument(ref value);

            // Get boolean out parameter
            MarshalingSampleNative.GetBOOLOutArgument(out value); // sets value to false

            // Pass boolean in-out argument by reference.
            value = initialValue;
            MarshalingSampleNative.ModifyBOOLInOutArgument(ref value); // sets value to false

            // Return boolean value.
            value = MarshalingSampleNative.ReturnBOOLArgument(value);

            // Marshal boolean value in various ways
            int count;

            value = initialValue;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                count = MarshalingSampleNative.CountTrueValuesWindows(!value, value, !value, value); // returns 2
            }
            else
            {
                count = MarshalingSampleNative.CountTrueValues(!value, value, !value); // returns 1
            }
        }
コード例 #6
0
        public static void Run()
        {
            Console.WriteLine("----- Numeric marshaling samples -----");

            { // byte
                // Result is 6, both inOutRef and outRef are also set to 6
                byte inRef = 2, inOutRef = 3, outRef;
                byte result = MarshalingSampleNative.SumBytes(1, ref inRef, ref inOutRef, out outRef);
            }

            { // sbyte
                // Result is -6, both inOutRef and outRef are also set to -6
                sbyte inRef = -2, inOutRef = -3, outRef;
                sbyte result = MarshalingSampleNative.SumSBytes(-1, ref inRef, ref inOutRef, out outRef);
            }

            { // ushort
                // Result is 6, both inOutRef and outRef are also set to 6
                ushort inRef = 2, inOutRef = 3, outRef;
                ushort result = MarshalingSampleNative.SumUShorts(1, ref inRef, ref inOutRef, out outRef);
            }

            { // short
                // Result is -6, both inOutRef and outRef are also set to -6
                short inRef = -2, inOutRef = -3, outRef;
                short result = MarshalingSampleNative.SumShorts(-1, ref inRef, ref inOutRef, out outRef);
            }

            { // uint
                // Result is 6, both inOutRef and outRef are also set to 6
                uint inRef = 2, inOutRef = 3, outRef;
                uint result = MarshalingSampleNative.SumUInts(1, ref inRef, ref inOutRef, out outRef);
            }

            { // int
                // Result is -6, both inOutRef and outRef are also set to -6
                int inRef = -2, inOutRef = -3, outRef;
                int result = MarshalingSampleNative.SumInts(-1, ref inRef, ref inOutRef, out outRef);
            }

            { // ulong
                // Result is 6, both inOutRef and outRef are also set to 6
                ulong inRef = 2, inOutRef = 3, outRef;
                ulong result = MarshalingSampleNative.SumULongs(1, ref inRef, ref inOutRef, out outRef);
            }

            { // long
                // Result is -6, both inOutRef and outRef are also set to -6
                long inRef = -2, inOutRef = -3, outRef;
                long result = MarshalingSampleNative.SumLongs(-1, ref inRef, ref inOutRef, out outRef);
            }

            { // float
                // Result is roughly 0.123, both inOutRef and outRef are also set to roughly 0.123
                float inRef = 0.02f, inOutRef = 0.003f, outRef;
                float result = MarshalingSampleNative.SumFloats(0.1f, ref inRef, ref inOutRef, out outRef);
            }

            { // double
                // Result is roughly 0.123, both inOutRef and outRef are also set to roughly 0.123
                double inRef = 0.02f, inOutRef = 0.003f, outRef;
                double result = MarshalingSampleNative.SumDoubles(0.1f, ref inRef, ref inOutRef, out outRef);
            }

            { // decimals
                // Result is 6, both inOutRef and outRef are also set to 6
                decimal inRef = 2, inOutRef = 3, outRef;
                decimal result = MarshalingSampleNative.SumDecimals(1m, ref inRef, ref inOutRef, out outRef);
            }
        }