internal static IntPtr PidlFromUnknown(IntPtr unknown) { IntPtr pidl; int retCode = ShellNativeMethods.SHGetIDListFromObject(unknown, out pidl); return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero); }
/// <summary> /// Returns a formatted, Unicode string representation of a property value. /// </summary> /// <param name="format"> /// One or more of the PropertyDescriptionFormat flags /// that indicate the desired format. /// </param> /// <param name="formattedString"> /// The formatted value as a string, or null if this property /// cannot be formatted for display. /// </param> /// <returns> /// True if the method successfully locates the formatted string; otherwise /// False. /// </returns> public bool TryFormatForDisplay(PropertyDescriptionFormatOptions format, out string formattedString) { if (Description == null || Description.NativePropertyDescription == null) { // We cannot do anything without a property description formattedString = null; return(false); } IPropertyStore store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); using (PropVariant propVar = new PropVariant()) { store.GetValue(ref propertyKey, propVar); // Release the Propertystore Marshal.ReleaseComObject(store); store = null; HResult hr = Description.NativePropertyDescription.FormatForDisplay(propVar, ref format, out formattedString); // Sometimes, the value cannot be displayed properly, such as for blobs // or if we get argument exception if (!CoreErrorHelper.Succeeded(hr)) { formattedString = null; return(false); } return(true); } }
internal static IntPtr PidlFromParsingName(string name) { IntPtr pidl; ShellNativeMethods.ShellFileGetAttributesOptions sfgao; int retCode = ShellNativeMethods.SHParseDisplayName( name, IntPtr.Zero, out pidl, (ShellNativeMethods.ShellFileGetAttributesOptions) 0, out sfgao); return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero); }
/// <summary> /// Updates the native shell item that maps to this shell object. This is necessary when the shell item /// changes after the shell object has been created. Without this method call, the retrieval of properties will /// return stale data. /// </summary> /// <param name="bindContext">Bind context object</param> public void Update(IBindCtx bindContext) { HResult hr = HResult.Ok; if (NativeShellItem2 != null) { hr = NativeShellItem2.Update(bindContext); } if (CoreErrorHelper.Failed(hr)) { throw new Exception(hr.ToString()); } }
private void StorePropVariantValue(PropVariant propVar) { Guid guid = new Guid(ShellIIDGuid.IPropertyStore); IPropertyStore writablePropStore = null; try { int hr = ParentShellObject.NativeShellItem2.GetPropertyStore( ShellNativeMethods.GetPropertyStoreOptions.ReadWrite, ref guid, out writablePropStore); if (!CoreErrorHelper.Succeeded(hr)) { throw new Exception("Unable to get writable property store for this property.", Marshal.GetExceptionForHR(hr)); } HResult result = writablePropStore.SetValue(ref propertyKey, propVar); if (!AllowSetTruncatedValue && (int)result == ShellNativeMethods.InPlaceStringTruncated) { throw new ArgumentOutOfRangeException("propVar", "A value had to be truncated in a string or rounded if a numeric value. Set AllowTruncatedValue to true to prevent this exception."); } if (!CoreErrorHelper.Succeeded(result)) { throw new Exception("Unable to set property.", Marshal.GetExceptionForHR((int)result)); } writablePropStore.Commit(); } catch (InvalidComObjectException e) { throw new Exception("Unable to get writable property store for this property.", e); } catch (InvalidCastException) { throw new Exception("Unable to get writable property store for this property."); } finally { if (writablePropStore != null) { Marshal.ReleaseComObject(writablePropStore); writablePropStore = null; } } }
internal IShellProperty CreateTypedProperty(string canonicalName) { // Otherwise, call the native PropertyStore method PropertyKey propKey; int result = PropertySystemNativeMethods.PSGetPropertyKeyFromName(canonicalName, out propKey); if (!CoreErrorHelper.Succeeded(result)) { throw new ArgumentException( "The given CanonicalName is not valid.", Marshal.GetExceptionForHR(result)); } return(CreateTypedProperty(propKey)); }
internal static IPropertyStore CreateDefaultPropertyStore(ShellObject shellObj) { IPropertyStore nativePropertyStore = null; Guid guid = new Guid(ShellIIDGuid.IPropertyStore); int hr = shellObj.NativeShellItem2.GetPropertyStore( ShellNativeMethods.GetPropertyStoreOptions.BestEffort, ref guid, out nativePropertyStore); // throw on failure if (nativePropertyStore == null || !CoreErrorHelper.Succeeded(hr)) { throw new Exception(hr.ToString()); } return(nativePropertyStore); }
/// <summary> /// Gets the localized display string that describes the current sort order. /// </summary> /// <param name="descending"> /// Indicates the sort order should /// reference the string "Z on top"; otherwise, the sort order should reference the string "A on top". /// </param> /// <returns>The sort description for this property.</returns> /// <remarks> /// The string retrieved by this method is determined by flags set in the /// <c>sortDescription</c> attribute of the <c>labelInfo</c> element in the property's .propdesc file. /// </remarks> public string GetSortDescriptionLabel(bool descending) { IntPtr ptr = IntPtr.Zero; string label = string.Empty; if (NativePropertyDescription != null) { HResult hr = NativePropertyDescription.GetSortDescriptionLabel(descending, out ptr); if (CoreErrorHelper.Succeeded(hr) && ptr != IntPtr.Zero) { label = Marshal.PtrToStringUni(ptr); // Free the string Marshal.FreeCoTaskMem(ptr); } } return(label); }