using System.Runtime.InteropServices.ComTypes;
namespace Vanara.PInvoke;
public static partial class Shell32
{
/// Exposes methods for operations with a file association dialog box or menu.
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-iassochandler
[ComImport, Guid("F04061AC-1659-4a3f-A954-775AA57FC083"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAssocHandler
{
/// Retrieves the full path and file name of the executable file associated with the file type.
///
/// Type: LPWSTR*
///
/// When this method returns, contains the address of a pointer to a null-terminated, Unicode string that contains the full path
/// of the file, including the file name.
///
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-getname HRESULT GetName(
// LPWSTR *ppsz );
[PreserveSig]
HRESULT GetName([MarshalAs(UnmanagedType.LPWStr)] out string ppsz);
/// Retrieves the display name of an application.
///
/// Type: LPWSTR*
///
/// When this method returns, contains the address of a pointer to a null-terminated, Unicode string that contains the display
/// name of the application.
///
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-getuiname HRESULT GetUIName(
// LPWSTR *ppsz );
[PreserveSig]
HRESULT GetUIName([MarshalAs(UnmanagedType.LPWStr)] out string ppsz);
/// Retrieves the location of the icon associated with the application.
///
/// Type: LPWSTR*
///
/// When this method returns, contains the address of a pointer to a null-terminated, Unicode string that contains the path to
/// the application's icon.
///
///
///
/// Type: int*
/// When this method returns, contains a pointer to the index of the icon within the resource named in ppszPath.
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
/// If the icon cannot be found, the function will return the path to the executable, and an icon index of zero.
///
/// For performance reasons, an application may use the Shell image cache to retrieve the icon, rather than loading the icon
/// directly from the path returned. The path and icon index can be passed directly to Shell_GetCachedImageIndex. One benefit of
/// this is that the Shell cache can provide a default icon in the event that no icon was available for the application.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-geticonlocation HRESULT
// GetIconLocation( LPWSTR *ppszPath, int *pIndex );
[PreserveSig]
HRESULT GetIconLocation([MarshalAs(UnmanagedType.LPWStr)] out string ppszPath, out int pIndex);
/// Indicates whether the application is registered as a recommended handler for the queried file type.
///
/// Type: HRESULT
/// Returns S_OK if the program is recommended; otherwise, S_FALSE.
///
///
///
/// Applications that register themselves as handlers for particular file types can specify whether they are recommended
/// handlers. This has no effect on the actual behavior of the applications when launched. It is simply provided as a hint to
/// the user and a value that the UI can utilize programmatically, if desired. For example, the Shell's Open With dialog
/// separates entries into Recommended Programs and Other Programs.
///
///
/// Note that program recommendations may change over time. One example is provided when the user chooses an application from
/// the Other Programs of the Open With dialog to open a particular file type. That may cause the Shell to
/// "promote" that application to recommended status for that file type. Because the recommended status may change over time,
/// applications should not cache this value, but query it each time it is needed.
///
///
/// If SHAssocEnumHandlers was called with the ASSOC_FILTER_RECOMMENDED flag, then only recommended handlers are returned. If
/// the ASSOC_FILTER_NONE flag was used, then you must call IAssocHandler::IsRecommended on each IAssocHandler object to
/// determine whether it is recommended or not.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-isrecommended HRESULT IsRecommended();
[PreserveSig]
HRESULT IsRecommended();
/// Sets an application as the default application for this file type.
///
/// Type: LPCWSTR
/// A pointer to a null-terminated, Unicode string that contains the display name of the application.
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-makedefault HRESULT
// MakeDefault( LPCWSTR pszDescription );
[PreserveSig]
HRESULT MakeDefault([MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
/// Directly invokes the associated handler.
///
/// Type: IDataObject*
///
/// A pointer to an IDataObject that represents the selected item on which to invoke the handler. Note that you should not call
/// IAssocHandler::Invoke with a selection of multiple items. If you have multiple items, call
/// IAssocHandler::CreateInvoker instead. See Remarks for more details.
///
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
///
/// IAssocHandler objects are typically used to populate an Open With menu. When one of those menu items is selected,
/// this method is called to launch the chosen application.
///
/// Invoke and CreateInvoker
///
/// The IDataObject used by these methods can represent either a single file or a selection of multiple files. Not all
/// applications support the multiple file option. The applications that do support that scenario might impose other
/// restrictions, such as the number of files that can be opened simultaneously, or the acceptable combination of file types.
///
///
/// Therefore, an application often must determine whether the handler supports the selection before trying to invoke the
/// handler. For example, an application might enable a menu item only if it has verified that the selection in question was
/// supported by that handler.
///
///
/// It is generally safe to assume that an application will support invocation on a single item, and in those cases the
/// application typically calls IAssocHandler::Invoke based on that assumption.
///
///
/// For multiple selection scenarios, however, the application should call IAssocHandler::CreateInvoker. That method retrieves
/// an IAssocHandlerInvoker object that allows the calling application to first check whether the selection is supported
/// (SupportsSelection), then to invoke the handler (Invoke).
///
///
/// IAssocHandler::Invoke can be called on a selection of multiple files, but it is not recommended because of the large
/// processing load involved and no guarantee that it will succeed.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-invoke HRESULT Invoke(
// IDataObject *pdo );
[PreserveSig]
HRESULT Invoke([In] IDataObject pdo);
///
/// Retrieves an object that enables the invocation of the associated handler on the current selection. The invoker includes the
/// ability to verify whether the current selection is supported.
///
///
/// Type: IDataObject*
///
/// A pointer to an IDataObject that represents the selected item or items on which to invoke the handler. Note that if you have
/// only a single item, IAssocHandler::Invoke could be the better choice. See Remarks for more details.
///
///
///
/// Type: IAssocHandlerInvoker**
///
/// When this method returns, contains the address of a pointer to an IAssocHandlerInvoker object. This object is used to invoke
/// the menu item after ensuring that the selected items are supported by the associated handler.
///
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
///
/// IAssocHandler objects are typically used to populate an Open With menu. When one of those menu items is selected,
/// this method is called to launch the chosen application.
///
/// Invoke and CreateInvoker
///
/// The IDataObject used by these methods can represent either a single file or it may represent a selection of multiple files.
/// Not all applications support the multiple files option. Those applications that do support that scenario might impose other
/// restrictions such as the number of files that can be opened at once, or acceptable combinations of file types.
///
///
/// Therefore, an application often must determine whether the handler supports the selection before trying to invoke the
/// handler. For example, an application might enable a menu item only if it knew that the selection in question was supported
/// by that handler.
///
///
/// It is generally safe to assume that an application will support invocation on a single item; in those cases the application
/// typically calls IAssocHandler::Invoke.
///
///
/// For multiple selection scenarios, the application should call IAssocHandler::CreateInvoker. That method retrieves an
/// IAssocHandlerInvoker object that allows the calling application to first check whether the selection is supported
/// (SupportsSelection), then to invoke the handler (Invoke).
///
///
/// IAssocHandler::Invoke can be called on a selection of multiple files, but it is not recommended due to the large processing
/// load involved and no guarantee of success.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandler-createinvoker HRESULT
// CreateInvoker( IDataObject *pdo, IAssocHandlerInvoker **ppInvoker );
[PreserveSig]
HRESULT CreateInvoker([In] IDataObject pdo, out IAssocHandlerInvoker ppInvoker);
}
/// Exposes methods that invoke an associated application handler.
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-iassochandlerinvoker
[PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IAssocHandlerInvoker")]
[ComImport, Guid("92218CAB-ECAA-4335-8133-807FD234C2EE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAssocHandlerInvoker
{
/// Determines whether an invoker supports its selection.
///
/// Type: HRESULT
/// Returns S_OK if this instance supports its selection, or S_FALSE otherwise.
///
///
/// For example, this method should return whether an application (as selected from an "Open With" context menu) can Open
/// a file.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandlerinvoker-supportsselection
// HRESULT SupportsSelection();
[PreserveSig]
HRESULT SupportsSelection();
/// Invokes an associated application handler.
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
/// There is no guarantee that a given association handler will support a particular selection, especially if multiple items are
/// selected. Before attempting to invoke the selection via this method, it is recommended to call IAssocHandlerInvoker::SupportsSelection.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-iassochandlerinvoker-invoke HRESULT Invoke();
[PreserveSig]
HRESULT Invoke();
}
/// Exposes a method that allows enumeration of a collection of handlers associated with particular file name extensions.
/// SHAssocEnumHandlers is the usual method of creating an IEnumAssocHandlers pointer.
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ienumassochandlers
[PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IEnumAssocHandlers")]
[ComImport, Guid("973810ae-9599-4b88-9e4d-6ee98c9552da"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IEnumAssocHandlers
{
/// Retrieves a specified number of elements.
///
/// Type: ULONG
/// The number of elements to retrieve.
///
///
/// Type: IAssocHandler**
///
/// When this method returns, contains the address of an array of IAssocHandler pointers. Each IAssocHandler represents a
/// single handler.
///
///
///
/// Type: ULONG*
/// When this method returns, contains a pointer to the number of elements retrieved.
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ienumassochandlers-next HRESULT Next( ULONG
// celt, IAssocHandler **rgelt, ULONG *pceltFetched );
[PreserveSig]
HRESULT Next(uint celt, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface, SizeParamIndex = 0)] IAssocHandler[] rgelt, out uint pceltFetched);
}
}