Skip to content

Instantly share code, notes, and snippets.

View kennykerr's full-sized avatar

Kenny Kerr kennykerr

View GitHub Profile
#include <windows.h>
#include <pathcch.h>
#include <string>
#pragma comment(lib, "pathcch")
namespace impl
{
void check(HRESULT const result)
{
#pragma comment(lib, "windowsapp")
#include "winrt/Windows.Networking.Connectivity.h"
using namespace winrt;
using namespace Windows::Networking;
using namespace Windows::Networking::Connectivity;
int main()
{
initialize();
@kennykerr
kennykerr / IMemoryBufferByteAccess.cpp
Last active September 13, 2022 04:54
How to use IMemoryBufferByteAccess with C++/WinRT
#include "winrt/Windows.Foundation.h"
struct __declspec(uuid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")) __declspec(novtable) IMemoryBufferByteAccess : ::IUnknown
{
virtual HRESULT __stdcall GetBuffer(uint8_t** value, uint32_t* capacity) = 0;
};
using namespace winrt;
using namespace Windows::Foundation;
@kennykerr
kennykerr / SyncTools.cpp
Last active June 5, 2021 09:10
Curious how SyncTools (https://kennykerr.ca/2013/01/04/synctools-for-sysinternals/) was implemented? Code circa 2012
#include"Precompiled.h"
static char const Constant_LinkExpression[] = ">([^<]+)<A HREF=\"([^\"]+)\">";
static char const Constant_DefaultUrl[] = "http://live.sysinternals.com/";
static char const Constant_DefaultIgnore[] = "*.sys;*.html;*.cnt;*.scr;*.hlp;*.txt;*.asp;*.aspx";
static char const Constant_Banner[] = "SyncTools for Sysinternals. Copyright (c) 2013 Kenny Kerr\n\n";
static char const Constant_SyncStatus[] = ".SyncStatus";
static char const Constant_SyncIgnore[] = ".SyncIgnore";
static UINT const Constant_SyncStatusFormat = 2;
@kennykerr
kennykerr / observable.cpp
Last active January 22, 2023 12:04
How to implement IObservableVector<T> with C++/WinRT
using namespace Windows::Foundation::Collections;
template <typename T>
struct observable_vector : implements<observable_vector<T>,
IObservableVector<T>,
IVector<T>,
IVectorView<T>,
IIterable<T>>
{
event_token VectorChanged(VectorChangedEventHandler<T> const& handler)
@kennykerr
kennykerr / casting.cpp
Created April 18, 2017 15:49
How to cast between C++/WinRT and C++/CX
template <typename T>
T^ to_cx(winrt::Windows::Foundation::IUnknown const& from)
{
return safe_cast<T^>(reinterpret_cast<Platform::Object^>(winrt::get_abi(from)));
}
template <typename T>
T from_cx(Platform::Object^ from)
{
T to{ nullptr };
@kennykerr
kennykerr / buffer.cpp
Last active January 17, 2022 15:13
How to implement a custom Windows::Storage buffer with C++/WinRT
struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da")) IBufferByteAccess : ::IUnknown
{
virtual HRESULT __stdcall Buffer(uint8_t** value) = 0;
};
struct CustomBuffer : implements<CustomBuffer, IBuffer, IBufferByteAccess>
{
std::vector<uint8_t> m_buffer;
uint32_t m_length{};
@kennykerr
kennykerr / direct3d11.interop.h
Created October 7, 2017 17:25
Alternative to the Windows SDK's <windows.graphics.directx.direct3d11.interop.h> that works well with C++/WinRT.
#include <winrt/windows.graphics.directx.direct3d11.h>
extern "C"
{
HRESULT __stdcall CreateDirect3D11DeviceFromDXGIDevice(::IDXGIDevice* dxgiDevice,
::IInspectable** graphicsDevice);
HRESULT __stdcall CreateDirect3D11SurfaceFromDXGISurface(::IDXGISurface* dgxiSurface,
::IInspectable** graphicsSurface);
}
@kennykerr
kennykerr / Events.idl
Last active November 30, 2017 14:31
How to define a custom delegate for an event in IDL
runtimeclass Events;
[version(1.0), uuid(da38922c-3d3a-404f-8477-de9c104c4f34)]
delegate HRESULT CustomDelegate([in] int value);
[version(1.0), uuid(6ac35d2c-b2cf-4c36-995c-a9c0764636c9), exclusiveto(Events)]
interface IEvents : IInspectable
{
[eventadd] HRESULT CustomEvent([in] CustomDelegate* handler, [out][retval] EventRegistrationToken* cookie);
[eventremove] HRESULT CustomEvent([in] EventRegistrationToken cookie);
@kennykerr
kennykerr / resume_foreground.h
Last active January 5, 2018 14:46
A complement to C++/WinRT's resume_background coroutine helper
#include <winrt/Windows.UI.Core.h>
namespace winrt
{
struct resume_foreground
{
explicit resume_foreground(Windows::UI::Core::CoreDispatcher&& dispatcher, Windows::UI::Core::CoreDispatcherPriority const priority = Windows::UI::Core::CoreDispatcherPriority::Normal) :
m_dispatcher(std::move(dispatcher)),
m_priority(priority)
{