Files
wfassoc/wfassoc-cdylib/cbinding/wfassoc.h
T

574 lines
21 KiB
C++

/**
* @file wfassoc.h
* @brief Windows File Association C API header
*
* This header provides a C-compatible API for managing Windows file associations,
* including schema creation, program registration, and extension management.
* The API is designed to at least work with both C99 and C++17 compilers.
*/
#pragma once
#ifndef WFASSOC_H_
#define WFASSOC_H_
#ifdef __cplusplus
#include <cstddef>
#include <cstdint>
#else // __cplusplus
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#endif // __cplusplus
#ifdef __cplusplus
/**
* @brief Windows File Association C/C++ API namespace
*
* This namespace only provide the raw binding to C-compatible wfassoc API in C++.
* For the C++ RAII wrappers of this, please see @ref wfassocpp namespace.
*/
namespace wfassoc {
#endif // __cplusplus
#ifdef __cplusplus
/** Type representing a null-terminated UTF-8 C-style string */
using WFStrPtr = const char*;
/**
* @brief Type representing an error code returned by FFI functions
*
* WFERROR_OK is the only value representing absolute success.
* Any other value's meaning is decided by this library,
* currently WFERROR_ERR represents a generic failure.
*/
using WFError = uint32_t;
/**
* @brief Type representing a handle/token for managed objects
*
* This library use object pool to manage any objects created during calling.
* And we expose this type as an opaque handle for visiting your created object.
*/
using WFToken = uint64_t;
/**
* @brief Type representing an icon handle (opaque pointer)
*
* This type is equivalent with Win32 HICON type.
*/
using WFHICON = void*;
#else // __cplusplus
typedef const char *WFStrPtr;
typedef uint32_t WFError;
typedef uint64_t WFToken;
typedef void *WFHICON;
#endif // __cplusplus
#ifdef __cplusplus
/** The error code representing absolute success. */
constexpr WFError WFERROR_OK = 0u;
/** The error code representing a generic failure. */
constexpr WFError WFERROR_ERR = 1u;
/** Invalid token value used as a sentinel for no object */
constexpr WFToken WF_INVALID_TOKEN = 0u;
/** Invalid icon handle value */
constexpr WFHICON WF_INVALID_HICON = nullptr;
/** Invalid index value used for error conditions */
constexpr size_t WF_INVALID_INDEX = static_cast<size_t>(-1);
#else // __cplusplus
static const WFError WFERROR_OK = 0u;
static const WFError WFERROR_ERR = 1u;
static const WFToken WF_INVALID_TOKEN = 0u;
static const WFHICON WF_INVALID_HICON = NULL;
static const size_t WF_INVALID_INDEX = ((size_t)-1);
#endif // __cplusplus
#ifdef __cplusplus
/**
* @brief Registration scope for file associations
*
* Determines whether a program is registered for the current user or system-wide.
*/
enum class WFScope : uint32_t {
/** Current user scope */
User = 0u,
/** System-wide scope */
System = 1u
};
/**
* @brief View mode for querying file association status
*
* Determines how the association status is viewed/queried.
*/
enum class WFView : uint32_t {
/** User-level view */
User = 0u,
/** System-level view */
System = 1u,
/** Combined hybrid view of both user and system */
Hybrid = 2u
};
#else // __cplusplus
typedef uint32_t WFScope;
/** Current user scope */
static const WFScope WF_SCOPE_USER = 0u;
/** System-wide scope */
static const WFScope WF_SCOPE_SYSTEM = 1u;
typedef uint32_t WFView;
/** User-level view */
static const WFView WF_VIEW_USER = 0u;
/** System-level view */
static const WFView WF_VIEW_SYSTEM = 1u;
/** Combined hybrid view of both user and system */
static const WFView WF_VIEW_HYBRID = 2u;
#endif // __cplusplus
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* @brief Get the last error message
*
* Returns a human-readable error message describing the last error that occurred.
* The returned error message string is valid until the next API call.
*
* For most functions located in this library, except some special function indicated in their notes,
* they return WFERROR_OK when the function is successful, and WFERROR_ERR when the function is failed.
* Once they fail, you can call this function to get a human-readable error message.
*
* The string this function return use different buffer with function return string value.
* So you don't worry about that calling this function may invalidate function return string value.
*
* @return Null-terminated UTF-8 string containing the error message.
* If no error has occurred, the string is empty.
* There is no possibility of a NULL return value.
*/
WFStrPtr WFGetLastError(void);
/**
* @brief Check if the current process has administrative privileges
*
* @param[out] out_has Pointer to receive whether the current process has administrative privileges.
* @return WFERROR_OK on success, WFERROR_ERR on failure.
*/
WFError WFHasPrivilege(bool *out_has);
/**
* @brief Create a new Schema object
*
* A Schema is a sketchpad of a complete program.
* For the user of this library, they should create a Schema object first.
* Then convert it to a Program object for following operations.
*
* @param[out] out_schema Pointer to receive the Schema token.
* The receiver take the ownership of this Schema object.
* And it should be freed by calling WFSchemaDestroy() when it is no longer needed,
* or consumed by creating a Program object via WFProgramCreate().
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaCreate(WFToken *out_schema);
/**
* @brief Destroy a Schema object
*
* Releases resources associated with the Schema object.
*
* Usually you do not need to call this function,
* because the convertion function from Schema to Program will consume given Schema object to produce Program object.
*
* @param[in] in_schema Schema token to destroy
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaDestroy(WFToken in_schema);
/**
* @brief Set the program identifier for a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the identifier.
* This identifier should not be empty, must start with alphabet character,
* and follow with alphabet characters, digits, underline, or hyphens.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetIdentifier(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Set the program path for a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the program path.
* This path should be the fully qualified path to the application.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetPath(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Set the program CLSID for a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the CLSID.
* This CLSID string should be in the format of @c {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} .
* Please note that curly braces are required.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetClsid(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Set the program name for a Schema (optional)
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the name, or NULL to clear
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetName(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Set the program icon for a Schema (optional)
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the icon path, or NULL to clear
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetIcon(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Set the program behavior for a Schema (optional)
*
* @param[in] in_schema Schema token
* @param[in] in_value Null-terminated UTF-8 string containing the behavior command, or NULL to clear
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaSetBehavior(WFToken in_schema, WFStrPtr in_value);
/**
* @brief Add a string resource entry to a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_name Null-terminated UTF-8 string containing the name of this entry
* @param[in] in_value Null-terminated UTF-8 string containing the value of this entry.
* It can be a plain string or a reference string to resource.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaAddStr(WFToken in_schema, WFStrPtr in_name, WFStrPtr in_value);
/**
* @brief Add an icon registry entry to a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_name Null-terminated UTF-8 string containing the name of this entry
* @param[in] in_value Null-terminated UTF-8 string containing the value of this entry.
* It can be a path to icon or a reference string to resource.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaAddIcon(WFToken in_schema, WFStrPtr in_name, WFStrPtr in_value);
/**
* @brief Add a behavior registry entry to a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_name Null-terminated UTF-8 string containing the name of this entry
* @param[in] in_value Null-terminated UTF-8 string containing the value of this entry.
* It should be a valid command line string which use \c %1, \c %2, etc. to represent parameters.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaAddBehavior(WFToken in_schema, WFStrPtr in_name, WFStrPtr in_value);
/**
* @brief Add a file extension to a Schema
*
* @param[in] in_schema Schema token
* @param[in] in_ext Null-terminated UTF-8 string containing the file extension name (without leading dot).
* @param[in] in_ext_name Null-terminated UTF-8 string containing the name pointing to associated name for this extension.
* This name should be registered by calling WFSchemaAddStr().
* @param[in] in_ext_icon Null-terminated UTF-8 string containing the name pointing to associated icon for this extension.
* This name should be registered by calling WFSchemaAddIcon().
* @param[in] in_ext_behavior Null-terminated UTF-8 string containing the name pointing to associated behavior for this extension.
* This name should be registered by calling WFSchemaAddBehavior().
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSchemaAddExt(WFToken in_schema,
WFStrPtr in_ext,
WFStrPtr in_ext_name,
WFStrPtr in_ext_icon,
WFStrPtr in_ext_behavior);
/**
* @brief Create a Program object from a Schema
*
* Please note that this function will consume the Schema object.
* It means that the Schema object cannot be used after this call.
* And you do not need to call WFSchemaDestroy() for this Schema object after this call.
*
* Please note that the given Schema object will always be consumed,
* no matter this function return success or failure.
*
* @param[in] in_schema Schema token (will be consumed)
* @param[out] out_program Pointer to receive the Program token.
* The receiver take the ownership of this Program object.
* And it should be freed by calling WFProgramDestroy() when it is no longer needed.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramCreate(WFToken in_schema, WFToken *out_program);
/**
* @brief Destroy a Program object
*
* Releases resources associated with the Program.
*
* @param[in] in_program Program token to destroy
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramDestroy(WFToken in_program);
/**
* @brief Resolve the provided program name of this Program
*
* The name will be user specified first,
* then fallback to program manifest file specified name,
* and finally fallback to the file name of executable.
*
* @param[in] in_program Program token
* @param[out] out_name Pointer to receive the resolved name.
* There is no possibility that this value is NULL.
* This string will be freed at the next API call. Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramResolveName(WFToken in_program, WFStrPtr *out_name);
/**
* @brief Resolve the Program icon resource
*
* The icon will be user specified first,
* the fallback to the first icon of program,
* and finally fallback to the system default executable icon.
*
* @param[in] in_program Program token
* @param[out] out_icon_rc Pointer to receive the icon resource token.
* The caller take the ownership of created icon resource object.
* And it should be freed by calling WFIconRcDestroy() when it is no longer needed.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramResolveIcon(WFToken in_program, WFToken *out_icon_rc);
/**
* @brief Get the number of file extensions in the Program
*
* @param[in] in_program Program token
* @param[out] out_len Pointer to receive the number of extensions
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramExtsLen(WFToken in_program, size_t *out_len);
/**
* @brief Find a file extension by its body (extension string)
*
* @param[in] in_program Program token
* @param[in] in_body Null-terminated UTF-8 string containing the file extension name (without leading dot) to find.
* @param[out] out_index Pointer to receive the file extension index, or WF_INVALID_INDEX if not found.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramFindExt(WFToken in_program, WFStrPtr in_body, size_t *out_index);
/**
* @brief Resolve this program provided extension's details by index
*
* @param[in] in_program Program token
* @param[in] in_index Index of the extension to resolve
* @param[out] out_self_ext_status Pointer to receive the self extension status token.
* The caller take the ownership of created self extension status object.
* And it should be freed by calling WFSelfExtStatusDestroy() when it is no longer needed.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramResolveExt(WFToken in_program, size_t in_index, WFToken *out_self_ext_status);
/**
* @brief Register the Program in the specified scope
*
* @param[in] in_program Program token
* @param[in] in_scope Registration scope
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramRegister(WFToken in_program, WFScope in_scope);
/**
* @brief Unregister the Program from the specified scope
*
* @param[in] in_program Program token
* @param[in] in_scope Registration scope
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramUnregister(WFToken in_program, WFScope in_scope);
/**
* @brief Check if the Program is registered in the specified scope
*
* @param[in] in_program Program token
* @param[in] in_scope Registration scope for checking
* @param[out] out_is_registered Pointer to receive the registration status.
* True if the Program is registered in the specified scope, false otherwise.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramIsRegistered(WFToken in_program, WFScope in_scope, bool *out_is_registered);
/**
* @brief Link a file extension in the specified scope
*
* @param[in] in_program Program token
* @param[in] in_scope Registration scope
* @param[in] in_index Index of the extension to link
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramLinkExt(WFToken in_program, WFScope in_scope, size_t in_index);
/**
* @brief Unlink a file extension in the specified scope
*
* @param[in] in_program Program token
* @param[in] in_scope Registration scope
* @param[in] in_index Index of the extension to unlink
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramUnlinkExt(WFToken in_program, WFScope in_scope, size_t in_index);
/**
* @brief Query the status of a file extension
*
* @param[in] in_program Program token
* @param[in] in_view View viewpoint.
* @param[in] in_index Index of the extension to query
* @param[out] out_ext_status Pointer to receive the extension status token, or WF_INVALID_TOKEN if not found.
* If the extension is not found, it usually means that this extension is not registered in the specified scope.
* The caller take the ownership of created extension status object.
* And it should be freed by calling WFExtStatusDestroy() when it is no longer needed.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFProgramQueryExt(WFToken in_program, WFView in_view, size_t in_index, WFToken *out_ext_status);
/**
* @brief Destroy an extension status object
*
* @param[in] in_ext_status Extension status token to destroy
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFExtStatusDestroy(WFToken in_ext_status);
/**
* @brief Get the display name from an extension status object
*
* The display will be user specified first,
* the fallback to its ProgId verbatim.
*
* @param[in] in_ext_status Extension status token
* @param[out] out_name Pointer to receive the name.
* There is no possibility that this value is NULL.
* We will try to use localized name first, then use raw ProgId name if localized name is not available.
* This string will be freed at the next API call. Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFExtStatusGetName(WFToken in_ext_status, WFStrPtr *out_name);
/**
* @brief Get the icon from an extension status object
*
* The icon will be user specified first,
* the fallback to the system default file icon.
*
* @param[in] in_ext_status Extension status token
* @param[out] out_icon Pointer to receive the icon handle.
* This icon handle will be freed once this icon resource object is destroyed.
* Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFExtStatusGetIcon(WFToken in_ext_status, WFHICON *out_icon);
/**
* @brief Destroy a self extension status object
*
* @param[in] in_self_ext_status Self extension status token to destroy
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSelfExtStatusDestroy(WFToken in_self_ext_status);
/**
* @brief Get the display name from a self extension status object
*
* The display will be user specified first,
* the fallback to its ProgId verbatim.
*
* @param[in] in_self_ext_status Self extension status token
* @param[out] out_name Pointer to receive the name string.
* There is no possibility that this value is NULL.
* This string will be freed at the next API call. Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSelfExtStatusGetName(WFToken in_self_ext_status, WFStrPtr *out_name);
/**
* @brief Get the icon from a self extension status object
*
* The icon will be user specified first,
* the fallback to the system default file icon.
*
* @param[in] in_self_ext_status Self extension status token
* @param[out] out_icon Pointer to receive the icon handle.
* This icon handle will be freed once this self extension status object is destroyed.
* Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSelfExtStatusGetIcon(WFToken in_self_ext_status, WFHICON *out_icon);
/**
* @brief Get the extension string (without leading dot) from a self extension status object
*
* @param[in] in_self_ext_status Self extension status token
* @param[out] out_inner Pointer to receive the file extension name (without leading dot).
* There is no possibility that this value is NULL.
* This string will be freed at the next API call. Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSelfExtStatusGetExt(WFToken in_self_ext_status, WFStrPtr *out_inner);
/**
* @brief Get the dotted extension string (with leading dot) from a self extension status object
*
* @param[in] in_self_ext_status Self extension status token
* @param[out] out_inner Pointer to receive the file extension string (with leading dot).
* There is no possibility that this value is NULL.
* This string will be freed at the next API call. Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFSelfExtStatusGetDottedExt(WFToken in_self_ext_status, WFStrPtr *out_inner);
/**
* @brief Destroy an icon resource object
*
* @param[in] in_icon_rc Icon resource token to destroy
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFIconRcDestroy(WFToken in_icon_rc);
/**
* @brief Get the icon handle from an icon resource object
*
* @param[in] in_icon_rc Icon resource token
* @param[out] out_icon Pointer to receive the icon handle.
* There is no possibility that this value is WF_INVALID_HICON.
* This icon handle will be freed once this icon resource object is destroyed.
* Please make a copy immediately if you need to use it longer.
* @return WFERROR_OK on success, WFERROR_ERR on failure
*/
WFError WFIconRcGetIcon(WFToken in_icon_rc, WFHICON *out_icon);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#ifdef __cplusplus
} // namespace wfassoc
#endif // __cplusplus
#endif // WFASSOC_H_