private static Array CrackSingleDimSafeArray(IntPtr psa) { uint cDims = PropVariantNativeMethods.SafeArrayGetDim(psa); if (cDims != 1) { throw new ArgumentException("LocalizedMessages.PropVariantMultiDimArray", "psa"); } int lBound = PropVariantNativeMethods.SafeArrayGetLBound(psa, 1U); int uBound = PropVariantNativeMethods.SafeArrayGetUBound(psa, 1U); int n = uBound - lBound + 1; // uBound is inclusive object[] array = new object[n]; for (int i = lBound; i <= uBound; ++i) { array[i] = PropVariantNativeMethods.SafeArrayGetElement(psa, ref i); } return(array); }
private Array GetVector <T>() { int count = PropVariantNativeMethods.PropVariantGetElementCount(this); if (count <= 0) { return(null); } if (typeof(T) == typeof(byte)) { return(this.GetBlobData() as T[]); } lock (_padlock) { if (_vectorActions == null) { _vectorActions = GenerateVectorActions(); } } Action <PropVariant, Array, uint> action; if (!_vectorActions.TryGetValue(typeof(T), out action)) { throw new InvalidCastException("LocalizedMessages.PropVariantUnsupportedType"); } Array array = new T[count]; for (uint i = 0; i < count; i++) { action(this, array, i); } return(array); }
/// <summary> /// Disposes the object, calls the clear function. /// </summary> public void Dispose() { PropVariantNativeMethods.PropVariantClear(this); GC.SuppressFinalize(this); }
private static Dictionary <Type, Action <PropVariant, Array, uint> > GenerateVectorActions() { Dictionary <Type, Action <PropVariant, Array, uint> > cache = new Dictionary <Type, Action <PropVariant, Array, uint> >(); cache.Add(typeof(Int16), (pv, array, i) => { short val; PropVariantNativeMethods.PropVariantGetInt16Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(UInt16), (pv, array, i) => { ushort val; PropVariantNativeMethods.PropVariantGetUInt16Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(Int32), (pv, array, i) => { int val; PropVariantNativeMethods.PropVariantGetInt32Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(UInt32), (pv, array, i) => { uint val; PropVariantNativeMethods.PropVariantGetUInt32Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(Int64), (pv, array, i) => { long val; PropVariantNativeMethods.PropVariantGetInt64Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(UInt64), (pv, array, i) => { ulong val; PropVariantNativeMethods.PropVariantGetUInt64Elem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(DateTime), (pv, array, i) => { System.Runtime.InteropServices.ComTypes.FILETIME val; PropVariantNativeMethods.PropVariantGetFileTimeElem(pv, i, out val); long fileTime = GetFileTimeAsLong(ref val); array.SetValue(DateTime.FromFileTime(fileTime), i); }); cache.Add(typeof(Boolean), (pv, array, i) => { bool val; PropVariantNativeMethods.PropVariantGetBooleanElem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(Double), (pv, array, i) => { double val; PropVariantNativeMethods.PropVariantGetDoubleElem(pv, i, out val); array.SetValue(val, i); }); cache.Add(typeof(Single), (pv, array, i) => // float { float[] val = new float[1]; Marshal.Copy(pv._ptr2, val, (int)i, 1); array.SetValue(val[0], (int)i); }); cache.Add(typeof(Decimal), (pv, array, i) => { int[] val = new int[4]; for (int a = 0; a < val.Length; a++) { val[a] = Marshal.ReadInt32(pv._ptr2, (int)i * sizeof(decimal) + a * sizeof(int)); //index * size + offset quarter } array.SetValue(new decimal(val), i); }); cache.Add(typeof(String), (pv, array, i) => { string val = string.Empty; PropVariantNativeMethods.PropVariantGetStringElem(pv, i, ref val); array.SetValue(val, i); }); return(cache); }