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); }
Array CrackSingleDimSafeArray(IntPtr psa) { uint cDims = PropVariantNativeMethods.SafeArrayGetDim(psa); if (cDims != 1) { throw new ArgumentException("Multi-dimensional SafeArrays not supported."); } 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); }
/// <summary> /// Set a safe array value /// </summary> /// <param name="array">The new value to set.</param> public void SetSafeArray(Array array) { const ushort vtUnknown = 13; IntPtr psa = PropVariantNativeMethods.SafeArrayCreateVector(vtUnknown, 0, (uint)array.Length); IntPtr pvData = PropVariantNativeMethods.SafeArrayAccessData(psa); try // to remember to release lock on data { for (int i = 0; i < array.Length; ++i) { object obj = array.GetValue(i); IntPtr punk = (obj != null) ? Marshal.GetIUnknownForObject(obj) : IntPtr.Zero; Marshal.WriteIntPtr(pvData, i * IntPtr.Size, punk); } } finally { PropVariantNativeMethods.SafeArrayUnaccessData(psa); } this.valueType = (ushort)VarEnum.VT_ARRAY | (ushort)VarEnum.VT_UNKNOWN; this.valueData = psa; }
/// <summary> /// Disposes the object, calls the clear function. /// </summary> public void Dispose() { PropVariantNativeMethods.PropVariantClear(this); GC.SuppressFinalize(this); }
private Array GetVector <T>() where T : struct { int count = PropVariantNativeMethods.PropVariantGetElementCount(ref this); if (count <= 0) { return(null); } Array arr = new T[count]; for (uint i = 0; i < count; i++) { if (typeof(T) == typeof(Int16)) { short val; PropVariantNativeMethods.PropVariantGetInt16Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt16)) { ushort val; PropVariantNativeMethods.PropVariantGetUInt16Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Int32)) { int val; PropVariantNativeMethods.PropVariantGetInt32Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt32)) { uint val; PropVariantNativeMethods.PropVariantGetUInt32Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Int64)) { long val; PropVariantNativeMethods.PropVariantGetInt64Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt64)) { ulong val; PropVariantNativeMethods.PropVariantGetUInt64Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(DateTime)) { System.Runtime.InteropServices.ComTypes.FILETIME val; PropVariantNativeMethods.PropVariantGetFileTimeElem(ref this, i, out val); long fileTime = FileTimeToDateTime(ref val); arr.SetValue(DateTime.FromFileTime(fileTime), i); } else if (typeof(T) == typeof(Boolean)) { bool val; PropVariantNativeMethods.PropVariantGetBooleanElem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Double)) { double val; PropVariantNativeMethods.PropVariantGetDoubleElem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(String)) { string val; PropVariantNativeMethods.PropVariantGetStringElem(ref this, i, out val); arr.SetValue(val, i); } } return(arr); }
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); }