Example #1
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.GetComInterfaceForObject <object, IDispatch>(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();
            }
        }