/// <summary> /// Returns the property IDs and values for all data contained in this data report. /// </summary> /// <returns>A dictionary mapping property IDs to values.</returns> public IDictionary <PropertyKey, object> GetDataFields() { Dictionary <PropertyKey, object> reportData = new Dictionary <PropertyKey, object>(); IPortableDeviceValues valuesCollection; _iSensorReport.GetSensorValues(null, out valuesCollection); uint nItems = 0; valuesCollection.GetCount(ref nItems); for (uint i = 0; i < nItems; i++) { PropertyKey propKey = new PropertyKey(); PROPVARIANT propValue = new PROPVARIANT(); valuesCollection.GetAt(i, ref propKey, out propValue); try { reportData.Add(propKey, propValue.Value); } finally { propValue.Clear(); } } return(reportData); }
/// <summary> /// Retrives the values of multiple properties. /// </summary> /// <param name="propKeys">Properties to retrieve.</param> /// <returns>A dictionary containing the property keys and values.</returns> public IDictionary <PropertyKey, object> GetAllProperties() { IPortableDeviceValues valuesCollection; _iSensor.GetProperties(null, out valuesCollection); Dictionary <PropertyKey, object> data = new Dictionary <PropertyKey, object>(); if (valuesCollection == null) { return(data); } uint count = 0; valuesCollection.GetCount(ref count); for (uint i = 0; i < count; i++) { PropertyKey propKey = new PropertyKey(); PROPVARIANT propVal = new PROPVARIANT(); valuesCollection.GetAt(i, ref propKey, out propVal); try { data.Add(propKey, propVal.Value); } finally { propVal.Clear(); } } return(data); }
// clear the ChunkValue public void Clear() { m_fIsValid = false; m_chunk = default; m_propVariant.Clear(); m_pszValue = null; }
/// <summary> /// Retrives the values of multiple properties by their indices. /// Assues that the GUID component of property keys is the sensor's type GUID. /// </summary> /// <param name="propIndices">Indices of properties to retrieve.</param> /// <returns>An array containing the property values.</returns> /// <remarks> /// If the values of some properties could not be retrieved, then the returned array will contain null values in the corresponding positions. /// </remarks> public object[] GetProperties(params int[] propIndices) { if (propIndices == null || propIndices.Length == 0) { throw new ArgumentNullException("propIndices", "Property keys array must not be null or empty."); } IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection(); try { IPortableDeviceValues valuesCollection; Dictionary <PropertyKey, int> propKeyToIdx = new Dictionary <PropertyKey, int>(); for (int i = 0; i < propIndices.Length; i++) { PropertyKey propKey = PropertyKey.Create(this.TypeID, propIndices[i]); keyCollection.Add(ref propKey); propKeyToIdx.Add(propKey, i); } object[] data = new object[propIndices.Length]; _iSensor.GetProperties(keyCollection, out valuesCollection); if (valuesCollection == null) { return(data); } uint count = 0; valuesCollection.GetCount(ref count); for (uint i = 0; i < count; i++) { PropertyKey propKey = new PropertyKey(); PROPVARIANT propVal = new PROPVARIANT(); valuesCollection.GetAt(i, ref propKey, out propVal); try { int idx = propKeyToIdx[propKey]; data[idx] = propVal.Value; } finally { propVal.Clear(); } } return(data); } finally { Marshal.ReleaseComObject(keyCollection); } }
private static IShellLink CreateShellLink(string title, string path, string arguments, string icon_path, int icon_pos) { try { IShellLink shell_link = (IShellLink)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID.ShellLink)); shell_link.SetPath(path); if (!string.IsNullOrEmpty(arguments)) { shell_link.SetArguments(arguments); } if (!string.IsNullOrEmpty(icon_path)) { shell_link.SetIconLocation(icon_path, icon_pos); } IntPtr pps; Guid ipsiid = CLSID.IPropertyStore; Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref ipsiid, out pps); IPropertyStore property_store = (IPropertyStore)Marshal.GetTypedObjectForIUnknown(pps, typeof(IPropertyStore)); PROPVARIANT propvar = new PROPVARIANT(); propvar.SetString(title); // PKEY_Title PROPERTYKEY PKEY_Title = new PROPERTYKEY(); PKEY_Title.fmtid = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9"); PKEY_Title.pid = 2; property_store.SetValue(ref PKEY_Title, ref propvar); property_store.Commit(); IntPtr psl; Guid psliid = CLSID.IShellLinkW; Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref psliid, out psl); IShellLink link = (IShellLink)Marshal.GetTypedObjectForIUnknown(psl, typeof(IShellLink)); propvar.Clear(); Marshal.ReleaseComObject(property_store); property_store = null; Marshal.ReleaseComObject(shell_link); shell_link = null; return(link); } catch (COMException e) { Logger.Error("Error createing shell link: {0}\n{1}", e.Message, e.StackTrace); } return(null); }
/// <summary> /// Retrives the values of multiple properties. /// </summary> /// <param name="propKeys">Properties to retrieve.</param> /// <returns>A dictionary containing the property keys and values.</returns> public IDictionary <PropertyKey, object> GetProperties(PropertyKey[] propKeys) { if (propKeys == null || propKeys.Length == 0) { throw new ArgumentNullException("propKeys", "Property keys array must not be null or empty."); } IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection(); try { IPortableDeviceValues valuesCollection; for (int i = 0; i < propKeys.Length; i++) { PropertyKey propKey = propKeys[i]; keyCollection.Add(ref propKey); } _iSensor.GetProperties(keyCollection, out valuesCollection); Dictionary <PropertyKey, object> data = new Dictionary <PropertyKey, object>(); if (valuesCollection == null) { return(data); } uint count = 0; valuesCollection.GetCount(ref count); for (uint i = 0; i < count; i++) { PropertyKey propKey = new PropertyKey(); PROPVARIANT propVal = new PROPVARIANT(); valuesCollection.GetAt(i, ref propKey, out propVal); try { data.Add(propKey, propVal.Value); } finally { propVal.Clear(); } } return(data); } finally { Marshal.ReleaseComObject(keyCollection); } }
/// <summary> /// Returns the property value for the given property ID. /// </summary> /// <param name="propKey">Property ID.</param> /// <returns>A value (may be of various types).</returns> public object GetDataField(PropertyKey propKey) { PROPVARIANT propVal = new PROPVARIANT(); try { _iSensorReport.GetSensorValue(ref propKey, out propVal); return(propVal.Value); } finally { propVal.Clear(); } }
/// <summary> /// Retrieves a property value by property key. /// </summary> /// <param name="propKey">Property key.</param> /// <returns>Property's value.</returns> public object GetProperty(PropertyKey propKey) { PROPVARIANT propVariant = new PROPVARIANT(); try { _iSensor.GetProperty(ref propKey, out propVariant); return(propVariant.Value); } finally { propVariant.Clear(); } }
// Getting inconsistent results. Need to work on code. public Bitmap GetThumbnail() { Bitmap bitmap = null; PROPVARIANT propvar = default(PROPVARIANT); try { Marshal.ThrowExceptionForHR(GetPropertyValue(FMTID.FMTID_ExtendedSummaryInformation, (uint)ExtendedSummaryInformationConstants.LARGE_DIB, out propvar)); if (propvar.Type == VarEnum.VT_CF) { // Get CLIPDATA. CLIPDATA clipdata = propvar.GetCLIPDATA(); // built-in Windows format if (clipdata.ulClipFmt == -1) { // Pointer to BITMAPINFOHEADER. IntPtr pBIH = clipdata.pClipData; bitmap = DibToBitmap.Convert(pBIH); } else { Console.WriteLine("CLIP FORMAT ERROR"); Marshal.ThrowExceptionForHR(HRESULT.DV_E_CLIPFORMAT); } } else { Console.WriteLine("INVALID VARIANT ERROR"); Marshal.ThrowExceptionForHR(HRESULT.ERROR_INVALID_VARIANT); } } catch { throw; } finally { propvar.Clear(); } return(bitmap); }
/// <summary> /// Sets the values of multiple properties. /// </summary> /// <param name="data">The keys and values of properties to set.</param> /// <returns>The new values of the properties. Actual values may not match requested values.</returns> public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data) { if (data == null || data.Length == 0) { throw new ArgumentNullException("data", "Data field array must not be null or empty."); } IPortableDeviceValues pdv = new PortableDeviceValues(); for (int i = 0; i < data.Length; i++) { PropertyKey propKey = data[i].Key; object value = data[i].Value; if (value == null) { throw new ArgumentNullException("data", String.Format("Data contains a null value at index {0}", i)); } if (value is string) { pdv.SetStringValue(ref propKey, (string)value); } else if (value is uint) { pdv.SetUnsignedIntegerValue(ref propKey, (uint)value); } else if (value is int) { pdv.SetSignedIntegerValue(ref propKey, (int)value); } else if (value is ulong) { pdv.SetUnsignedLargeIntegerValue(ref propKey, (ulong)value); } else if (value is long) { pdv.SetSignedLargeIntegerValue(ref propKey, (long)value); } else if (value is float || value is double) { pdv.SetFloatValue(ref propKey, (float)value); } else if (value is bool) { pdv.SetBoolValue(ref propKey, ((bool)value) ? 1 : 0); } else if (value is Guid) { Guid guid = (Guid)value; pdv.SetGuidValue(ref propKey, ref guid); } else if (value is byte[]) { byte[] buffer = (byte[])value; pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length); } else { pdv.SetIUnknownValue(ref propKey, value); } } IPortableDeviceValues pdv2 = null; _iSensor.SetProperties(pdv, out pdv2); Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>(); if (pdv2 == null) { return(results); } uint count = 0; pdv2.GetCount(ref count); for (uint i = 0; i < count; i++) { PropertyKey propKey = new PropertyKey(); PROPVARIANT propVal = new PROPVARIANT(); try { pdv2.GetAt(i, ref propKey, out propVal); results.Add(propKey, propVal.Value); } finally { propVal.Clear(); } } return(results); }