예제 #1
0
        unsafe public void CopyFromIndirect(object value)
        {
            VarEnum vt = (VarEnum)(((int)this.VariantType) & ~((int)VarEnum.VT_BYREF));

            if (value == null)
            {
                if (vt == VarEnum.VT_DISPATCH || vt == VarEnum.VT_UNKNOWN || vt == VarEnum.VT_BSTR)
                {
                    *(IntPtr *)this._typeUnion._unionTypes._byref = IntPtr.Zero;
                }
                return;
            }

            // the quirk specifies that this should only fire when run on the latest version
            if (!AppContextSwitches.DoNotMarshalOutByrefSafeArrayOnInvoke && (vt & VarEnum.VT_ARRAY) != 0)
            {
                Variant vArray;
                Marshal.GetNativeVariantForObject(value, (IntPtr)(void *)&vArray);
                *(IntPtr *)this._typeUnion._unionTypes._byref = vArray._typeUnion._unionTypes._byref;
                return;
            }

            switch (vt)
            {
            case VarEnum.VT_I1:
                *(sbyte *)this._typeUnion._unionTypes._byref = (sbyte)value;
                break;

            case VarEnum.VT_UI1:
                *(byte *)this._typeUnion._unionTypes._byref = (byte)value;
                break;

            case VarEnum.VT_I2:
                *(short *)this._typeUnion._unionTypes._byref = (short)value;
                break;

            case VarEnum.VT_UI2:
                *(ushort *)this._typeUnion._unionTypes._byref = (ushort)value;
                break;

            case VarEnum.VT_BOOL:
                *(short *)this._typeUnion._unionTypes._byref = (bool)value ? (short)-1 : (short)0;
                break;

            case VarEnum.VT_I4:
            case VarEnum.VT_INT:
                *(int *)this._typeUnion._unionTypes._byref = (int)value;
                break;

            case VarEnum.VT_UI4:
            case VarEnum.VT_UINT:
                *(uint *)this._typeUnion._unionTypes._byref = (uint)value;
                break;

            case VarEnum.VT_ERROR:
                *(int *)this._typeUnion._unionTypes._byref = ((ErrorWrapper)value).ErrorCode;
                break;

            case VarEnum.VT_I8:
                *(Int64 *)this._typeUnion._unionTypes._byref = (Int64)value;
                break;

            case VarEnum.VT_UI8:
                *(UInt64 *)this._typeUnion._unionTypes._byref = (UInt64)value;
                break;

            case VarEnum.VT_R4:
                *(float *)this._typeUnion._unionTypes._byref = (float)value;
                break;

            case VarEnum.VT_R8:
                *(double *)this._typeUnion._unionTypes._byref = (double)value;
                break;

            case VarEnum.VT_DATE:
                *(double *)this._typeUnion._unionTypes._byref = ((DateTime)value).ToOADate();
                break;

            case VarEnum.VT_UNKNOWN:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.GetIUnknownForObject(value);
                break;

            case VarEnum.VT_DISPATCH:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.GetIDispatchForObject(value);
                break;

            case VarEnum.VT_BSTR:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.StringToBSTR((string)value);
                break;

            case VarEnum.VT_CY:
                *(long *)this._typeUnion._unionTypes._byref = decimal.ToOACurrency((decimal)value);
                break;

            case VarEnum.VT_DECIMAL:
                *(decimal *)this._typeUnion._unionTypes._byref = (decimal)value;
                break;

            case VarEnum.VT_VARIANT:
                Marshal.GetNativeVariantForObject(value, this._typeUnion._unionTypes._byref);
                break;

            default:
                throw new ArgumentException("invalid argument type");
            }
        }
예제 #2
0
        public unsafe void CopyFromIndirect(object value)
        {
            VarEnum vt = (VarEnum)(((int)this.VariantType) & ~((int)VarEnum.VT_BYREF));

            if (value == null)
            {
                if (vt == VarEnum.VT_DISPATCH || vt == VarEnum.VT_UNKNOWN || vt == VarEnum.VT_BSTR)
                {
                    *(IntPtr *)this._typeUnion._unionTypes._byref = IntPtr.Zero;
                }
                return;
            }

            if ((vt & VarEnum.VT_ARRAY) != 0)
            {
                Variant vArray;
                Marshal.GetNativeVariantForObject(value, (IntPtr)(void *)&vArray);
                *(IntPtr *)this._typeUnion._unionTypes._byref = vArray._typeUnion._unionTypes._byref;
                return;
            }

            switch (vt)
            {
            case VarEnum.VT_I1:
                *(sbyte *)this._typeUnion._unionTypes._byref = (sbyte)value;
                break;

            case VarEnum.VT_UI1:
                *(byte *)this._typeUnion._unionTypes._byref = (byte)value;
                break;

            case VarEnum.VT_I2:
                *(short *)this._typeUnion._unionTypes._byref = (short)value;
                break;

            case VarEnum.VT_UI2:
                *(ushort *)this._typeUnion._unionTypes._byref = (ushort)value;
                break;

            case VarEnum.VT_BOOL:
                // VARIANT_TRUE  = -1
                // VARIANT_FALSE = 0
                *(short *)this._typeUnion._unionTypes._byref = (bool)value ? (short)-1 : (short)0;
                break;

            case VarEnum.VT_I4:
            case VarEnum.VT_INT:
                *(int *)this._typeUnion._unionTypes._byref = (int)value;
                break;

            case VarEnum.VT_UI4:
            case VarEnum.VT_UINT:
                *(uint *)this._typeUnion._unionTypes._byref = (uint)value;
                break;

            case VarEnum.VT_ERROR:
                *(int *)this._typeUnion._unionTypes._byref = ((ErrorWrapper)value).ErrorCode;
                break;

            case VarEnum.VT_I8:
                *(long *)this._typeUnion._unionTypes._byref = (long)value;
                break;

            case VarEnum.VT_UI8:
                *(ulong *)this._typeUnion._unionTypes._byref = (ulong)value;
                break;

            case VarEnum.VT_R4:
                *(float *)this._typeUnion._unionTypes._byref = (float)value;
                break;

            case VarEnum.VT_R8:
                *(double *)this._typeUnion._unionTypes._byref = (double)value;
                break;

            case VarEnum.VT_DATE:
                *(double *)this._typeUnion._unionTypes._byref = ((DateTime)value).ToOADate();
                break;

            case VarEnum.VT_UNKNOWN:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.GetIUnknownForObject(value);
                break;

            case VarEnum.VT_DISPATCH:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.GetIDispatchForObject(value);
                break;

            case VarEnum.VT_BSTR:
                *(IntPtr *)this._typeUnion._unionTypes._byref = Marshal.StringToBSTR((string)value);
                break;

            case VarEnum.VT_CY:
                *(long *)this._typeUnion._unionTypes._byref = decimal.ToOACurrency((decimal)value);
                break;

            case VarEnum.VT_DECIMAL:
                *(decimal *)this._typeUnion._unionTypes._byref = (decimal)value;
                break;

            case VarEnum.VT_VARIANT:
                Marshal.GetNativeVariantForObject(value, this._typeUnion._unionTypes._byref);
                break;

            default:
                throw new ArgumentException();
            }
        }
예제 #3
0
 public DispatchWrapper(object obj)
 {
     Marshal.GetIDispatchForObject(obj);
     wrappedObject = obj;
 }
예제 #4
0
 public static void GetIDispatchForObject()
 {
     Assert.Throws <PlatformNotSupportedException>(() => Marshal.GetIDispatchForObject(null));
 }
예제 #5
0
        public void CopyFromIndirect(object value)
        {
            VarEnum enum2 = this.VariantType & ~VarEnum.VT_BYREF;

            if (value == null)
            {
                switch (enum2)
                {
                case VarEnum.VT_DISPATCH:
                case VarEnum.VT_UNKNOWN:
                case VarEnum.VT_BSTR:
                    this._typeUnion._unionTypes._byref[0] = IntPtr.Zero;
                    break;
                }
            }
            else
            {
                switch (enum2)
                {
                case VarEnum.VT_I2:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((short)value);
                    return;

                case VarEnum.VT_I4:
                case VarEnum.VT_INT:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((int)value);
                    return;

                case VarEnum.VT_R4:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((float)value);
                    return;

                case VarEnum.VT_R8:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((double)value);
                    return;

                case VarEnum.VT_CY:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)decimal.ToOACurrency((decimal)value);
                    return;

                case VarEnum.VT_DATE:
                {
                    DateTime time = (DateTime)value;
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)time.ToOADate();
                    return;
                }

                case VarEnum.VT_BSTR:
                    this._typeUnion._unionTypes._byref[0] = Marshal.StringToBSTR((string)value);
                    return;

                case VarEnum.VT_DISPATCH:
                    this._typeUnion._unionTypes._byref[0] = Marshal.GetIDispatchForObject(value);
                    return;

                case VarEnum.VT_ERROR:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((ErrorWrapper)value).ErrorCode;
                    return;

                case VarEnum.VT_BOOL:
                    this._typeUnion._unionTypes._byref[0] = ((bool)value) ? ((IntPtr)(-1)) : IntPtr.Zero;
                    return;

                case VarEnum.VT_UNKNOWN:
                    this._typeUnion._unionTypes._byref[0] = Marshal.GetIUnknownForObject(value);
                    return;

                case VarEnum.VT_DECIMAL:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((decimal)value);
                    return;

                case VarEnum.VT_I1:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((sbyte)value);
                    return;

                case VarEnum.VT_UI1:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((byte)value);
                    return;

                case VarEnum.VT_UI2:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((ushort)value);
                    return;

                case VarEnum.VT_UI4:
                case VarEnum.VT_UINT:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((uint)value);
                    return;

                case VarEnum.VT_I8:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((long)value);
                    return;

                case VarEnum.VT_UI8:
                    this._typeUnion._unionTypes._byref[0] = (IntPtr)((ulong)value);
                    return;
                }
                throw new ArgumentException("invalid argument type");
            }
        }