2024-06-17 12:46:32 +08:00
|
|
|
#pragma once
|
|
|
|
#include "YYCCInternal.hpp"
|
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "WinImportPrefix.hpp"
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <shlobj_core.h>
|
|
|
|
#include "WinImportSuffix.hpp"
|
|
|
|
|
2024-07-25 11:22:50 +08:00
|
|
|
/**
|
|
|
|
* @brief Windows COM related types and checker.
|
|
|
|
* @details
|
|
|
|
* This namespace is Windows specific.
|
|
|
|
* In other platforms, this whole namespace will be unavailable.
|
|
|
|
*
|
|
|
|
* See also \ref com_helper.
|
|
|
|
*/
|
2024-06-17 12:46:32 +08:00
|
|
|
namespace YYCC::COMHelper {
|
|
|
|
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief C++ standard deleter for every COM interfaces inheriting IUnknown.
|
2024-06-17 12:46:32 +08:00
|
|
|
class ComPtrDeleter {
|
|
|
|
public:
|
|
|
|
ComPtrDeleter() {}
|
|
|
|
void operator() (IUnknown* com_ptr) {
|
|
|
|
if (com_ptr != nullptr) {
|
|
|
|
com_ptr->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief Smart unique pointer of \c IFileDialog
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartIFileDialog = std::unique_ptr<IFileDialog, ComPtrDeleter>;
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief Smart unique pointer of \c IFileOpenDialog
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartIFileOpenDialog = std::unique_ptr<IFileOpenDialog, ComPtrDeleter>;
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief Smart unique pointer of \c IShellItem
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartIShellItem = std::unique_ptr<IShellItem, ComPtrDeleter>;
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief Smart unique pointer of \c IShellItemArray
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartIShellItemArray = std::unique_ptr<IShellItemArray, ComPtrDeleter>;
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief Smart unique pointer of \c IShellFolder
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartIShellFolder = std::unique_ptr<IShellFolder, ComPtrDeleter>;
|
|
|
|
|
2024-07-25 11:22:50 +08:00
|
|
|
/// @brief C++ standard deleter for almost raw pointer used in COM which need to be free by CoTaskMemFree()
|
2024-06-17 12:46:32 +08:00
|
|
|
class CoTaskMemDeleter {
|
|
|
|
public:
|
|
|
|
CoTaskMemDeleter() {}
|
|
|
|
void operator() (void* com_ptr) {
|
|
|
|
if (com_ptr != nullptr) {
|
|
|
|
CoTaskMemFree(com_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-07-25 11:22:50 +08:00
|
|
|
|
|
|
|
/// @brief Smart unique pointer of COM created \c WCHAR sequence.
|
2024-06-17 12:46:32 +08:00
|
|
|
using SmartLPWSTR = std::unique_ptr<std::remove_pointer_t<LPWSTR>, CoTaskMemDeleter>;
|
|
|
|
|
2024-06-20 10:16:13 +08:00
|
|
|
/**
|
|
|
|
* @brief Check whether COM environment has been initialized.
|
|
|
|
* @return True if it is, otherwise false.
|
|
|
|
* @remarks
|
|
|
|
* This function will call corresponding function of COM Guard.
|
|
|
|
* Do not remove this function and you must preserve at least one reference to this function in final program.
|
|
|
|
* Some compiler will try to drop COM Guard in final program if no reference to it and it will cause the initialization of COM environment failed.
|
|
|
|
* This is the reason why I order you do the things said above.
|
|
|
|
*/
|
|
|
|
bool IsInitialized();
|
|
|
|
|
2024-06-17 12:46:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|