Exemplo n.º 1
0
        /// <summary>
        /// Gets the managed type that an object needs to be coverted to in order for it to be able
        /// to be represented as a Variant.
        /// 
        /// In general, there is a many-to-many mapping between Type and VarEnum. However, this method
        /// returns a simple mapping that is needed for the current implementation. The reason for the 
        /// many-to-many relation is:
        /// 1. Int32 maps to VT_I4 as well as VT_ERROR, and Decimal maps to VT_DECIMAL and VT_CY. However,
        ///    this changes if you throw the wrapper types into the mix.
        /// 2. There is no Type to represent COM types. __ComObject is a private type, and Object is too
        ///    general.
        /// </summary>
        internal static Type GetManagedMarshalType(VarEnum varEnum) {
            Debug.Assert((varEnum & VarEnum.VT_BYREF) == 0);

            if (varEnum == VarEnum.VT_CY) {
                return typeof(CurrencyWrapper);
            }

            if (IsPrimitiveType(varEnum)) {
                return _ComToManagedPrimitiveTypes[varEnum];
            }

            switch (varEnum) {
                case VarEnum.VT_EMPTY:
                case VarEnum.VT_NULL:
                case VarEnum.VT_UNKNOWN:
                case VarEnum.VT_DISPATCH:
                case VarEnum.VT_VARIANT:
                    return typeof(Object);

                case VarEnum.VT_ERROR:
                    return typeof(ErrorWrapper);

                default:
                    throw new InvalidOperationException(string.Format("Unexpected VarEnum {0}.", varEnum));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the managed type that an object needs to be coverted to in order for it to be able
        /// to be represented as a Variant.
        /// 
        /// In general, there is a many-to-many mapping between Type and VarEnum. However, this method
        /// returns a simple mapping that is needed for the current implementation. The reason for the 
        /// many-to-many relation is:
        /// 1. Int32 maps to VT_I4 as well as VT_ERROR, and Decimal maps to VT_DECIMAL and VT_CY. However,
        ///    this changes if you throw the wrapper types into the mix.
        /// 2. There is no Type to represent COM types. __ComObject is a private type, and Object is too
        ///    general.
        /// </summary>
        internal static Type GetManagedMarshalType(VarEnum varEnum) {
            Debug.Assert((varEnum & VarEnum.VT_BYREF) == 0);

            if (varEnum == VarEnum.VT_CY) {
                return typeof(CurrencyWrapper);
            }

            if (Variant.IsPrimitiveType(varEnum)) {
                return _ComToManagedPrimitiveTypes[varEnum];
            }

            switch (varEnum) {
                case VarEnum.VT_EMPTY:
                case VarEnum.VT_NULL:
                case VarEnum.VT_UNKNOWN:
                case VarEnum.VT_DISPATCH:
                case VarEnum.VT_VARIANT:
                    return typeof(Object);

                case VarEnum.VT_ERROR:
                    return typeof(ErrorWrapper);

                default:
                    throw Error.UnexpectedVarEnum(varEnum);
            }
        }
Exemplo n.º 3
0
        public void StaticFromObjectTest(object inputObject, VarEnum expectedVarType)
        {
            using (PropVariant pv = PropVariant.FromObject(inputObject))
            {

                Assert.Equal(pv.Value, inputObject);
                Assert.Equal(pv.VarType, expectedVarType);
                Assert.False(pv.IsNullOrEmpty);
            }
        }
Exemplo n.º 4
0
 internal static void GetMarshalAs(ConstArray nativeType, out UnmanagedType unmanagedType, out VarEnum safeArraySubType, out string safeArrayUserDefinedSubType, out UnmanagedType arraySubType, out int sizeParamIndex, out int sizeConst, out string marshalType, out string marshalCookie, out int iidParamIndex)
 {
     int num;
     int num2;
     int num3;
     _GetMarshalAs(nativeType.Signature, nativeType.Length, out num, out num2, out safeArrayUserDefinedSubType, out num3, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie, out iidParamIndex);
     unmanagedType = (UnmanagedType) num;
     safeArraySubType = (VarEnum) num2;
     arraySubType = (UnmanagedType) num3;
 }
Exemplo n.º 5
0
        internal static Type GetTypeForVarEnum(VarEnum vt)
        {
            Type type;

            switch (vt)
            {
                // VarEnums which can be used in VARIANTs, but which cannot occur in a TYPEDESC
                case VarEnum.VT_EMPTY:
                case VarEnum.VT_NULL:
                case VarEnum.VT_RECORD:
                    type = typeof(void);
                    break;

                // VarEnums which are not used in VARIANTs, but which can occur in a TYPEDESC
                case VarEnum.VT_VOID:
                    type = typeof(void);
                    break;

                case VarEnum.VT_HRESULT:
                    type = typeof(int);
                    break;

                case ((VarEnum)37): // VT_INT_PTR:
                    type = typeof(IntPtr);
                    break;

                case ((VarEnum)38): // VT_UINT_PTR:
                    type = typeof(UIntPtr);
                    break;

                case VarEnum.VT_SAFEARRAY:
                case VarEnum.VT_CARRAY:
                    type = typeof(Array);
                    break;

                case VarEnum.VT_LPSTR:
                case VarEnum.VT_LPWSTR:
                    type = typeof(string);
                    break;

                case VarEnum.VT_PTR:
                case VarEnum.VT_USERDEFINED:
                    type = typeof(object);
                    break;

                // For VarEnums that can be used in VARIANTs and well as TYPEDESCs, just use VarEnumSelector
                default:
                    type = VarEnumSelector.GetManagedMarshalType(vt);
                    break;
            }

            return type;
        }
Exemplo n.º 6
0
        public void Clear()
        {
            VariantType = VarEnum.VT_EMPTY;

            unsafe
            {
                fixed (Variant* p = &this)
                {
                    NativeMethods.VariantClear((IntPtr)p);
                }
            }
        }
 public int[] QueryAvailableProperties(string itemId, out string[] descriptions, out VarEnum[] dataTypes)
 {
     int dwCount = 0;
     int[] pdwPropertyIDs = {};
     int[] ppvtDataTypes = {};
     string[] _descriptions = {};
     DoComCall(ComObject, "IOpcItemProperties::QueryAvailableProperties",() =>
             ComObject.QueryAvailableProperties(itemId, out dwCount, out pdwPropertyIDs, out _descriptions,
                 out ppvtDataTypes), itemId);
     dataTypes = new VarEnum[ppvtDataTypes.Length];
     for (int i = 0; i < dwCount; i++)
     {
         dataTypes[i] = (VarEnum) ppvtDataTypes[i];
     }
     descriptions = _descriptions;
     return pdwPropertyIDs;
 }
        public string[] BrowseOpcItemIds(OpcDaBrowseType browseFilterType, string filterCriteria = "",
            VarEnum dataTypeFilter = VarEnum.VT_EMPTY,
            OpcDaAccessRights accessRightsFilter = OpcDaAccessRights.Ignore)
        {
            string szFilterCriteria = filterCriteria ?? "";

            return DoComCall(ComObject, "IOPCBrowseServerAddressSpace::BrowseOpcItemIDs", () =>
            {
                IEnumString enumString;
                ComObject.BrowseOPCItemIDs((OPCBROWSETYPE) browseFilterType, szFilterCriteria,
                    (short) dataTypeFilter,
                    (OPCACCESSRIGHTS) accessRightsFilter, out enumString);
                return DoComCall(enumString, "IEnumString::EnumareateAllAndRelease",
                    () => enumString.EnumareateAllAndRelease(OpcConfiguration.BatchSize).ToArray(),
                    OpcConfiguration.BatchSize);
            }, browseFilterType, szFilterCriteria, dataTypeFilter,
                accessRightsFilter);
        }
        /// <summary>
        /// Retrieves enumerator of current branch.
        /// </summary>
        /// <param name="type">Namespace browse mode. See <see cref="BrowseType"/></param>
        /// <param name="filterCriteria">Mask of item/folder name.</param>
        /// <param name="dataTypeFilter">Filter by type.</param>
        /// <param name="accessRightsFilter">Filter by access rights.</param>
        /// <returns>Enumerator of current namespace level.</returns>
		public IEnumerable<string> GetItemIds(BrowseType type, string filterCriteria, VarEnum dataTypeFilter, int accessRightsFilter)
		{
            IEnumString enumerator;
			browseServerSpace.BrowseOPCItemIDs(
				type, 
				filterCriteria,
                (short)dataTypeFilter, 
				accessRightsFilter, 
				out enumerator);								

			try
			{
				enumerator.Reset();
			    var itemBlock = new Queue<string>(blockSize);
				while(true)
				{
				    if (itemBlock.Count == 0)
				    {
                        var itemIds = new string[blockSize];
                        uint fetched;
                        var res = enumerator.Next((uint)blockSize, itemIds, out fetched);
                        if (res > 1)
                            Marshal.ThrowExceptionForHR(res);
                        for(var i = 0; i < fetched; i++)
                            itemBlock.Enqueue(itemIds[i]);
                    }

                    if (itemBlock.Count == 0)
						break;

                    yield return itemBlock.Dequeue();					
				}            
			}
			finally
			{
				if(enumerator != null)
					Marshal.ReleaseComObject(enumerator);
			}
		}
 private object ToType(VarEnum type, string value) {
    switch ( type ) {
    case VarEnum.VT_BOOL:
       return XmlConvert.ToBoolean(value);
    case VarEnum.VT_DATE:
       return XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified);
    case VarEnum.VT_DECIMAL:
       return XmlConvert.ToDecimal(value);
    case VarEnum.VT_EMPTY:
       return null;
    case VarEnum.VT_I1:
       return XmlConvert.ToSByte(value);
    case VarEnum.VT_I2:
       return XmlConvert.ToInt16(value);
    case VarEnum.VT_INT:
    case VarEnum.VT_I4:
       return XmlConvert.ToInt32(value);
    case VarEnum.VT_I8:
       return XmlConvert.ToInt64(value);
    case VarEnum.VT_R4:
       return XmlConvert.ToSingle(value);
    case VarEnum.VT_R8:
       return XmlConvert.ToDouble(value);
    case VarEnum.VT_UI1:
       return XmlConvert.ToByte(value);
    case VarEnum.VT_UI2:
       return XmlConvert.ToUInt16(value);
    case VarEnum.VT_UINT:
    case VarEnum.VT_UI4:
       return XmlConvert.ToUInt32(value);
    case VarEnum.VT_UI8:
       return XmlConvert.ToUInt64(value);
    default:
       return value;
    }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a family of COM types such that within each family, there is a completely non-lossy
        /// conversion from a type to an earlier type in the family.
        /// </summary>
        private static IList<IList<VarEnum>> CreateComPrimitiveTypeFamilies() {
            VarEnum[][] typeFamilies = new VarEnum[][] {
                new VarEnum[] { VarEnum.VT_I8, VarEnum.VT_I4, VarEnum.VT_I2, VarEnum.VT_I1 },
                new VarEnum[] { VarEnum.VT_UI8, VarEnum.VT_UI4, VarEnum.VT_UI2, VarEnum.VT_UI1 },
                new VarEnum[] { VarEnum.VT_INT },
                new VarEnum[] { VarEnum.VT_UINT },
                new VarEnum[] { VarEnum.VT_BOOL },
                new VarEnum[] { VarEnum.VT_DATE },
                new VarEnum[] { VarEnum.VT_R8, VarEnum.VT_R4 },
                new VarEnum[] { VarEnum.VT_DECIMAL },
                new VarEnum[] { VarEnum.VT_BSTR },

                // wrappers
                new VarEnum[] { VarEnum.VT_CY },
                new VarEnum[] { VarEnum.VT_ERROR },
            };

            return typeFamilies;
        }
Exemplo n.º 12
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;
            }

            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");
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Decodes a byte array (stored in the memory stream) into an object
        /// If the GUID is one of the internal versions, then the type is assumed
        /// to be byte array.
        /// If however, the guid is of unknown origin or not v1 internal, then the type
        ///     information is assumed to be stored in the first 2 bytes of the stream.
        /// </summary>
        /// <param name="guid">Guid of property - to detect origin</param>
        /// <param name="memStream">Buffer of data</param>
        /// <param name="type">the type info stored in the stream</param>
        /// <returns>object stored in data buffer</returns>
        /// <remarks>The buffer stream passed in to the method will be closed after reading</remarks>
        internal static object DecodeAttribute(Guid guid, Stream memStream, out VarEnum type)
        {
            // First determine the object type
            using (BinaryReader br = new BinaryReader(memStream))
            {
                //
                // if usesEmbeddedTypeInfo is true, we do not 
                // read the variant type from the ISF stream.  Instead, 
                // we assume it to be a byte[]
                //
                bool usesEmbeddedTypeInfo = UsesEmbeddedTypeInformation(guid);

                // if the Id has embedded type information then retrieve it from the stream
                if (usesEmbeddedTypeInfo)
                {
                    // We must read the data type from the stream
                    type = (VarEnum)br.ReadUInt16();
                }
                else
                {
                    // The data is stored as byte array
                    type = (VarEnum.VT_ARRAY | VarEnum.VT_UI1);
                }
                switch (type)
                {
                    case (VarEnum.VT_ARRAY | VarEnum.VT_I1):
                        return br.ReadChars((int)(memStream.Length - 2));
                    case (VarEnum.VT_ARRAY | VarEnum.VT_UI1):
                        {
                            //
                            // note: for (VarEnum.VT_ARRAY | VarEnum.VT_UI1),
                            // we might be reading data that didn't have the 
                            // type embedded in the ISF stream, in which case
                            // we must not assume we've already read two bytes
                            //
                            int previouslyReadBytes = 2;
                            if (!usesEmbeddedTypeInfo)
                            {
                                previouslyReadBytes = 0;
                            }
                            return br.ReadBytes((int)(memStream.Length - previouslyReadBytes));
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_I2):
                        {
                            int count = (int)(memStream.Length - 2) / 2;    // 2 is the size of one element
                            short[] val = new short[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadInt16();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_UI2):
                        {
                            int count = (int)(memStream.Length - 2) / 2;    // 2 is the size of one element
                            ushort[] val = new ushort[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadUInt16();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_I4):
                        {
                            int count = (int)(memStream.Length - 2) / 4;    // 2 is the size of one element
                            int[] val = new int[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadInt32();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_UI4):
                        {
                            int count = (int)(memStream.Length - 2) / 4;    // size of one element
                            uint[] val = new uint[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadUInt32();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_I8):
                        {
                            int count = (int)(memStream.Length - 2) / Native.BitsPerByte;    // size of one element
                            long[] val = new long[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadInt64();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_UI8):
                        {
                            int count = (int)(memStream.Length - 2) / Native.BitsPerByte;    // size of one element
                            ulong[] val = new ulong[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadUInt64();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_R4):
                        {
                            int count = (int)(memStream.Length - 2) / 4;    // size of one element
                            float[] val = new float[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadSingle();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_R8):
                        {
                            int count = (int)(memStream.Length - 2) / Native.BitsPerByte;    // size of one element
                            double[] val = new double[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadDouble();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_DATE):
                        {
                            int count = (int)(memStream.Length - 2) / Native.BitsPerByte;    // size of one element
                            DateTime[] val = new DateTime[count];
                            for (int i = 0; i < count; i++)
                            {
                                val[i] = DateTime.FromOADate(br.ReadDouble());
                            }
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_BOOL):
                        {
                            int count = (int)(memStream.Length - 2);    // size of one element
                            bool[] val = new bool[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadBoolean();
                            return val;
                        }
                    case (VarEnum.VT_ARRAY | VarEnum.VT_DECIMAL):
                        {
                            int count = (int)((memStream.Length - 2) / Native.SizeOfDecimal);    // size of one element
                            decimal[] val = new decimal[count];
                            for (int i = 0; i < count; i++)
                                val[i] = br.ReadDecimal();
                            return val;
                        }
                    case (VarEnum.VT_I1):
                        return br.ReadChar();
                    case (VarEnum.VT_UI1):
                        return br.ReadByte();
                    case (VarEnum.VT_I2):
                        return br.ReadInt16();
                    case (VarEnum.VT_UI2):
                        return br.ReadUInt16();
                    case (VarEnum.VT_I4):
                        return br.ReadInt32();
                    case (VarEnum.VT_UI4):
                        return br.ReadUInt32();
                    case (VarEnum.VT_I8):
                        return br.ReadInt64();
                    case (VarEnum.VT_UI8):
                        return br.ReadUInt64();
                    case (VarEnum.VT_R4):
                        return br.ReadSingle();
                    case (VarEnum.VT_R8):
                        return br.ReadDouble();
                    case (VarEnum.VT_DATE):
                        return DateTime.FromOADate(br.ReadDouble());
                    case (VarEnum.VT_BOOL):
                        return br.ReadBoolean();
                    case (VarEnum.VT_DECIMAL):
                        return br.ReadDecimal();
                    case (VarEnum.VT_BSTR):
                        {
                            byte[] bytestring = br.ReadBytes((int)memStream.Length);
                            return System.Text.Encoding.Unicode.GetString(bytestring);
                        }
                    default:
                        {
                            throw new InvalidOperationException(SR.Get(SRID.InvalidEpInIsf));
                        }
                }
            }
        }
Exemplo n.º 14
0
 public static extern IntPtr SafeArrayCreate(VarEnum vt, uint cDims, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] SAFEARRAYBOUND[] rgsabound);
        /// <summary>
        /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa372045(v=vs.85).aspx
        /// </summary>
        public void SetSummaryInformation(MsiMethods.PropertyId propertyId, VarEnum datatype, object value)
        {
            var summaryHandle = IntPtr.Zero;
            if (MsiMethods.MsiGetSummaryInformation(dbhandle, null, 7, ref summaryHandle) != (int)MsiMethods.ErrorCodes.Success)
            {
                throw new InstallerVerificationLibraryException("Could not open summary handler to");
            }

            switch (datatype)
            {
                case VarEnum.VT_FILETIME:
                    break;
                case VarEnum.VT_I2:
                case VarEnum.VT_I4:
                    if (MsiMethods.MsiSummaryInfoSetProperty(summaryHandle, (int)propertyId, (int)datatype, Convert.ToInt32(value), IntPtr.Zero, null) != (int)MsiMethods.ErrorCodes.Success)
                    {
                        throw new InstallerVerificationLibraryException("Could not set summary property" + propertyId);
                    }
                    break;
                case VarEnum.VT_LPSTR:
                    if (MsiMethods.MsiSummaryInfoSetProperty(summaryHandle, (int)propertyId, (int)datatype, 0, IntPtr.Zero, Convert.ToString(value)) != (int)MsiMethods.ErrorCodes.Success)
                    {
                        throw new InstallerVerificationLibraryException("Could not set summary property" + propertyId);
                    }
                    break;
            }

            if (MsiMethods.MsiSummaryInfoPersist(summaryHandle) != (int)MsiMethods.ErrorCodes.Success)
            {
                throw new InstallerVerificationLibraryException("Could not persist summary property");
            }

            CloseHandle(summaryHandle);
        }
Exemplo n.º 16
0
 internal void InitVector(Array array, Type type, VarEnum varEnum)
 {
     this.Init(array, type, varEnum | VarEnum.VT_VECTOR);
 }
Exemplo n.º 17
0
 internal void InitVector(Array array, Type type, VarEnum varEnum)
 {
     Init(array,  type, varEnum | VarEnum.VT_VECTOR);
 }
Exemplo n.º 18
0
        public static PropertyKey GlobalTextColor       = CreateRibbonPropertyKey(2002, VarEnum.VT_UI4); // UI_HSBCOLOR

        public static PropertyKey CreateRibbonPropertyKey(Int32 index, VarEnum id)
        {
            return(new PropertyKey(
                       new Guid(index, 0x7363, 0x696e, new byte[] { 0x84, 0x41, 0x79, 0x8a, 0xcf, 0x5a, 0xeb, 0xb7 }),
                       (uint)id));
        }
Exemplo n.º 19
0
        internal static System.Reflection.MethodInfo GetByrefSetter(VarEnum varType)
        {
            switch (varType)
            {
                #region Generated Outer Variant byref setter

            // *** BEGIN GENERATED CODE ***
            // generated by function: gen_byref_setters from: generate_comdispatch.py

            case VarEnum.VT_I1: return(typeof(Variant).GetMethod("SetAsByrefI1"));

            case VarEnum.VT_I2: return(typeof(Variant).GetMethod("SetAsByrefI2"));

            case VarEnum.VT_I4: return(typeof(Variant).GetMethod("SetAsByrefI4"));

            case VarEnum.VT_I8: return(typeof(Variant).GetMethod("SetAsByrefI8"));

            case VarEnum.VT_UI1: return(typeof(Variant).GetMethod("SetAsByrefUi1"));

            case VarEnum.VT_UI2: return(typeof(Variant).GetMethod("SetAsByrefUi2"));

            case VarEnum.VT_UI4: return(typeof(Variant).GetMethod("SetAsByrefUi4"));

            case VarEnum.VT_UI8: return(typeof(Variant).GetMethod("SetAsByrefUi8"));

            case VarEnum.VT_INT: return(typeof(Variant).GetMethod("SetAsByrefInt"));

            case VarEnum.VT_UINT: return(typeof(Variant).GetMethod("SetAsByrefUint"));

            case VarEnum.VT_BOOL: return(typeof(Variant).GetMethod("SetAsByrefBool"));

            case VarEnum.VT_ERROR: return(typeof(Variant).GetMethod("SetAsByrefError"));

            case VarEnum.VT_R4: return(typeof(Variant).GetMethod("SetAsByrefR4"));

            case VarEnum.VT_R8: return(typeof(Variant).GetMethod("SetAsByrefR8"));

            case VarEnum.VT_DECIMAL: return(typeof(Variant).GetMethod("SetAsByrefDecimal"));

            case VarEnum.VT_CY: return(typeof(Variant).GetMethod("SetAsByrefCy"));

            case VarEnum.VT_DATE: return(typeof(Variant).GetMethod("SetAsByrefDate"));

            case VarEnum.VT_BSTR: return(typeof(Variant).GetMethod("SetAsByrefBstr"));

            case VarEnum.VT_UNKNOWN: return(typeof(Variant).GetMethod("SetAsByrefUnknown"));

            case VarEnum.VT_DISPATCH: return(typeof(Variant).GetMethod("SetAsByrefDispatch"));

                // *** END GENERATED CODE ***

                #endregion

            case VarEnum.VT_VARIANT:
                return(typeof(Variant).GetMethod("SetAsByrefVariant"));

            case VarEnum.VT_RECORD:
            case VarEnum.VT_ARRAY:
                return(typeof(Variant).GetMethod("SetAsByrefVariantIndirect"));

            default:
                throw Error.VariantGetAccessorNYI(varType);
            }
        }
Exemplo n.º 20
0
 internal VariantBuilder(VarEnum targetComType, ArgBuilder builder)
 {
     _targetComType = targetComType;
     _argBuilder    = builder;
 }
Exemplo n.º 21
0
 private void GetNativeVariantForObject_RoundtrippingPrimitives_Success(object primitive, VarEnum expectedVarType, IntPtr expectedValue)
 {
     GetNativeVariantForObject_ValidObject_Success(primitive, expectedVarType, expectedValue, primitive);
 }
Exemplo n.º 22
0
        public Variant(object?obj)
        {
            _data = 0;

            VarEnum vt = VarEnum.VT_EMPTY;

            if (obj is DateTime)
            {
                _objref = null;
                _flags  = CV_DATETIME;
                _data   = ((DateTime)obj).Ticks;
                return;
            }

            if (obj is string)
            {
                _flags  = CV_STRING;
                _objref = obj;
                return;
            }

            if (obj == null)
            {
                this = Empty;
                return;
            }
            if (obj == System.DBNull.Value)
            {
                this = DBNull;
                return;
            }
            if (obj == Type.Missing)
            {
                this = Missing;
                return;
            }

            if (obj is Array)
            {
                _flags  = CV_OBJECT | ArrayBitMask;
                _objref = obj;
                return;
            }

            // Compiler appeasement
            _flags  = CV_EMPTY;
            _objref = null;

            // Check to see if the object passed in is a wrapper object.
            if (obj is UnknownWrapper)
            {
                vt  = VarEnum.VT_UNKNOWN;
                obj = ((UnknownWrapper)obj).WrappedObject;
            }
            else if (obj is DispatchWrapper)
            {
                vt = VarEnum.VT_DISPATCH;
                Debug.Assert(OperatingSystem.IsWindows());
                obj = ((DispatchWrapper)obj).WrappedObject;
            }
            else if (obj is ErrorWrapper)
            {
                vt  = VarEnum.VT_ERROR;
                obj = (object)(((ErrorWrapper)obj).ErrorCode);
                Debug.Assert(obj != null, "obj != null");
            }
            else if (obj is CurrencyWrapper)
            {
                vt  = VarEnum.VT_CY;
                obj = (object)(((CurrencyWrapper)obj).WrappedObject);
                Debug.Assert(obj != null, "obj != null");
            }
            else if (obj is BStrWrapper)
            {
                vt  = VarEnum.VT_BSTR;
                obj = (object?)(((BStrWrapper)obj).WrappedObject);
            }

            if (obj != null)
            {
                SetFieldsObject(obj);
            }

            // If the object passed in is one of the wrappers then set the VARIANT type.
            if (vt != VarEnum.VT_EMPTY)
            {
                _flags |= ((int)vt << VTBitShift);
            }
        }
Exemplo n.º 23
0
        private static bool TryGetPrimitiveComType(Type argumentType, out VarEnum primitiveVarEnum)
        {
            switch (Type.GetTypeCode(argumentType))
            {
            case TypeCode.Boolean:
                primitiveVarEnum = VarEnum.VT_BOOL;
                return(true);

            case TypeCode.Char:
                primitiveVarEnum = VarEnum.VT_UI2;
                return(true);

            case TypeCode.SByte:
                primitiveVarEnum = VarEnum.VT_I1;
                return(true);

            case TypeCode.Byte:
                primitiveVarEnum = VarEnum.VT_UI1;
                return(true);

            case TypeCode.Int16:
                primitiveVarEnum = VarEnum.VT_I2;
                return(true);

            case TypeCode.UInt16:
                primitiveVarEnum = VarEnum.VT_UI2;
                return(true);

            case TypeCode.Int32:
                primitiveVarEnum = VarEnum.VT_I4;
                return(true);

            case TypeCode.UInt32:
                primitiveVarEnum = VarEnum.VT_UI4;
                return(true);

            case TypeCode.Int64:
                primitiveVarEnum = VarEnum.VT_I8;
                return(true);

            case TypeCode.UInt64:
                primitiveVarEnum = VarEnum.VT_UI8;
                return(true);

            case TypeCode.Single:
                primitiveVarEnum = VarEnum.VT_R4;
                return(true);

            case TypeCode.Double:
                primitiveVarEnum = VarEnum.VT_R8;
                return(true);

            case TypeCode.Decimal:
                primitiveVarEnum = VarEnum.VT_DECIMAL;
                return(true);

            case TypeCode.DateTime:
                primitiveVarEnum = VarEnum.VT_DATE;
                return(true);

            case TypeCode.String:
                primitiveVarEnum = VarEnum.VT_BSTR;
                return(true);
            }

            if (argumentType == typeof(CurrencyWrapper))
            {
                primitiveVarEnum = VarEnum.VT_CY;
                return(true);
            }

            if (argumentType == typeof(ErrorWrapper))
            {
                primitiveVarEnum = VarEnum.VT_ERROR;
                return(true);
            }

            if (argumentType == typeof(IntPtr))
            {
                primitiveVarEnum = VarEnum.VT_INT;
                return(true);
            }

            if (argumentType == typeof(UIntPtr))
            {
                primitiveVarEnum = VarEnum.VT_UINT;
                return(true);
            }

            primitiveVarEnum = VarEnum.VT_VOID; // error
            return(false);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Determine .net type for the given type descriptor.
 /// </summary>
 /// <param name="typedesc">COM type descriptor to convert.</param>
 /// <returns>Type represented by the typedesc.</returns>
 internal static Type GetTypeFromTypeDesc(COM.TYPEDESC typedesc)
 {
     VarEnum vt = (VarEnum)typedesc.vt;
     return VarEnumSelector.GetTypeForVarEnum(vt);
 }
Exemplo n.º 25
0
 internal static Type GetTypeForVarEnum(VarEnum vt)
 {
     throw new PlatformNotSupportedException();
 }
Exemplo n.º 26
0
        internal static System.Reflection.PropertyInfo GetAccessor(VarEnum varType)
        {
            switch (varType)
            {
                #region Generated Outer Variant accessors PropertyInfos

            // *** BEGIN GENERATED CODE ***
            // generated by function: gen_accessor_propertyinfo from: generate_comdispatch.py

            case VarEnum.VT_I1: return(typeof(Variant).GetProperty("AsI1"));

            case VarEnum.VT_I2: return(typeof(Variant).GetProperty("AsI2"));

            case VarEnum.VT_I4: return(typeof(Variant).GetProperty("AsI4"));

            case VarEnum.VT_I8: return(typeof(Variant).GetProperty("AsI8"));

            case VarEnum.VT_UI1: return(typeof(Variant).GetProperty("AsUi1"));

            case VarEnum.VT_UI2: return(typeof(Variant).GetProperty("AsUi2"));

            case VarEnum.VT_UI4: return(typeof(Variant).GetProperty("AsUi4"));

            case VarEnum.VT_UI8: return(typeof(Variant).GetProperty("AsUi8"));

            case VarEnum.VT_INT: return(typeof(Variant).GetProperty("AsInt"));

            case VarEnum.VT_UINT: return(typeof(Variant).GetProperty("AsUint"));

            case VarEnum.VT_BOOL: return(typeof(Variant).GetProperty("AsBool"));

            case VarEnum.VT_ERROR: return(typeof(Variant).GetProperty("AsError"));

            case VarEnum.VT_R4: return(typeof(Variant).GetProperty("AsR4"));

            case VarEnum.VT_R8: return(typeof(Variant).GetProperty("AsR8"));

            case VarEnum.VT_DECIMAL: return(typeof(Variant).GetProperty("AsDecimal"));

            case VarEnum.VT_CY: return(typeof(Variant).GetProperty("AsCy"));

            case VarEnum.VT_DATE: return(typeof(Variant).GetProperty("AsDate"));

            case VarEnum.VT_BSTR: return(typeof(Variant).GetProperty("AsBstr"));

            case VarEnum.VT_UNKNOWN: return(typeof(Variant).GetProperty("AsUnknown"));

            case VarEnum.VT_DISPATCH: return(typeof(Variant).GetProperty("AsDispatch"));

                // *** END GENERATED CODE ***

                #endregion

            case VarEnum.VT_VARIANT:
            case VarEnum.VT_RECORD:
            case VarEnum.VT_ARRAY:
                return(typeof(Variant).GetProperty("AsVariant"));

            default:
                throw Error.VariantGetAccessorNYI(varType);
            }
        }
Exemplo n.º 27
0
        internal unsafe object ToObject(object syncObject)
        {
            VarEnum varType = (VarEnum)this.varType;

            if ((varType & VarEnum.VT_VECTOR) == VarEnum.VT_EMPTY)
            {
                switch (varType)
                {
                case VarEnum.VT_FILETIME:
                    return(this.filetime);

                case VarEnum.VT_BLOB:

                    //byte[] destination = new byte[this.ca.cElems];
                    //Marshal.Copy(this.ca.pElems, destination, 0, (int)this.ca.cElems);
                    //return new BitmapMetadataBlob(destination);
                    return(null);

                case VarEnum.VT_CLSID:
                {
                    byte[] buffer2 = new byte[0x10];
                    Marshal.Copy(this.pclsidVal, buffer2, 0, 0x10);
                    return(new Guid(buffer2));
                }

                case VarEnum.VT_EMPTY:
                    return(null);

                case VarEnum.VT_NULL:
                case VarEnum.VT_CY:
                case VarEnum.VT_DATE:
                case VarEnum.VT_BSTR:
                case VarEnum.VT_DISPATCH:
                case VarEnum.VT_ERROR:
                case VarEnum.VT_VARIANT:
                case VarEnum.VT_DECIMAL:
                case (VarEnum.VT_DECIMAL | VarEnum.VT_NULL):
                    goto Label_06B3;

                case VarEnum.VT_I2:
                    return(this.iVal);

                case VarEnum.VT_I4:
                    return(this.intVal);

                case VarEnum.VT_R4:
                    return(this.fltVal);

                case VarEnum.VT_R8:
                    return(this.dblVal);

                case VarEnum.VT_BOOL:
                    return(this.boolVal != 0);

                case VarEnum.VT_UNKNOWN:
                    //{
                    //    IntPtr zero = IntPtr.Zero;
                    //    Guid guid2 = MILGuidData.IID_IWICMetadataQueryWriter;
                    //    Guid guid = MILGuidData.IID_IWICMetadataQueryReader;
                    //    try
                    //    {
                    //        if (UnsafeNativeMethods.MILUnknown.QueryInterface(this.punkVal, ref guid2, out zero) == 0)
                    //        {
                    //            SafeMILHandle metadataHandle = new SafeMILHandle(zero, 0L);
                    //            zero = IntPtr.Zero;
                    //            return new BitmapMetadata(metadataHandle, false, false, syncObject);
                    //        }
                    //        int hr = UnsafeNativeMethods.MILUnknown.QueryInterface(this.punkVal, ref guid, out zero);
                    //        if (hr == 0)
                    //        {
                    //            SafeMILHandle handle = new SafeMILHandle(zero, 0L);
                    //            zero = IntPtr.Zero;
                    //            return new BitmapMetadata(handle, true, false, syncObject);
                    //        }
                    //        HRESULT.Check(hr);
                    //    }
                    //    finally
                    //    {
                    //        if (zero != IntPtr.Zero)
                    //        {
                    //            UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref zero);
                    //        }
                    //    }
                    //    goto Label_06B3;
                    //}
                    return(null);

                case VarEnum.VT_I1:
                    return(this.cVal);

                case VarEnum.VT_UI1:
                    return(this.bVal);

                case VarEnum.VT_UI2:
                    return(this.uiVal);

                case VarEnum.VT_UI4:
                    return(this.uintVal);

                case VarEnum.VT_I8:
                    return(this.lVal);

                case VarEnum.VT_UI8:
                    return(this.ulVal);

                case VarEnum.VT_LPSTR:
                    return(Marshal.PtrToStringAnsi(this.pszVal));

                case VarEnum.VT_LPWSTR:
                    return(Marshal.PtrToStringUni(this.pwszVal));
                }
            }
            else
            {
                switch ((varType & ~VarEnum.VT_VECTOR))
                {
                case VarEnum.VT_EMPTY:
                    return(null);

                case VarEnum.VT_NULL:
                case VarEnum.VT_CY:
                case VarEnum.VT_DATE:
                case VarEnum.VT_BSTR:
                case VarEnum.VT_DISPATCH:
                case VarEnum.VT_ERROR:
                case VarEnum.VT_VARIANT:
                case VarEnum.VT_UNKNOWN:
                case VarEnum.VT_DECIMAL:
                case (VarEnum.VT_DECIMAL | VarEnum.VT_NULL):
                    goto Label_06B3;

                case VarEnum.VT_I2:
                {
                    short[] numArray8 = new short[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, numArray8, 0, (int)this.ca.cElems);
                    return(numArray8);
                }

                case VarEnum.VT_I4:
                {
                    int[] numArray6 = new int[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, numArray6, 0, (int)this.ca.cElems);
                    return(numArray6);
                }

                case VarEnum.VT_R4:
                {
                    float[] numArray2 = new float[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, numArray2, 0, (int)this.ca.cElems);
                    return(numArray2);
                }

                case VarEnum.VT_R8:
                {
                    double[] numArray = new double[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, numArray, 0, (int)this.ca.cElems);
                    return(numArray);
                }

                case VarEnum.VT_BOOL:
                {
                    bool[] flagArray = new bool[this.ca.cElems];
                    for (int i = 0; i < this.ca.cElems; i++)
                    {
                        flagArray[i] = Marshal.ReadInt16(this.ca.pElems, i * 2) != 0;
                    }
                    return(flagArray);
                }

                case VarEnum.VT_I1:
                {
                    sbyte[] numArray9 = new sbyte[this.ca.cElems];
                    for (int j = 0; j < this.ca.cElems; j++)
                    {
                        numArray9[j] = (sbyte)Marshal.ReadByte(this.ca.pElems, j);
                    }
                    return(numArray9);
                }

                case VarEnum.VT_UI1:
                {
                    byte[] buffer4 = new byte[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, buffer4, 0, (int)this.ca.cElems);
                    return(buffer4);
                }

                case VarEnum.VT_UI2:
                {
                    ushort[] numArray7 = new ushort[this.ca.cElems];
                    for (int k = 0; k < this.ca.cElems; k++)
                    {
                        numArray7[k] = (ushort)Marshal.ReadInt16(this.ca.pElems, k * 2);
                    }
                    return(numArray7);
                }

                case VarEnum.VT_UI4:
                {
                    uint[] numArray5 = new uint[this.ca.cElems];
                    for (int m = 0; m < this.ca.cElems; m++)
                    {
                        numArray5[m] = (uint)Marshal.ReadInt32(this.ca.pElems, m * 4);
                    }
                    return(numArray5);
                }

                case VarEnum.VT_I8:
                {
                    long[] numArray4 = new long[this.ca.cElems];
                    Marshal.Copy(this.ca.pElems, numArray4, 0, (int)this.ca.cElems);
                    return(numArray4);
                }

                case VarEnum.VT_UI8:
                {
                    ulong[] numArray3 = new ulong[this.ca.cElems];
                    for (int n = 0; n < this.ca.cElems; n++)
                    {
                        numArray3[n] = (ulong)Marshal.ReadInt64(this.ca.pElems, n * 8);
                    }
                    return(numArray3);
                }

                case VarEnum.VT_LPSTR:
                {
                    string[] strArray2 = new string[this.ca.cElems];
                    int      num11     = 0;
                    num11 = sizeof(IntPtr);
                    for (int num2 = 0; num2 < this.ca.cElems; num2++)
                    {
                        IntPtr ptr = Marshal.ReadIntPtr(this.ca.pElems, num2 * num11);
                        strArray2[num2] = Marshal.PtrToStringAnsi(ptr);
                    }
                    return(strArray2);
                }

                case VarEnum.VT_LPWSTR:
                {
                    string[] strArray = new string[this.ca.cElems];
                    int      num10    = 0;
                    num10 = sizeof(IntPtr);
                    for (int num = 0; num < this.ca.cElems; num++)
                    {
                        IntPtr ptr2 = Marshal.ReadIntPtr(this.ca.pElems, num * num10);
                        strArray[num] = Marshal.PtrToStringUni(ptr2);
                    }
                    return(strArray);
                }

                case VarEnum.VT_CLSID:
                {
                    Guid[] guidArray = new Guid[this.ca.cElems];
                    for (int num3 = 0; num3 < this.ca.cElems; num3++)
                    {
                        byte[] buffer3 = new byte[0x10];
                        Marshal.Copy(this.ca.pElems, buffer3, num3 * 0x10, 0x10);
                        guidArray[num3] = new Guid(buffer3);
                    }
                    return(guidArray);
                }
                }
            }
Label_06B3:
            throw new NotSupportedException("Image_PropertyNotSupported");
        }
Exemplo n.º 28
0
        private static bool TryGetPrimitiveComType(Type argumentType, out VarEnum primitiveVarEnum)
        {
            #region Generated Outer Managed To COM Primitive Type Map

            // *** BEGIN GENERATED CODE ***
            // generated by function: gen_ManagedToComPrimitiveTypes from: generate_comdispatch.py

            switch (Type.GetTypeCode(argumentType))
            {
            case TypeCode.Boolean:
                primitiveVarEnum = VarEnum.VT_BOOL;
                return(true);

            case TypeCode.Char:
                primitiveVarEnum = VarEnum.VT_UI2;
                return(true);

            case TypeCode.SByte:
                primitiveVarEnum = VarEnum.VT_I1;
                return(true);

            case TypeCode.Byte:
                primitiveVarEnum = VarEnum.VT_UI1;
                return(true);

            case TypeCode.Int16:
                primitiveVarEnum = VarEnum.VT_I2;
                return(true);

            case TypeCode.UInt16:
                primitiveVarEnum = VarEnum.VT_UI2;
                return(true);

            case TypeCode.Int32:
                primitiveVarEnum = VarEnum.VT_I4;
                return(true);

            case TypeCode.UInt32:
                primitiveVarEnum = VarEnum.VT_UI4;
                return(true);

            case TypeCode.Int64:
                primitiveVarEnum = VarEnum.VT_I8;
                return(true);

            case TypeCode.UInt64:
                primitiveVarEnum = VarEnum.VT_UI8;
                return(true);

            case TypeCode.Single:
                primitiveVarEnum = VarEnum.VT_R4;
                return(true);

            case TypeCode.Double:
                primitiveVarEnum = VarEnum.VT_R8;
                return(true);

            case TypeCode.Decimal:
                primitiveVarEnum = VarEnum.VT_DECIMAL;
                return(true);

            case TypeCode.DateTime:
                primitiveVarEnum = VarEnum.VT_DATE;
                return(true);

            case TypeCode.String:
                primitiveVarEnum = VarEnum.VT_BSTR;
                return(true);
            }

            if (argumentType == typeof(CurrencyWrapper))
            {
                primitiveVarEnum = VarEnum.VT_CY;
                return(true);
            }

            if (argumentType == typeof(ErrorWrapper))
            {
                primitiveVarEnum = VarEnum.VT_ERROR;
                return(true);
            }

            if (argumentType == typeof(IntPtr))
            {
                primitiveVarEnum = VarEnum.VT_PTR;
                return(true);
            }

            if (argumentType == typeof(UIntPtr))
            {
                primitiveVarEnum = VarEnum.VT_PTR;
                return(true);
            }

            // *** END GENERATED CODE ***

            #endregion

            primitiveVarEnum = VarEnum.VT_VOID; // error
            return(false);
        }
Exemplo n.º 29
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void GetMarshalAs(ConstArray nativeType, 
            out UnmanagedType unmanagedType, out VarEnum safeArraySubType, out string safeArrayUserDefinedSubType, 
            out UnmanagedType arraySubType, out int sizeParamIndex, out int sizeConst, out string marshalType, out string marshalCookie,
            out int iidParamIndex)
        {
            int _unmanagedType, _safeArraySubType, _arraySubType;

            _GetMarshalAs(nativeType.Signature, (int)nativeType.Length,
                out _unmanagedType, out _safeArraySubType, out safeArrayUserDefinedSubType, 
                out _arraySubType, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie,
                out iidParamIndex);
            unmanagedType = (UnmanagedType)_unmanagedType;
            safeArraySubType = (VarEnum)_safeArraySubType;
            arraySubType = (UnmanagedType)_arraySubType;
        }
Exemplo n.º 30
0
		/// <summary>
		/// Converts a value to the specified type using COM conversion rules.
		/// </summary>
        public static int ChangeTypeForCOM(object source, VarEnum targetType, out object target)
        {
            return ChangeTypeForCOM(source, GetVarType(source), targetType, out target);
        }
Exemplo n.º 31
0
        internal void Init(Array array, Type type, VarEnum vt)
        {
            varType = (ushort) vt;
            ca.cElems = 0;
            ca.pElems = IntPtr.Zero;

            int length = array.Length;

            if (length > 0)
            {
                long size = Marshal.SizeOf(type) * length;

                IntPtr destPtr =IntPtr.Zero;
                GCHandle handle = new GCHandle();

                try
                {
                    destPtr = Marshal.AllocCoTaskMem((int) size);
                    handle = GCHandle.Alloc(array, GCHandleType.Pinned);
                    unsafe
                    {
                        CopyBytes((byte *) destPtr, (int)size, (byte *)handle.AddrOfPinnedObject(), (int)size);
                    }

                    ca.cElems = (uint)length;
                    ca.pElems = destPtr;

                    destPtr = IntPtr.Zero;
                }
                finally
                {
                    if (handle.IsAllocated)
                    {
                        handle.Free();
                    }

                    if (destPtr != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(destPtr);
                    }
                }
            }
        }
Exemplo n.º 32
0
        /// <summary>
        /// Primitive types are the basic COM types. It includes valuetypes like ints, but also reference types
        /// like BStrs. It does not include composite types like arrays and user-defined COM types (IUnknown/IDispatch).
        /// </summary>
        internal static bool IsPrimitiveType(VarEnum varEnum) {
            switch (varEnum) {
                #region Generated Outer Variant IsPrimitiveType

                // *** BEGIN GENERATED CODE ***
                // generated by function: gen_IsPrimitiveType from: generate_comdispatch.py

                case VarEnum.VT_I1:
                case VarEnum.VT_I2:
                case VarEnum.VT_I4:
                case VarEnum.VT_I8:
                case VarEnum.VT_UI1:
                case VarEnum.VT_UI2:
                case VarEnum.VT_UI4:
                case VarEnum.VT_UI8:
                case VarEnum.VT_INT:
                case VarEnum.VT_UINT:
                case VarEnum.VT_BOOL:
                case VarEnum.VT_ERROR:
                case VarEnum.VT_R4:
                case VarEnum.VT_R8:
                case VarEnum.VT_DECIMAL:
                case VarEnum.VT_CY:
                case VarEnum.VT_DATE:
                case VarEnum.VT_BSTR:

                // *** END GENERATED CODE ***

                #endregion
                    return true;
            }

            return false;
        }
Exemplo n.º 33
0
 public OPCItemDef(string id, bool activ, int hclt, VarEnum vt)
 {
     ItemID = id; Active = activ; HandleClient = hclt; RequestedDataType = vt;
 }
Exemplo n.º 34
0
        internal static System.Reflection.MethodInfo GetByrefSetter(VarEnum varType) {
            switch (varType) {

                #region Generated Outer Variant byref setter

                // *** BEGIN GENERATED CODE ***
                // generated by function: gen_byref_setters from: generate_comdispatch.py

                case VarEnum.VT_I1: return typeof(Variant).GetMethod("SetAsByrefI1");
                case VarEnum.VT_I2: return typeof(Variant).GetMethod("SetAsByrefI2");
                case VarEnum.VT_I4: return typeof(Variant).GetMethod("SetAsByrefI4");
                case VarEnum.VT_I8: return typeof(Variant).GetMethod("SetAsByrefI8");
                case VarEnum.VT_UI1: return typeof(Variant).GetMethod("SetAsByrefUi1");
                case VarEnum.VT_UI2: return typeof(Variant).GetMethod("SetAsByrefUi2");
                case VarEnum.VT_UI4: return typeof(Variant).GetMethod("SetAsByrefUi4");
                case VarEnum.VT_UI8: return typeof(Variant).GetMethod("SetAsByrefUi8");
                case VarEnum.VT_INT: return typeof(Variant).GetMethod("SetAsByrefInt");
                case VarEnum.VT_UINT: return typeof(Variant).GetMethod("SetAsByrefUint");
                case VarEnum.VT_BOOL: return typeof(Variant).GetMethod("SetAsByrefBool");
                case VarEnum.VT_ERROR: return typeof(Variant).GetMethod("SetAsByrefError");
                case VarEnum.VT_R4: return typeof(Variant).GetMethod("SetAsByrefR4");
                case VarEnum.VT_R8: return typeof(Variant).GetMethod("SetAsByrefR8");
                case VarEnum.VT_DECIMAL: return typeof(Variant).GetMethod("SetAsByrefDecimal");
                case VarEnum.VT_CY: return typeof(Variant).GetMethod("SetAsByrefCy");
                case VarEnum.VT_DATE: return typeof(Variant).GetMethod("SetAsByrefDate");
                case VarEnum.VT_BSTR: return typeof(Variant).GetMethod("SetAsByrefBstr");
                case VarEnum.VT_UNKNOWN: return typeof(Variant).GetMethod("SetAsByrefUnknown");
                case VarEnum.VT_DISPATCH: return typeof(Variant).GetMethod("SetAsByrefDispatch");

                // *** END GENERATED CODE ***

                #endregion

                case VarEnum.VT_VARIANT:
                    return typeof(Variant).GetMethod("SetAsByrefVariant");
                case VarEnum.VT_RECORD:
                case VarEnum.VT_ARRAY:
                    return typeof(Variant).GetMethod("SetAsByrefVariantIndirect");

                default:
                    throw Error.VariantGetAccessorNYI(varType);
            }
        }
Exemplo n.º 35
0
 internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType,
     short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
 {
     _val = val;
     SafeArraySubType = safeArraySubType;
     SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
     IidParameterIndex = iidParamIndex;
     ArraySubType = arraySubType;
     SizeParamIndex = sizeParamIndex;
     SizeConst = sizeConst;
     MarshalType = marshalType;
     MarshalTypeRef = marshalTypeRef;
     MarshalCookie = marshalCookie;
 }
Exemplo n.º 36
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();
            }
        }
Exemplo n.º 37
0
		/// <summary>
		/// Converts a value to the specified type using COM conversion rules.
		/// </summary>
		public static int ChangeTypeForCOM(object source, VarEnum sourceType, VarEnum targetType, out object target)
		{
            target = source;
            
            // check for trivial case.
            if (sourceType == targetType)
            {
                return ResultIds.S_OK;
            }

			// check for conversions to date time from string.
            string stringValue = source as string;

			if (stringValue != null && targetType == VarEnum.VT_DATE)
			{
				try   
                { 
                    target = System.Convert.ToDateTime(stringValue);
                    return ResultIds.S_OK;
                }
				catch 
                {
                    target = null;
                    return ResultIds.DISP_E_OVERFLOW;
                }
			}

			// check for conversions from date time to boolean.
            if (sourceType == VarEnum.VT_DATE && targetType == VarEnum.VT_BOOL)
			{
				target = !(new DateTime(1899, 12, 30, 0, 0, 0).Equals((DateTime)source));
                return ResultIds.S_OK;
			}

			// check for conversions from float to double.
            if (sourceType == VarEnum.VT_R4 && targetType == VarEnum.VT_R8)
			{
				target = System.Convert.ToDouble((float)source);
                return ResultIds.S_OK;
			}

			// check for array conversion.
            Array array = source as Array;
            bool targetIsArray = ((targetType & VarEnum.VT_ARRAY) != 0);

			if (array != null && targetIsArray)
			{
                VarEnum elementType = (VarEnum)((short)targetType & ~(short)VarEnum.VT_ARRAY);

                Array convertedArray = Array.CreateInstance(GetSystemType((short)elementType), array.Length);

                for (int ii = 0; ii < array.Length; ii++)
                {
                    object elementValue = null;
                    int error = ChangeTypeForCOM(array.GetValue(ii), elementType, out elementValue);

                    if (error < 0)
                    {
                        target = null;
                        return ResultIds.DISP_E_OVERFLOW;
                    }
                        
                    convertedArray.SetValue(elementValue, ii);
                }

				target = convertedArray;
                return ResultIds.S_OK;
			}
			else if (array == null && !targetIsArray)
			{
				IntPtr pvargDest = Marshal.AllocCoTaskMem(16);
				IntPtr pvarSrc   = Marshal.AllocCoTaskMem(16);
				
				VariantInit(pvargDest);
				VariantInit(pvarSrc);

				Marshal.GetNativeVariantForObject(source, pvarSrc);

				try
				{
					// change type.
					int error = VariantChangeTypeEx(
						pvargDest, 
						pvarSrc, 
						Thread.CurrentThread.CurrentCulture.LCID, 
						VARIANT_NOVALUEPROP | VARIANT_ALPHABOOL, 
						(short)targetType);

					// check error code.
					if (error != 0)
					{
                        target = null;
                        return error;
					}

					// unmarshal result.
					object result = Marshal.GetObjectForNativeVariant(pvargDest);
					
					// check for invalid unsigned <=> signed conversions.
					switch (targetType)
					{
						case VarEnum.VT_I1:
						case VarEnum.VT_I2:
						case VarEnum.VT_I4:
						case VarEnum.VT_I8:
						case VarEnum.VT_UI1:
						case VarEnum.VT_UI2:
						case VarEnum.VT_UI4:
						case VarEnum.VT_UI8:
						{
							// ignore issue for conversions from boolean.
                            if (sourceType == VarEnum.VT_BOOL)
							{	
								break;					
							}

							decimal sourceAsDecimal = 0;
							decimal resultAsDecimal = System.Convert.ToDecimal(result);

							try   { sourceAsDecimal = System.Convert.ToDecimal(source); }
							catch { sourceAsDecimal = 0; }
							
							if ((sourceAsDecimal < 0 && resultAsDecimal > 0) || (sourceAsDecimal > 0 && resultAsDecimal < 0))
							{
                                target = null;
                                return ResultIds.E_RANGE;
							}
                            
							// conversion from datetime should have failed.
                            if (sourceType == VarEnum.VT_DATE)
							{	
                                if (resultAsDecimal == 0)
                                {
                                    target = null;
                                    return ResultIds.E_RANGE;
                                }
							}

							break;
						}		
							
						case VarEnum.VT_R8:
						{						
							// fix precision problem introduced with conversion from float to double.
                            if (sourceType == VarEnum.VT_R4)
							{	
								result = System.Convert.ToDouble(source.ToString());
							}

							break;			
						}
					}

					target = result;
                    return ResultIds.S_OK;
				}
				finally
				{
					VariantClear(pvargDest);
					VariantClear(pvarSrc);

					Marshal.FreeCoTaskMem(pvargDest);
					Marshal.FreeCoTaskMem(pvarSrc);
				}
			}
			else if (array != null && targetType == VarEnum.VT_BSTR)
			{
				int count = ((Array)source).Length;

				StringBuilder buffer = new StringBuilder();

				buffer.Append("{");

				foreach (object element in (Array)source)
				{
                    object elementValue = null;
               
                    int error = ChangeTypeForCOM(element, VarEnum.VT_BSTR, out elementValue);

                    if (error < 0)
                    {
                        target = null;
                        return error;
                    }
                    
					buffer.Append((string)elementValue);

					count--;

					if (count > 0)
					{
						buffer.Append(" | ");
					}
				}

				buffer.Append("}");

				target = buffer.ToString();
                return ResultIds.S_OK;
			}
            
			// no conversions between scalar and array types allowed.
            target = null;
            return ResultIds.E_BADTYPE;
		}
Exemplo n.º 38
0
        public static Type VarEnumToSystemType(VarEnum VarEnumType)
        {
            switch (VarEnumType)
            {
            case (VarEnum.VT_EMPTY):
            case (VarEnum.VT_NULL):
                return(typeof(Object));

            case (VarEnum.VT_UI1):
                return(typeof(Byte?));

            case (VarEnum.VT_I2):
                return(typeof(Int16?));

            case (VarEnum.VT_UI2):
                return(typeof(UInt16?));

            case (VarEnum.VT_I4):
                return(typeof(Int32?));

            case (VarEnum.VT_UI4):
                return(typeof(UInt32?));

            case (VarEnum.VT_I8):
                return(typeof(Int64?));

            case (VarEnum.VT_UI8):
                return(typeof(UInt64?));

            case (VarEnum.VT_R8):
                return(typeof(Double?));

            case (VarEnum.VT_BOOL):
                return(typeof(Boolean?));

            case (VarEnum.VT_FILETIME):
                return(typeof(DateTime?));

            case (VarEnum.VT_CLSID):
                return(typeof(IntPtr?));

            case (VarEnum.VT_CF):
                return(typeof(IntPtr?));

            case (VarEnum.VT_BLOB):
                return(typeof(Byte[]));

            case (VarEnum.VT_LPWSTR):
                return(typeof(String));

            case (VarEnum.VT_UNKNOWN):
                return(typeof(IntPtr?));

            case (VarEnum.VT_STREAM):
                return(typeof(IStream));

            case (VarEnum.VT_VECTOR | VarEnum.VT_UI1):
                return(typeof(Byte[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_I2):
                return(typeof(Int16[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_UI2):
                return(typeof(UInt16[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_I4):
                return(typeof(Int32[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_UI4):
                return(typeof(UInt32[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_I8):
                return(typeof(Int64[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_UI8):
                return(typeof(UInt64[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_R8):
                return(typeof(Double[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_BOOL):
                return(typeof(Boolean[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_FILETIME):
                return(typeof(DateTime[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_CLSID):
                return(typeof(IntPtr[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_CF):
                return(typeof(IntPtr[]));

            case (VarEnum.VT_VECTOR | VarEnum.VT_LPWSTR):
                return(typeof(String[]));

            default:
                return(typeof(Object));
            }
        }
Exemplo n.º 39
0
        internal static System.Reflection.PropertyInfo GetAccessor(VarEnum varType) {
            switch (varType) {

                #region Generated Outer Variant accessors PropertyInfos

                // *** BEGIN GENERATED CODE ***
                // generated by function: gen_accessor_propertyinfo from: generate_comdispatch.py

                case VarEnum.VT_I1: return typeof(Variant).GetProperty("AsI1");
                case VarEnum.VT_I2: return typeof(Variant).GetProperty("AsI2");
                case VarEnum.VT_I4: return typeof(Variant).GetProperty("AsI4");
                case VarEnum.VT_I8: return typeof(Variant).GetProperty("AsI8");
                case VarEnum.VT_UI1: return typeof(Variant).GetProperty("AsUi1");
                case VarEnum.VT_UI2: return typeof(Variant).GetProperty("AsUi2");
                case VarEnum.VT_UI4: return typeof(Variant).GetProperty("AsUi4");
                case VarEnum.VT_UI8: return typeof(Variant).GetProperty("AsUi8");
                case VarEnum.VT_INT: return typeof(Variant).GetProperty("AsInt");
                case VarEnum.VT_UINT: return typeof(Variant).GetProperty("AsUint");
                case VarEnum.VT_BOOL: return typeof(Variant).GetProperty("AsBool");
                case VarEnum.VT_ERROR: return typeof(Variant).GetProperty("AsError");
                case VarEnum.VT_R4: return typeof(Variant).GetProperty("AsR4");
                case VarEnum.VT_R8: return typeof(Variant).GetProperty("AsR8");
                case VarEnum.VT_DECIMAL: return typeof(Variant).GetProperty("AsDecimal");
                case VarEnum.VT_CY: return typeof(Variant).GetProperty("AsCy");
                case VarEnum.VT_DATE: return typeof(Variant).GetProperty("AsDate");
                case VarEnum.VT_BSTR: return typeof(Variant).GetProperty("AsBstr");
                case VarEnum.VT_UNKNOWN: return typeof(Variant).GetProperty("AsUnknown");
                case VarEnum.VT_DISPATCH: return typeof(Variant).GetProperty("AsDispatch");

                // *** END GENERATED CODE ***

                #endregion

                case VarEnum.VT_VARIANT:
                case VarEnum.VT_RECORD:
                case VarEnum.VT_ARRAY:
                    return typeof(Variant).GetProperty("AsVariant");

                default:
                    throw Error.VariantGetAccessorNYI(varType);
            }
        }
Exemplo n.º 40
0
        /// <summary>
        /// Is there a unique primitive type that has the best conversion for the argument
        /// </summary>
        private static bool TryGetPrimitiveComTypeViaConversion(Type argumentType, out VarEnum primitiveVarEnum) {
            // Look for a unique type family that the argument can be converted to.
            List<VarEnum> compatibleComTypes = GetConversionsToComPrimitiveTypeFamilies(argumentType);
            CheckForAmbiguousMatch(argumentType, compatibleComTypes);
            if (compatibleComTypes.Count == 1) {
                primitiveVarEnum = compatibleComTypes[0];
                return true;
            }

            primitiveVarEnum = VarEnum.VT_VOID; // error
            return false;
        }
Exemplo n.º 41
0
 internal VariantBuilder(VarEnum targetComType, ArgBuilder builder) {
     _targetComType = targetComType;
     _argBuilder = builder;
 }
Exemplo n.º 42
0
        // This helper can produce a builder for types that are directly supported by Variant.
        private static SimpleArgBuilder GetSimpleArgBuilder(Type elementType, VarEnum elementVarEnum) {
            SimpleArgBuilder argBuilder;

            switch (elementVarEnum) {
                case VarEnum.VT_BSTR:
                    argBuilder = new StringArgBuilder(elementType);
                    break;
                case VarEnum.VT_BOOL:
                    argBuilder = new BoolArgBuilder(elementType);
                    break;
                case VarEnum.VT_DATE:
                    argBuilder = new DateTimeArgBuilder(elementType);
                    break;
                case VarEnum.VT_CY:
                    argBuilder = new CurrencyArgBuilder(elementType);
                    break;
                case VarEnum.VT_DISPATCH:
                    argBuilder = new DispatchArgBuilder(elementType);
                    break;
                case VarEnum.VT_UNKNOWN:
                    argBuilder = new UnknownArgBuilder(elementType);
                    break;
                case VarEnum.VT_VARIANT:
                case VarEnum.VT_ARRAY:
                case VarEnum.VT_RECORD:
                    argBuilder = new VariantArgBuilder(elementType);
                    break;
                case VarEnum.VT_ERROR:
                    argBuilder = new ErrorArgBuilder(elementType);
                    break;
                default:
                    argBuilder = new SimpleArgBuilder(elementType);
                    break;
            }

            return argBuilder;
        }
Exemplo n.º 43
0
        /// <summary>
        /// This function returns the Data bytes that accurately describes the object
        /// </summary>
        /// <returns></returns>
        internal static void EncodeAttribute(Guid guid, object value, VarEnum type, Stream stream)
        {
            // samgeo - Presharp issue
            // Presharp gives a warning when local IDisposable variables are not closed
            // in this case, we can't call Dispose since it will also close the underlying stream
            // which still needs to be written to
#pragma warning disable 1634, 1691
#pragma warning disable 6518
            BinaryWriter bw = new BinaryWriter(stream);

            // if this guid used the legacy internal attribute persistence APIs,
            //      then it doesn't include embedded type information (it's always a byte array)
            if (UsesEmbeddedTypeInformation(guid))
            {
                //
                ushort datatype = (ushort)type;
                bw.Write(datatype);
            }
            // We know the type of the object. We must serialize it accordingly.
            switch(type)
            {
                case (VarEnum.VT_ARRAY | VarEnum.VT_I1)://8208
                {
                    char[] data = (char[])value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_UI1)://8209
                {
                    byte[] data = (byte[])value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_I2)://8194
                {
                    short [] data = (short[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_UI2)://8210
                {
                    ushort [] data = (ushort[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_I4)://8195
                {
                    int [] data = (int[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_UI4)://8211
                {
                    uint [] data = (uint[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_I8)://8212
                {
                    long [] data = (long[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_UI8)://8213
                {
                    ulong [] data = (ulong[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_R4 )://8196
                {
                    float [] data = (float[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_R8)://8197
                {
                    double [] data = (double[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_DATE)://8199
                {
                    DateTime [] data = (DateTime[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i].ToOADate());
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_BOOL )://8203
                {
                    bool [] data = (bool[])value;
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i])
                        {
                            //true is two consecutive all bits on bytes
                            bw.Write((byte)0xFF);
                            bw.Write((byte)0xFF);
                        }
                        else
                        {
                            //false is two consecutive all bits off
                            bw.Write((byte)0);
                            bw.Write((byte)0);
                        }
                    }
                    break;
                }
                case (VarEnum.VT_ARRAY | VarEnum.VT_DECIMAL)://8206
                {
                    decimal [] data = (decimal[])value;
                    for( int i = 0; i < data.Length; i++ )
                        bw.Write(data[i]);
                    break;
                }
                case (VarEnum.VT_I1)://16
                {
                    char data = (char)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_UI1)://17
                {
                    byte data = (byte)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_I2)://2
                {
                    short data = (short)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_UI2)://18
                {
                    ushort data = (ushort)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_I4)://3
                {
                    int data = (int)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_UI4)://19
                {
                    uint data = (uint)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_I8)://20
                {
                    long data = (long)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_UI8)://21
                {
                    ulong data = (ulong)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_R4 )://4
                {
                    float data = (float)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_R8)://5
                {
                    double data = (double)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_DATE)://7
                {
                    DateTime data = (DateTime)value;
                    bw.Write(data.ToOADate());
                    break;
                }
                case (VarEnum.VT_BOOL )://11
                {
                    bool data = (bool)value;
                    if (data)
                    {
                        //true is two consecutive all bits on bytes
                        bw.Write((byte)0xFF);
                        bw.Write((byte)0xFF);
                    }
                    else
                    {
                        //false is two consecutive all bits off bytes
                        bw.Write((byte)0);
                        bw.Write((byte)0);
                    }
                    break;
                }
                case (VarEnum.VT_DECIMAL)://14
                {
                    decimal data = (decimal)value;
                    bw.Write(data);
                    break;
                }
                case (VarEnum.VT_BSTR)://8
                {
                    string data = (string)value;
                    bw.Write( System.Text.Encoding.Unicode.GetBytes( data ) );
                    break;
                }
                default:
                {
                    throw new InvalidOperationException(SR.Get(SRID.InvalidEpInIsf));
                }
            }
#pragma warning restore 6518
#pragma warning restore 1634, 1691
        }
Exemplo n.º 44
0
 public void GetNativeVariantForObject_ComObject_Success(object obj, VarEnum expectedVarType)
 {
     GetNativeVariantForObject_ValidObject_Success(obj, expectedVarType, (IntPtr)(-1), obj);
 }
Exemplo n.º 45
0
        private static bool TryGetPrimitiveComType(Type argumentType, out VarEnum primitiveVarEnum) {
            // Look for an exact match with a COM primitive type

            foreach (KeyValuePair<VarEnum, Type> kvp in _ComToManagedPrimitiveTypes) {
                if (kvp.Value == argumentType) {
                    primitiveVarEnum = kvp.Key;
                    return true;
                }
            }

            primitiveVarEnum = VarEnum.VT_VOID; // error
            return false;
        }
Exemplo n.º 46
0
        IAttribute ConvertMarshalInfo(SRM.BlobReader marshalInfo)
        {
            var   b = new AttributeBuilder(module, KnownAttribute.MarshalAs);
            IType unmanagedTypeType = module.Compilation.FindType(new TopLevelTypeName(InteropServices, nameof(UnmanagedType)));

            int type = marshalInfo.ReadByte();

            b.AddFixedArg(unmanagedTypeType, type);

            int size;

            switch (type)
            {
            case 0x1e:                     // FixedArray
                if (!marshalInfo.TryReadCompressedInteger(out size))
                {
                    size = 0;
                }
                b.AddNamedArg("SizeConst", KnownTypeCode.Int32, size);
                if (marshalInfo.RemainingBytes > 0)
                {
                    type = marshalInfo.ReadByte();
                    if (type != 0x66)                             // None
                    {
                        b.AddNamedArg("ArraySubType", unmanagedTypeType, type);
                    }
                }
                break;

            case 0x1d:                     // SafeArray
                if (marshalInfo.RemainingBytes > 0)
                {
                    VarEnum varType = (VarEnum)marshalInfo.ReadByte();
                    if (varType != VarEnum.VT_EMPTY)
                    {
                        var varEnumType = new TopLevelTypeName(InteropServices, nameof(VarEnum));
                        b.AddNamedArg("SafeArraySubType", varEnumType, (int)varType);
                    }
                }
                break;

            case 0x2a:                     // NATIVE_TYPE_ARRAY
                if (marshalInfo.RemainingBytes > 0)
                {
                    type = marshalInfo.ReadByte();
                }
                else
                {
                    type = 0x66;                            // Cecil uses NativeType.None as default.
                }
                if (type != 0x50)                           // Max
                {
                    b.AddNamedArg("ArraySubType", unmanagedTypeType, type);
                }
                int sizeParameterIndex = marshalInfo.TryReadCompressedInteger(out int value) ? value : -1;
                size = marshalInfo.TryReadCompressedInteger(out value) ? value : -1;
                int sizeParameterMultiplier = marshalInfo.TryReadCompressedInteger(out value) ? value : -1;
                if (size >= 0)
                {
                    b.AddNamedArg("SizeConst", KnownTypeCode.Int32, size);
                }
                if (sizeParameterMultiplier != 0 && sizeParameterIndex >= 0)
                {
                    b.AddNamedArg("SizeParamIndex", KnownTypeCode.Int16, (short)sizeParameterIndex);
                }
                break;

            case 0x2c:                     // CustomMarshaler
                string guidValue     = marshalInfo.ReadSerializedString();
                string unmanagedType = marshalInfo.ReadSerializedString();
                string managedType   = marshalInfo.ReadSerializedString();
                string cookie        = marshalInfo.ReadSerializedString();
                if (managedType != null)
                {
                    b.AddNamedArg("MarshalType", KnownTypeCode.String, managedType);
                }
                if (!string.IsNullOrEmpty(cookie))
                {
                    b.AddNamedArg("MarshalCookie", KnownTypeCode.String, cookie);
                }
                break;

            case 0x17:                     // FixedSysString
                b.AddNamedArg("SizeConst", KnownTypeCode.Int32, marshalInfo.ReadCompressedInteger());
                break;
            }

            return(b.Build());
        }
Exemplo n.º 47
0
        // This helper is called when we are looking for a ByVal marhsalling
        // In a ByVal case we can take into account conversions or IConvertible if all other 
        // attempts to find marshalling type failed 
        private static ArgBuilder GetByValArgBuilder(Type elementType, ref VarEnum elementVarEnum) {
            // if VT indicates that marshalling type is unknown
            if (elementVarEnum == VT_DEFAULT) {
                //trying to find a conversion.
                VarEnum convertibleTo;
                if (TryGetPrimitiveComTypeViaConversion(elementType, out convertibleTo)) {
                    elementVarEnum = convertibleTo;
                    Type marshalType = GetManagedMarshalType(elementVarEnum);
                    return new ConversionArgBuilder(elementType, GetSimpleArgBuilder(marshalType, elementVarEnum));
                }

                //checking for IConvertible.
                if (typeof(IConvertible).IsAssignableFrom(elementType)) {
                    return new ConvertibleArgBuilder();
                }
            }
            return GetSimpleArgBuilder(elementType, elementVarEnum);
        }
Exemplo n.º 48
0
 public void GetNativeVariantForObject_WrappedComObject_Success(object obj, object wrapped, VarEnum expectedVarType)
 {
     GetNativeVariantForObject_ValidObject_Success(obj, expectedVarType, (IntPtr)(-1), wrapped);
 }
Exemplo n.º 49
0
        internal object ToObject(object syncObject)
        {
            VarEnum vt = (VarEnum)varType;

            if ((vt & VarEnum.VT_VECTOR) != 0)
            {
                switch (vt & (~VarEnum.VT_VECTOR))
                {
                case VarEnum.VT_EMPTY:
                    return(null);

                case VarEnum.VT_I1:
                {
                    sbyte[] array = new sbyte[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        array[i] = (sbyte)Marshal.ReadByte(ca.pElems, i);
                    }
                    return(array);
                }

                case VarEnum.VT_UI1:
                {
                    byte[] array = new byte[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_I2:
                {
                    short[] array = new short[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_UI2:
                {
                    ushort[] array = new ushort[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        array[i] = (ushort)Marshal.ReadInt16(ca.pElems, i * sizeof(ushort));
                    }
                    return(array);
                }

                case VarEnum.VT_I4:
                {
                    int[] array = new int[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_UI4:
                {
                    uint[] array = new uint[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        array[i] = (uint)Marshal.ReadInt32(ca.pElems, i * sizeof(uint));
                    }
                    return(array);
                }

                case VarEnum.VT_I8:
                {
                    Int64[] array = new Int64[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_UI8:
                {
                    UInt64[] array = new UInt64[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        array[i] = (UInt64)Marshal.ReadInt64(ca.pElems, i * sizeof(UInt64));
                    }
                    return(array);
                }

                case VarEnum.VT_R4:
                {
                    float[] array = new float[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_R8:
                {
                    double[] array = new double[ca.cElems];
                    Marshal.Copy(ca.pElems, array, 0, (int)ca.cElems);
                    return(array);
                }

                case VarEnum.VT_BOOL:
                {
                    bool[] array = new bool[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        array[i] = (bool)(Marshal.ReadInt16(ca.pElems, i * sizeof(ushort)) != 0);
                    }
                    return(array);
                }

                case VarEnum.VT_CLSID:
                {
                    Guid[] array = new Guid[ca.cElems];
                    for (int i = 0; i < ca.cElems; i++)
                    {
                        byte[] guid = new byte[16];
                        Marshal.Copy(ca.pElems, guid, i * 16, 16);
                        array[i] = new Guid(guid);
                    }
                    return(array);
                }

                case VarEnum.VT_LPSTR:
                {
                    String[] array      = new String[ca.cElems];
                    int      sizeIntPtr = 0;
                    unsafe
                    {
                        sizeIntPtr = sizeof(IntPtr);
                    }

                    for (int i = 0; i < ca.cElems; i++)
                    {
                        IntPtr ptr = Marshal.ReadIntPtr(ca.pElems, i * sizeIntPtr);
                        array[i] = Marshal.PtrToStringAnsi(ptr);
                    }
                    return(array);
                }

                case VarEnum.VT_LPWSTR:
                {
                    String[] array      = new String[ca.cElems];
                    int      sizeIntPtr = 0;
                    unsafe
                    {
                        sizeIntPtr = sizeof(IntPtr);
                    }

                    for (int i = 0; i < ca.cElems; i++)
                    {
                        IntPtr ptr = Marshal.ReadIntPtr(ca.pElems, i * sizeIntPtr);
                        array[i] = Marshal.PtrToStringUni(ptr);
                    }
                    return(array);
                }

                case VarEnum.VT_UNKNOWN:
                default:
                    break;
                }
            }
            else
            {
                switch (vt)
                {
                case VarEnum.VT_EMPTY:
                    return(null);

                case VarEnum.VT_I1:
                    return(cVal);

                case VarEnum.VT_UI1:
                    return(bVal);

                case VarEnum.VT_I2:
                    return(iVal);

                case VarEnum.VT_UI2:
                    return(uiVal);

                case VarEnum.VT_I4:
                    return(intVal);

                case VarEnum.VT_UI4:
                    return(uintVal);

                case VarEnum.VT_I8:
                    return(lVal);

                case VarEnum.VT_UI8:
                    return(ulVal);

                case VarEnum.VT_R4:
                    return(fltVal);

                case VarEnum.VT_R8:
                    return(dblVal);

                case VarEnum.VT_FILETIME:
                    return(filetime);

                case VarEnum.VT_BOOL:
                    return((bool)(boolVal != 0));

                case VarEnum.VT_CLSID:
                    byte[] guid = new byte[16];
                    Marshal.Copy(pclsidVal, guid, 0, 16);
                    return(new Guid(guid));

                case VarEnum.VT_LPSTR:
                    return(Marshal.PtrToStringAnsi(pszVal));

                case VarEnum.VT_LPWSTR:
                    return(Marshal.PtrToStringUni(pwszVal));

                case VarEnum.VT_BLOB:
                {
                    byte[] blob = new byte[ca.cElems];
                    Marshal.Copy(ca.pElems, blob, 0, (int)ca.cElems);
                    return(new BitmapMetadataBlob(blob));
                }

                case VarEnum.VT_UNKNOWN:
                {
                    IntPtr queryHandle         = IntPtr.Zero;
                    Guid   guidIWICQueryWriter = MILGuidData.IID_IWICMetadataQueryWriter;
                    Guid   guidIWICQueryReader = MILGuidData.IID_IWICMetadataQueryReader;

                    try
                    {
                        int hr = UnsafeNativeMethods.MILUnknown.QueryInterface(punkVal, ref guidIWICQueryWriter, out queryHandle);

                        if (hr == HRESULT.S_OK)
                        {
                            // It's a IWICMetadataQueryWriter interface - read and write
                            SafeMILHandle metadataHandle = new SafeMILHandle(queryHandle);

                            // To avoid releasing the queryHandle in finally.
                            queryHandle = IntPtr.Zero;

                            return(new BitmapMetadata(metadataHandle, false, false, syncObject));
                        }
                        else
                        {
                            hr = UnsafeNativeMethods.MILUnknown.QueryInterface(punkVal, ref guidIWICQueryReader, out queryHandle);

                            if (hr == HRESULT.S_OK)
                            {
                                // It's a IWICMetadataQueryReader interface - read only
                                SafeMILHandle metadataHandle = new SafeMILHandle(queryHandle);

                                // To avoid releasing the queryHandle in finally.
                                queryHandle = IntPtr.Zero;

                                return(new BitmapMetadata(metadataHandle, true, false, syncObject));
                            }

                            HRESULT.Check(hr);
                        }
                    }
                    finally
                    {
                        if (queryHandle != IntPtr.Zero)
                        {
                            UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref queryHandle);
                        }
                    }
                    break;
                }

                default:
                    break;
                }
            }

            throw new System.NotSupportedException(SR.Get(SRID.Image_PropertyNotSupported));
        }
Exemplo n.º 50
0
 public void GetNativeVariantForObject_ComObjectArray_Success(object obj, VarEnum expectedVarType, object expectedRoundtripValue)
 {
     GetNativeVariantForObject_ValidObject_Success(obj, expectedVarType, (IntPtr)(-1), expectedRoundtripValue);
 }