From efbc6c5c0a01e0782f320d4a53b77df7c46be5fb Mon Sep 17 00:00:00 2001 From: MouriNaruto Date: Sun, 8 Sep 2024 15:52:10 +0800 Subject: [PATCH] Continue to remove C++/WinRT dependency for NanaZip.Frieren. --- .../NanaZip.Frieren.RuntimeObjectWrapper.cpp | 49 +++++++++++++++---- NanaZip.Frieren/NanaZip.Frieren.vcxproj | 1 + 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/NanaZip.Frieren/NanaZip.Frieren.RuntimeObjectWrapper.cpp b/NanaZip.Frieren/NanaZip.Frieren.RuntimeObjectWrapper.cpp index 421597b0c..66c845203 100644 --- a/NanaZip.Frieren/NanaZip.Frieren.RuntimeObjectWrapper.cpp +++ b/NanaZip.Frieren/NanaZip.Frieren.RuntimeObjectWrapper.cpp @@ -15,7 +15,29 @@ #include -#include +#include +#include + +// WINDOWS_RUNTIME_HSTRING_FLAGS + +#define WRHF_NONE 0x00000000 +#define WRHF_STRING_REFERENCE 0x00000001 +#define WRHF_VALID_UNICODE_FORMAT_INFO 0x00000002 +#define WRHF_WELL_FORMED_UNICODE 0x00000004 +#define WRHF_HAS_EMBEDDED_NULLS 0x00000008 +#define WRHF_EMBEDDED_NULLS_COMPUTED 0x00000010 +#define WRHF_RESERVED_FOR_PREALLOCATED_STRING_BUFFER 0x80000000 + +typedef struct _HSTRING_HEADER_INTERNAL +{ + UINT32 Flags; + UINT32 Length; + UINT32 Padding1; + UINT32 Padding2; + PCWSTR StringRef; +} HSTRING_HEADER_INTERNAL; + +#include namespace { @@ -45,7 +67,7 @@ namespace return CachedResult; } - struct ErrorInfoFallback : public winrt::implements< + struct ErrorInfoFallback : public Mile::ComObject< ErrorInfoFallback, IErrorInfo, IRestrictedErrorInfo> @@ -53,19 +75,27 @@ namespace private: HRESULT m_Code; - winrt::hstring m_Message; + BSTR m_Message; public: ErrorInfoFallback( _In_ HRESULT Code, - _In_ HSTRING Message) : + _In_opt_ HSTRING Message) : m_Code(Code), - m_Message(*reinterpret_cast(&Message)) + m_Message(::SysAllocString(Message + ? reinterpret_cast( + this->m_Message)->StringRef + : nullptr)) { } + ~ErrorInfoFallback() + { + ::SysFreeString(this->m_Message); + } + HRESULT STDMETHODCALLTYPE GetGUID( _Out_ GUID* pGUID) { @@ -83,7 +113,7 @@ namespace HRESULT STDMETHODCALLTYPE GetDescription( _Out_ BSTR* pBstrDescription) { - *pBstrDescription = ::SysAllocString(this->m_Message.c_str()); + *pBstrDescription = ::SysAllocString(this->m_Message); return *pBstrDescription ? S_OK : E_OUTOFMEMORY; } @@ -110,7 +140,7 @@ namespace *description = nullptr; *error = this->m_Code; *capabilitySid = nullptr; - *restrictedDescription = ::SysAllocString(this->m_Message.c_str()); + *restrictedDescription = ::SysAllocString(this->m_Message); return *restrictedDescription ? S_OK : E_OUTOFMEMORY; } @@ -155,9 +185,8 @@ EXTERN_C BOOL WINAPI RoOriginateLanguageException( return ProcAddress(error, message, languageException); } - winrt::com_ptr ErrorInfo = - winrt::make(error, message); - ::SetErrorInfo(0, ErrorInfo.get()); + IErrorInfo* ErrorInfo = new ErrorInfoFallback(error, message); + ::SetErrorInfo(0, ErrorInfo); return TRUE; } diff --git a/NanaZip.Frieren/NanaZip.Frieren.vcxproj b/NanaZip.Frieren/NanaZip.Frieren.vcxproj index 1a03a9bed..d19db6f2c 100644 --- a/NanaZip.Frieren/NanaZip.Frieren.vcxproj +++ b/NanaZip.Frieren/NanaZip.Frieren.vcxproj @@ -4,6 +4,7 @@ {4E78AF72-D7EF-4769-B64B-95E7EDF05FA2} NanaZip.Frieren StaticLibrary + true