Skip to content

Instantly share code, notes, and snippets.

View JaiganeshKumaran's full-sized avatar

Jaiganésh Kumaran JaiganeshKumaran

View GitHub Profile
@JaiganeshKumaran
JaiganeshKumaran / AsyncOperationWrapper.cpp
Created July 30, 2022 05:56
Create a fake async operation that returns a result immediately when invoked
```cpp
// Can be used in a similar way to .NET's Task.FromResult and PPL's concurrency::create_async (See https://github.com/microsoft/cppwinrt/issues/1036).
template <typename Result>
struct AsyncOperationWrapper : implements<AsyncOperationWrapper<Result>, Windows::Foundation::IAsyncOperation<Result>>
{
public:
AsyncOperationWrapper(Result result) : m_Result(result) {}
auto Id() const noexcept
{
@JaiganeshKumaran
JaiganeshKumaran / Disposable.h
Created July 30, 2022 01:57
Base class to conveniently implement the IClosable interface
#prgama once
#include <winerror.h>
#include <winrt/Windows.Foundation.h>
/// <summary>
/// Provides a base class to conveniently implement the IClosable interface.
/// </summary>
/// <remarks>Your derived class must provide a public or protected Dispose method which will be called by Close when the object hasn't been closed yet.</remarks>
template <typename Derived>
struct Disposable
@JaiganeshKumaran
JaiganeshKumaran / AppWindowHwnd.cpp
Last active October 30, 2022 10:05
Showcases how to get the handle (HWND) of an UWP app window
__interface __declspec(uuid("b74ea3bc-43c1-521f-9c75-e5c15054d78c")) IApplicationWindow_HwndInterop : IInspectable
{
// <summary>
// Gets the window handle of this AppWindow instance as a bit-blitted WindowId.
// </summary>
// <remarks>To get the actual ID, use [CoreAppWindowPreview::GetIdFromWindow](https://docs.microsoft.com/en-us/uwp/api/windows.ui.core.preview.coreappwindowpreview.getidfromwindow?view=winrt-22621). This method returns the handle (HWND) of the window.
HRESULT __stdcall get_WindowHandle(Windows::UI::WindowId* windowId) noexcept;
};
using IsWindow = int(__stdcall*)(HWND hwnd);
@JaiganeshKumaran
JaiganeshKumaran / StackWinRT.cpp
Last active October 30, 2022 10:05
Showcases how to create a stack allocated Windows Runtime object
#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: