Last active
October 30, 2022 10:05
-
-
Save jaigak/661bde391ce491078b75fb928739eee1 to your computer and use it in GitHub Desktop.
Showcases how to create a stack allocated Windows Runtime object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <Windows.h> | |
#include <winstring.h> | |
#include <Windows.Foundation.h> | |
#pragma comment(lib, "OneCoreUAP.lib") | |
struct StackStringable final : ABI::Windows::Foundation::IStringable | |
{ | |
public: | |
ULONG __stdcall AddRef() | |
{ | |
return InterlockedIncrement(&m_ReferenceCount); | |
} | |
ULONG __stdcall Release() | |
{ | |
return InterlockedDecrement(&m_ReferenceCount); | |
} | |
HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject) | |
{ | |
if (riid == __uuidof(IUnknown)) | |
{ | |
*ppvObject = static_cast<IUnknown*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else if (riid == __uuidof(IInspectable)) | |
{ | |
*ppvObject = static_cast<IInspectable*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else if (riid == __uuidof(ABI::Windows::Foundation::IStringable)) | |
{ | |
*ppvObject = static_cast<ABI::Windows::Foundation::IStringable*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else return E_NOINTERFACE; | |
} | |
HRESULT __stdcall GetIids(ULONG* iidCount, IID** iids) | |
{ | |
auto array = static_cast<IID*>(CoTaskMemAlloc(sizeof(IID))); | |
if (array == nullptr) return E_OUTOFMEMORY; | |
else | |
{ | |
array[0] = __uuidof(ABI::Windows::Foundation::IStringable); | |
*iidCount = 1; | |
*iids = array; | |
return S_OK; | |
} | |
} | |
HRESULT __stdcall GetRuntimeClassName(HSTRING* className) | |
{ | |
return WindowsCreateString(InterfaceName_Windows_Foundation_IStringable, _countof(InterfaceName_Windows_Foundation_IStringable), className); | |
} | |
HRESULT __stdcall GetTrustLevel(TrustLevel* trustLevel) | |
{ | |
*trustLevel = TrustLevel::FullTrust; | |
return S_OK; | |
} | |
HRESULT __stdcall ToString(HSTRING* value) | |
{ | |
return WindowsCreateString(L"This message is from StackStringable.", 37, value); | |
} | |
private: | |
ULONG m_ReferenceCount = 1; | |
}; | |
struct HeapStringable final : ABI::Windows::Foundation::IStringable | |
{ | |
public: | |
ULONG __stdcall AddRef() | |
{ | |
return InterlockedIncrement(&m_ReferenceCount); | |
} | |
ULONG __stdcall Release() | |
{ | |
auto count = InterlockedDecrement(&m_ReferenceCount); | |
if (count == 0) delete this; | |
return count; | |
} | |
HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject) | |
{ | |
if (riid == __uuidof(IUnknown)) | |
{ | |
*ppvObject = static_cast<IUnknown*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else if (riid == __uuidof(IInspectable)) | |
{ | |
*ppvObject = static_cast<IInspectable*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else if (riid == __uuidof(ABI::Windows::Foundation::IStringable)) | |
{ | |
*ppvObject = static_cast<ABI::Windows::Foundation::IStringable*>(this); | |
AddRef(); | |
return S_OK; | |
} | |
else return E_NOINTERFACE; | |
} | |
HRESULT __stdcall GetIids(ULONG* iidCount, IID** iids) | |
{ | |
auto array = static_cast<IID*>(CoTaskMemAlloc(sizeof(IID))); | |
if (array == nullptr) return E_OUTOFMEMORY; | |
else | |
{ | |
array[0] = __uuidof(ABI::Windows::Foundation::IStringable); | |
*iidCount = 1; | |
*iids = array; | |
return S_OK; | |
} | |
} | |
HRESULT __stdcall GetRuntimeClassName(HSTRING* className) | |
{ | |
return WindowsCreateString(InterfaceName_Windows_Foundation_IStringable, _countof(InterfaceName_Windows_Foundation_IStringable), className); | |
} | |
HRESULT __stdcall GetTrustLevel(TrustLevel* trustLevel) | |
{ | |
*trustLevel = TrustLevel::FullTrust; | |
return S_OK; | |
} | |
HRESULT __stdcall ToString(HSTRING* value) | |
{ | |
return WindowsCreateString(L"This message is from HeapStringable.", 36, value); | |
} | |
private: | |
ULONG m_ReferenceCount = 1; | |
}; | |
void PrintStringable(ABI::Windows::Foundation::IStringable* stringable) | |
{ | |
HSTRING text; | |
stringable->ToString(&text); | |
std::wcout << WindowsGetStringRawBuffer(text, nullptr) << L"\n"; | |
WindowsDeleteString(text); | |
} | |
int main() | |
{ | |
StackStringable stackStringable; | |
PrintStringable(&stackStringable); | |
stackStringable->Release(); | |
auto heapStringable = new HeapStringable(); | |
PrintStringable(heapStringable); | |
heapStringable->Release(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment