diff --git a/PInvoke/Shell32/ShObjIdl.IKnownFolder.cs b/PInvoke/Shell32/ShObjIdl.IKnownFolder.cs index 71108f2e..85e7832e 100644 --- a/PInvoke/Shell32/ShObjIdl.IKnownFolder.cs +++ b/PInvoke/Shell32/ShObjIdl.IKnownFolder.cs @@ -727,6 +727,22 @@ namespace Vanara.PInvoke FOLDERID_Windows, } + /// Frees the allocated fields in the result from IKnownFolder::GetFolderDefinition. + /// + /// Type: KNOWNFOLDER_DEFINITION* + /// A pointer to a KNOWNFOLDER_DEFINITION structure that contains information about the given known folder. + /// + /// This function does not return a value. + /// This is an inline helper function that calls CoTaskMemFree on the fields in the structure that need to be freed. Its implementation can be seen in the header file. + // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-freeknownfolderdefinitionfields + // void FreeKnownFolderDefinitionFields( KNOWNFOLDER_DEFINITION *pKFD ); + [PInvokeData("shobjidl_core.h", MSDNShortId = "0ad17dd3-e612-403a-b8c3-e93d5f259c1f")] + public static void FreeKnownFolderDefinitionFields(in KNOWNFOLDER_DEFINITION pKFD) + { + foreach (var fi in pKFD.GetType().GetFields().Where(f => f.FieldType == typeof(StrPtrUni))) + Marshal.FreeCoTaskMem((IntPtr)fi.GetValue(pKFD)); + } + /// /// Exposes methods that allow an application to retrieve information about a known folder's category, type, GUID, pointer to an item /// identifier list (PIDL) value, redirection capabilities, and definition. It provides a method for the retrieval of a known folder's @@ -975,13 +991,13 @@ namespace Vanara.PInvoke /// settings. This name is meant to be a unique, human-readable name. Third parties are recommended to follow the format /// Company.Application.Name. The name given here should not be confused with the display name. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszName; + public StrPtrUni pszName; /// /// A pointer to a short description of the known folder, stored as a null-terminated Unicode string. This description should /// include the folder's purpose and usage. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszDescription; + public StrPtrUni pszDescription; /// /// A KNOWNFOLDERID value that names another known folder to serve as the parent folder. Applies to common and per-user folders @@ -995,13 +1011,13 @@ namespace Vanara.PInvoke /// refers to the physical file system path, and is not localized. Applies to common and per-user folders only. See Remarks for /// more details. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszRelativePath; + public StrPtrUni pszRelativePath; /// /// A pointer to the Shell namespace folder path of the folder, stored as a null-terminated Unicode string. Applies to virtual /// folders only. For example, Control Panel has a parsing name of ::%CLSID_MyComputer%\::%CLSID_ControlPanel%. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszParsingName; + public StrPtrUni pszParsingName; /// /// Optional. A pointer to the default tooltip resource used for this known folder when it is created. This is a null-terminated @@ -1013,7 +1029,7 @@ namespace Vanara.PInvoke /// /// This information is not required for virtual folders. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszTooltip; + public StrPtrUni pszTooltip; /// /// Optional. A pointer to the default localized name resource used when the folder is created. This is a null-terminated Unicode @@ -1025,7 +1041,7 @@ namespace Vanara.PInvoke /// /// This information is not required for virtual folders. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszLocalizedName; + public StrPtrUni pszLocalizedName; /// /// Optional. A pointer to the default icon resource used when the folder is created. This is a null-terminated Unicode string in @@ -1037,7 +1053,7 @@ namespace Vanara.PInvoke /// /// This information is not required for virtual folders. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszIcon; + public StrPtrUni pszIcon; /// /// Optional. A pointer to a Security Descriptor Definition Language format string. This is a null-terminated Unicode string that @@ -1045,7 +1061,7 @@ namespace Vanara.PInvoke /// folder inherits the security descriptor of its parent. This is particularly useful for common folders that are accessed by /// all users. /// - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CoTaskMemStringMarshaler))] public string pszSecurity; + public StrPtrUni pszSecurity; /// /// Optional. Default file system attributes given to the folder when it is created. For example, the file could be hidden and diff --git a/PInvoke/Shell32/ShTypes.cs b/PInvoke/Shell32/ShTypes.cs index 88a545ed..fc97693c 100644 --- a/PInvoke/Shell32/ShTypes.cs +++ b/PInvoke/Shell32/ShTypes.cs @@ -134,27 +134,27 @@ namespace Vanara.PInvoke public struct STRRET { /// A value that specifies the desired format of the string. - [FieldOffset(0)] public STRRET_TYPE uType; + [FieldOffset(0)] + public STRRET_TYPE uType; /// /// A pointer to the string. This memory must be allocated with CoTaskMemAlloc. It is the calling application's responsibility to /// free this memory with CoTaskMemFree when it is no longer needed. /// - [FieldOffset(4), MarshalAs(UnmanagedType.BStr)] + [FieldOffset(4)] public StrPtrUni pOleStr; // must be freed by caller of GetDisplayNameOf /// The offset into the item identifier list. - [FieldOffset(4)] public uint uOffset; // Offset into SHITEMID + [FieldOffset(4)] + public uint uOffset; // Offset into SHITEMID /// The buffer to receive the display name. CHAR[MAX_PATH] - [FieldOffset(4), MarshalAs(UnmanagedType.LPStr, SizeConst = Kernel32.MAX_PATH)] - public string cStr; // Buffer to fill in (ANSI) + [FieldOffset(4)] + public StrPtrAnsi cStr; /// Returns a that represents this instance. /// A that represents this instance. - public override string ToString() => uType == STRRET_TYPE.STRRET_CSTR - ? cStr - : (uType == STRRET_TYPE.STRRET_WSTR ? pOleStr.ToString() : string.Empty); + public override string ToString() => (uType == STRRET_TYPE.STRRET_CSTR ? cStr : (uType == STRRET_TYPE.STRRET_WSTR ? pOleStr : (string)null)) ?? string.Empty; } internal class STRRETMarshaler : ICustomMarshaler diff --git a/PInvoke/Shell32/Thumbcache.cs b/PInvoke/Shell32/Thumbcache.cs index 197e0898..87318991 100644 --- a/PInvoke/Shell32/Thumbcache.cs +++ b/PInvoke/Shell32/Thumbcache.cs @@ -53,7 +53,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/desktop/api/thumbcache/nn-thumbcache-ithumbnailprovider [PInvokeData("thumbcache.h", MSDNShortId = "55c4739a-4835-4f53-a435-804ddf06ffcf")] - [ComImport, Guid("e357fccd-a995-4576-b01f-234630154e96"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + [ComImport, Guid("e357fccd-a995-4576-b01f-234630154e96"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), CoClass(typeof(PhotoThumbnailProvider))] public interface IThumbnailProvider { /// Gets a thumbnail image and alpha type. @@ -79,26 +79,6 @@ namespace Vanara.PInvoke /// An implementation of IThumbnailProvider for photo thumbnails is supplied in Microsoft Windows as CLSID_PhotoThumbnailProvider. [ComImport, Guid("C7657C4A-9F68-40fa-A4DF-96BC08EB3551"), ClassInterface(ClassInterfaceType.None)] [PInvokeData("thumbcache.h")] - public class PhotoThumbnailProvider : IThumbnailProvider - { - /// Gets a thumbnail image and alpha type. - /// - /// The maximum thumbnail size, in pixels. The Shell draws the returned bitmap at this size or smaller. The returned bitmap - /// should fit into a square of width and height cx, though it does not need to be a square image. The Shell scales the bitmap to - /// render at lower sizes. For example, if the image has a 6:4 aspect ratio, then the returned bitmap should also have a 6:4 - /// aspect ratio. - /// - /// - /// When this method returns, contains a pointer to the thumbnail image handle. The image must be a DIB section and 32 bits per - /// pixel. The Shell scales down the bitmap if its width or height is larger than the size specified by cx. The Shell always - /// respects the aspect ratio and never scales a bitmap larger than its original size. - /// - /// - /// When this method returns, contains a pointer to one of the following values from the WTS_ALPHATYPE enumeration. - /// - /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. - [PreserveSig] - public extern HRESULT GetThumbnail(uint cx, out HBITMAP phbmp, out WTS_ALPHATYPE pdwAlpha); - } + public class PhotoThumbnailProvider { } } } \ No newline at end of file