Top Banner
Victor Ciura Technical Lead, Advanced Installer www.advancedinstaller.com November, 2018 These Aren't the COM Objects You're Looking For
107

These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

May 21, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

Victor Ciura Technical Lead, Advanced Installer www.advancedinstaller.com

November, 2018

These Aren't the COM Objects You're Looking For

Page 2: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�X

Abstract

Windows COM is 25 years old. Yet it is relevant today more than ever, because Microsoft has bet its entire modern WinRT API on it (starting with Windows 8/10). But, if you’re familiar with the “old” COM with its idioms and SDK helper classes, you’re in for a treat. With the advent of modern C++ 17, using COM objects and new Windows APIs in your applications feels like a completely new experience. In this session, we’ll explore how using modern C++ features can radically transform the shape of your COM code. By eliminating a lot of boilerplate, your code will be much more readable and maintainable. Classic COM idioms around activation and QueryInterface() can feel totally different with modern C++ helpers. A beautiful example of modern COM usage is C++/WinRT (now part of Windows SDK). This is a standard C++ language projection for the new Windows Runtime API. COM memory management, data marshalling, string handling can all feel quite mechanical in nature and very error prone, so a little help from modern C++ facilities would be more than welcomed. Error handling and debugging can be cumbersome for COM like APIs; we’ll explore some tricks to improve this experience, as well.

2018 Victor Ciura | @ciura_victor

Page 3: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�2

⚙ Part 1 of N

2018 Victor Ciura | @ciura_victor

These Aren't the COM Objects You're Looking For

Page 4: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�3

Why are we talking about this ?

Why COM ?

Have we really exhausted all the cool C++ template<> topics 😜 ?

2018 Victor Ciura | @ciura_victor

Page 5: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

Who Am I ?

@ciura_victor

�4

Advanced Installer Clang Power Tools

2018 Victor Ciura | @ciura_victor

Page 6: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�52018 Victor Ciura | @ciura_victor

🙋$

How many of you have done any COM programming

in the last 20 years ?

Page 7: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�62018 Victor Ciura | @ciura_victor

🙋$

How many of you are doing COM programming

currently (this year) ?

Page 8: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�72018 Victor Ciura | @ciura_victor

Let's start using COM...

CoInitializeEx()

Page 9: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�82018 Victor Ciura | @ciura_victor

initializes the COM library for use by the calling thread

sets the thread's concurrency model (COINIT_APARTMENTTHREADED, COINIT_MULTITHREADED)

and creates a new apartment for the thread (if one is required)

CoInitializeEx()

Need to initialize the COM on a thread before any COM API calls, otherwise the function will return CO_E_NOTINITIALIZED

Page 10: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�92018 Victor Ciura | @ciura_victor

CoInitializeEx() must be called once for each thread that uses COM

CoInitializeEx()

Multiple calls to CoInitializeEx() by the same thread are allowed,

but subsequent valid calls return S_FALSE

Close COM gracefully on a thread: each successful call to CoInitializeEx(), must be

balanced by a corresponding call to CoUninitialize()

Page 11: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�102018 Victor Ciura | @ciura_victor

“Do. Or do not. There is no try.” – Yoda

Page 12: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�112018 Victor Ciura | @ciura_victor

Any Trekkies in the room ?

Page 13: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�122018 Victor Ciura | @ciura_victor

Any Trekkies in the room ?

I'm sorry...

⚠ Some of this material may be unpleasant for you.

Page 14: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�132018 Victor Ciura | @ciura_victor

So, how can we fix COM init ?

CoInitializeEx()

Page 15: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�142018 Victor Ciura | @ciura_victor

RAII

Page 16: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�152018 Victor Ciura | @ciura_victor

class ComInitializerEx {public: ComInitializerEx(DWORD aComFlags) { HRESULT hRes = ::CoInitializeEx(nullptr, aComFlags); mClose = (hRes == S_OK);

if ((hRes != S_OK) && (hRes != S_FALSE)) ATLASSERT(SUCCEEDED(hRes)); }

~ComInitializerEx() { if (mClose) ::CoUninitialize(); }

private: bool mClose; // true if COM was initialized by this instance on this thread};

* some details omitted for brevity

Page 17: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�162018 Victor Ciura | @ciura_victor

Use it anywhere...

void FuncRequiringCom(){ ComInitializerEx com(COINIT_MULTITHREADED); ... // use of COM APIs }

class ClassRequiringCom{public: MethodsRequiringCom(); ...private: ComInitializerEx com_{ COINIT_MULTITHREADED };};

👈 you can nest these objects as needed

Page 18: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�172018 Victor Ciura | @ciura_victor

CoInitializerEx / RAII

You don't care about the thread you're on

You don't care if you're already initialized on this thread

You can nest these objects as needed

You don't worry about closing/unloading COM when you're done

Page 19: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�182018 Victor Ciura | @ciura_victor

Let's Talk About Strings

Page 20: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�192018 Victor Ciura | @ciura_victor

What is the COM string type ?

BSTR

Page 21: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�202018 Victor Ciura | @ciura_victor

BSTR

typedef WCHAR OLECHAR;typedef OLECHAR * BSTR;

A BSTR is a composite data type that consists of:

Length prefixA four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator.

Data string A string of Unicode characters. May contain multiple embedded null characters.

Terminator A NULL (0x0000) WCHAR

Page 22: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�212018 Victor Ciura | @ciura_victor

BSTR

typedef WCHAR OLECHAR;typedef OLECHAR * BSTR;

BSTR is a pointer

Length prefixA four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator.

Data string A string of Unicode characters. May contain multiple embedded null characters.

Terminator A NULL (0x0000) WCHAR

Page 23: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�222018 Victor Ciura | @ciura_victor

BSTR

BSTR str = L"My first COM string";

This code:

compiles

links

is incorrect (not working)

If (by accident) you pass a string literal as an argument to a COM function that

is expecting a BSTR, the COM function behaves unexpectedly. 🔥

Page 24: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�232018 Victor Ciura | @ciura_victor

BSTR

Allocating / Releasing Memory for a BSTR

BSTR str = ::SysAllocString(L"My first COM string");

🤦

if (str){ UseTheString(str); ... ::SysFreeString(str);}

Page 25: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�242018 Victor Ciura | @ciura_victor

“Somebody has to save our skins.” – Leia Organa

Page 26: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�252018 Victor Ciura | @ciura_victor

RAIIFTW !

Page 27: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�262018 Victor Ciura | @ciura_victor

Visual C++ Compiler COM Support

#include <comutil.h>

Compiler RAII Support Classes:

_bstr_t

_variant_t

_com_ptr_t

_com_error(HRESULT wrapper)

Page 28: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�272018 Victor Ciura | @ciura_victor

_bstr_t

encapsulates the BSTR data type

manages resource allocation through SysAllocString / SysFreeString and other APIs

uses reference counting to avoid excessive overhead (CoW)

provides various conversion constructors: const char*, const wchar_t*, BSTR, etc.

provides various operators: =, +, ==, !=, +=, <, >, char*, wchar_t*, etc.

Page 29: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�282018 Victor Ciura | @ciura_victor

_bstr_t

_bstr_t str(L"My first COM string");

ComApiWithBstrParam(str.GetBSTR());

std::wstring my_std_str = str;

Also:

_bstr_t other_str(my_std_str.c_str());

if (str == other_str) // lexicographical compare{ ... }

Page 30: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�292018 Victor Ciura | @ciura_victor

_com_ptr_t

_COM_SMARTPTR_TYPEDEF(IMyInterface, __uuidof(IMyInterface));

declares the _com_ptr_t specialization: IMyInterfacePtr

COM interface smart pointer

Page 31: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�302018 Victor Ciura | @ciura_victor

_com_ptr_t

_COM_SMARTPTR_TYPEDEF(ITaskbarList3, __uuidof(ITaskbarList3));

ITaskbarList3Ptr taskBar;

HRESULT hr = ::CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskBar));ATLASSERT(SUCCEEDED(hr));

if (taskBar) taskBar->SetProgressState(wnd, TBPF_INDETERMINATE);

Page 32: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�312018 Victor Ciura | @ciura_victor

Active Template Library ATL

🙋$

Page 33: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�322018 Victor Ciura | @ciura_victor

ATLA set of template-based C++ classes

intended to simplify COM programming 🤔

Page 34: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�332018 Victor Ciura | @ciura_victor

template<class Base>class CComObject : public Base

CComObject<CMyCircle> * pCircle = nullptr;HRESULT hRes = CComObject<CMyCircle>::CreateInstance(&pCircle);ATLASSERT(SUCCEEDED(hRes));

pCircle->AddRef();pCircle->SetRadius(5.2);

CComObject<>

Page 35: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�342018 Victor Ciura | @ciura_victor

CComObject<>class ATL_NO_VTABLE CMyCircle : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CMyCircle, &CLSID_CMyCircle>, public IDispatchImpl<IMyCircle, &IID_IMyCircle, &LIBID_NVC_ATL_COMLib, 1, 0>{public:

DECLARE_REGISTRY_RESOURCEID(IDR_MYCIRCLE)

DECLARE_NOT_AGGREGATABLE(CMyCircle)

BEGIN_COM_MAP(CMyCircle) COM_INTERFACE_ENTRY(IMyCircle) COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP()

DECLARE_PROTECT_FINAL_CONSTRUCT() ...

} BOILERPLATE

Page 36: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�352018 Victor Ciura | @ciura_victor

class ATL_NO_VTABLE CMyCircle : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CMyCircle, &CLSID_CMyCircle>, public IDispatchImpl<IMyCircle, &IID_IMyCircle, &LIBID_NVC_ATL_COMLib, 1, 0>{ ...

HRESULT FinalConstruct() { return S_OK; } void FinalRelease() { }

public: CMyCircle() { } STDMETHOD(SetRadius)(double val); ...};

CComObject<>

} Actual code we care about

Page 37: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�362018 Victor Ciura | @ciura_victor

“Your focus determines your reality.” – Qui-Gon Jinn

Page 38: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�372018 Victor Ciura | @ciura_victor

template<class Base>class CComObject : public Base

CComObject<>

Do you recognize this pattern ?

CRTP

Page 39: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�382018 Victor Ciura | @ciura_victor

template<class Base>class CComObject : public Base

CRTP

achieves a similar effect to the use of virtual functions

without the costs of dynamic polymorphism (no VTABLE)binding done at compile time

This pattern is used extensively in the Windows ATL and WTL libraries.

Page 40: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�392018 Victor Ciura | @ciura_victor

“Power! Unlimited power!” — Darth Sidious

Page 41: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�402018 Victor Ciura | @ciura_victor

ATL

<comutil.h> ATL

_com_ptr_t CComPtr<T>

_bstr_t CComBSTR

_variant_t CComVariant

Page 42: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�412018 Victor Ciura | @ciura_victor

CComBSTR

ATL wrapper for the BSTR data type

manages resource allocation through SysAllocString / SysFreeString and other APIs

supports move semantics &&

provides various conversion constructors: const char*, const wchar_t*, BSTR, etc.

provides various operators: =, +, ==, !=, +=, <, >, char*, wchar_t*, BSTR, etc.

Page 43: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�422018 Victor Ciura | @ciura_victor

CComBSTR str(L"My first COM string");

ComApiWithBstrParam(str); // operator BSTR()

std::wstring my_std_str = str;

Also:

CComBSTR other_str(my_std_str.c_str());

if (str == other_str) // lexicographical compare{ ... }

CComBSTR

Page 44: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�432018 Victor Ciura | @ciura_victor

“There’s always a bigger fish.” — Qui-Gon Jinn

Page 45: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�442018 Victor Ciura | @ciura_victor

ATL CString

supports both char and wchar_t through StringTraits policy

manages resource allocation & deallocation

uses reference counting to avoid excessive copy overhead (CoW)

50+ methods and operators

typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;

Page 46: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�452018 Victor Ciura | @ciura_victor

ATL CString

provides various conversion constructors: const char*, const wchar_t*, VARIANT, etc.

provides various operators: =, +, ==, !=, +=, <, >, char*, wchar_t*, etc.

provides tons of utility methods and string algorithms

typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;

Eg.

Find, FindOneOf, Format, MakeLower, MakeReverse, Left, Mid, Right,

Replace, ReverseFind, Tokenize, Trim, etc.

Page 47: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�462018 Victor Ciura | @ciura_victor

CAtlString str(L"My first COM string");

ComApiWithBstrParam(str.AllocSysString()); // allocates an OLE BSTR copystd::wstring my_std_str = str.GetString(); // get null-term C string

Also:

CAtlString other_str(my_std_str.c_str());

if (str == other_str) // lexicographical compare{ ... }

ATL CString

Page 48: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�472018 Victor Ciura | @ciura_victor

What about this modern COM I keep hearing about ?

Surely things must have improved in the last 25 years...

Page 49: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�482018 Victor Ciura | @ciura_victor

Windows Runtime (WinRT)

Windows 8 / 10

Page 50: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�492018 Victor Ciura | @ciura_victor

“This is a new day, a new beginning.” – Ahsoka Tano

Page 51: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�502018 Victor Ciura | @ciura_victor

What is the Windows Runtime ?

Modern class-based, object-oriented Windows API

Metadata about the classes/members

Language projections for natural / familiar use (C++, C#, JavaScript, etc.)

Page 52: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�512018 Victor Ciura | @ciura_victor

How do I access the Windows Runtime from C++ ?

Page 53: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�522018 Victor Ciura | @ciura_victor

Windows Runtime C++ Template Library WRL

enables you to more easily implement and consume COM components

adds little abstraction over the Windows Runtime ABI (very thin wrapper) gives you the ability to control the underlying code (low-level access)

error handling based on HRESULT

design inspired by ATL => can be mixed with existing older COM code

uses ISO standard C++

uses smart pointers & RAIIrather verbose (boilerplate)

supports UWP apps

Page 54: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�532018 Victor Ciura | @ciura_victor

Windows Runtime C++ Template Library C++/CX

uses non-standard C++ language extensions

terse syntax learning curve

high-level abstraction

represents HRESULT values as exceptions

automates housekeeping tasksdiscontinued...

Page 55: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�542018 Victor Ciura | @ciura_victor

Let's start using Windows Runtime...

Page 56: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�552018 Victor Ciura | @ciura_victor

Init...

#include <Windows.Foundation.h>#include <wrl/wrappers/corewrappers.h>#include <wrl/client.h>

using namespace ABI::Windows::Foundation;using namespace Microsoft::WRL;using namespace Microsoft::WRL::Wrappers;

WRL

Page 57: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�562018 Victor Ciura | @ciura_victor

Init...

RoInitializeWrapper init(RO_INIT_MULTITHREADED); if (FAILED(init)) { return PrintError(__LINE__, init); }

Microsoft::WRL::Wrappers::RoInitializeWrapper

RAII

WRL

Page 58: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�572018 Victor Ciura | @ciura_victor

Using a class

ABI::Windows::Foundation::IUriRuntimeClassFactory

// get the activation factory for IUriRuntimeClass interfaceComPtr<IUriRuntimeClassFactory> uriFactory;HRESULT hr = GetActivationFactory( HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);if (FAILED(hr)){ return PrintError(__LINE__, hr);}

WRL

Page 59: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�582018 Victor Ciura | @ciura_victor

What is the Windows Runtime string type ?

HSTRING

Page 60: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�592018 Victor Ciura | @ciura_victor

https://docs.microsoft.com/en-us/windows/desktop/WinRT/hstring

HSTRING

represents immutable string in the Windows Runtime(handle)

WindowsCreateString()

WindowsDeleteString()

WindowsDuplicateString()

WindowsConcatString()

Usage:

Page 61: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�602018 Victor Ciura | @ciura_victor

https://docs.microsoft.com/en-us/windows/desktop/WinRT/hstring

HSTRING

HRESULT WindowsCreateString( PCNZWCH sourceString, UINT32 length, HSTRING *string);

😱

HRESULT WindowsDuplicateString( HSTRING string, HSTRING *newString);

HRESULT WindowsConcatString( HSTRING string1, HSTRING string2, HSTRING *newString);

HRESULT WindowsDeleteString( HSTRING string);

Page 62: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�612018 Victor Ciura | @ciura_victor

Microsoft::WRL::Wrappers::HString is the HSTRING wrapper from WRL

HSTRING

HString str;hr = str.Set(L"Hello");if (FAILED(hr)){ return PrintError(__LINE__, hr);}

Page 63: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�622018 Victor Ciura | @ciura_victor

Platform::String is the language projection for C++/CX

HSTRING

Usage:

Platform::String ^ s = L"Hello";

bool String::operator+ (String ^ str1, String ^ str2);

bool String::operator== (String ^ str1, String ^ str2);

^

Page 64: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�632018 Victor Ciura | @ciura_victor

“You can’t stop the change, any more than you can stop the suns from setting.”

— Shmi Skywalker

Page 65: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�642018 Victor Ciura | @ciura_victor

What is C++/WinRT ?

an ISO standard C++17 language projection for the Windows Runtime

header-only library 

C++ class wrappers for WinRT APIsyou can author and consume Windows Runtime APIs

supersedes WRL and C++/CX

http://aka.ms/cppwinrt

Page 66: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�652018 Victor Ciura | @ciura_victor

What is C++/WinRT ?

https://github.com/Microsoft/cppwinrt

Windows SDK 10.0.17134.0 (Windows 10, version 1803)

http://aka.ms/cppwinrt

Page 67: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�662018 Victor Ciura | @ciura_victor

What is C++/WinRT ?

Project was started a few years back by Kenny Kerr.

If you want to learn more about the project & history,

checkout the links below:https://moderncpp.comhttp://cppcast.com/2015/05/kenny-kerr/

http://cppcast.com/2016/10/kenny-kerr/

https://kennykerr.ca/about/

Page 68: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�672018 Victor Ciura | @ciura_victor

What is C++/WinRT ?

https://www.youtube.com/watch?v=7TdpWB_vRZM

Page 69: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�682018 Victor Ciura | @ciura_victor

What is C++/WinRT ?

https://channel9.msdn.com/Events/Build/2018/BRK2425

Effective C++/WinRT for UWP and Win32 - Brent Rector, Kenny Kerr

Microsoft BUILD 2018

Page 70: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�692018 Victor Ciura | @ciura_victor

What is C++/WinRT ?How to get it Comes with Visual Studio 2017 (starting with v15.7)

✅ Select C++ workload

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt

Page 71: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�702018 Victor Ciura | @ciura_victor

https://marketplace.visualstudio.com/items?itemName=CppWinRTTeam.cppwinrt101804264

C++/WinRT Visual Studio extension

Debug visualization of C++/WinRT projected types (similar to C# debugging)

Project templates for getting started a C++/WinRT application MSBuild support for generating C++/WinRT projection headers and component skeletons

Page 72: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�712018 Victor Ciura | @ciura_victor

Let's start using Windows Runtime... (again)

Page 73: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�722018 Victor Ciura | @ciura_victor

Init...

#pragma comment(lib, "windowsapp")

#include <winrt/base.h>#include <winrt/Windows.Foundation.h>

using namespace winrt;using namespace Windows::Foundation;

winrt::init_apartment();

C++/WinRT

cl /std:c++17 /EHsc /W4 /WX /I"%WindowsSdkDir%Include\%UCRTVersion%\cppwinrt"

Page 74: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�732018 Victor Ciura | @ciura_victor

performs better and produces smaller binaries than any other language projection

outperforms handwritten code using the ABI interfaces directly (eg. WRL)

the underlying abstractions use modern C++ idioms, that the Visual C++ compiler

is designed to optimize (magic statics, empty base classes, strlen() elision)

C++/WinRT

Page 75: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�742018 Victor Ciura | @ciura_victor

What is the C++/WinRT string type ?

winrt::hstring

Page 76: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�752018 Victor Ciura | @ciura_victor

winrt::hstring

winrt::hstring str(L"Hello!");

Represents an immutable string consistent with the underlying HSTRING

Page 77: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�762018 Victor Ciura | @ciura_victor

winrt::hstring

Encapsulates HSTRING behind an interface similar to that of std::wstring

hstring() noexcept;

hstring(hstring const & h);

explicit hstring(std::wstring_view const & v);

hstring(wchar_t const * c);

hstring(wchar_t const * c, uint32_t s);

Page 78: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�772018 Victor Ciura | @ciura_victor

void Test(hstring const & theHstring, wstring_view const & theWstringView, wchar_t const * wideLiteral, wstring const & wideString){ hstring fromDefault{};

hstring fromHstring{ theHstring };

hstring fromWstringView{ theWstringView };

hstring fromWideLiteral{ wideLiteral }; hstring fromWideString{ wideString.c_str() };

hstring fromWideLiteralWithSize{ wideLiteral, 256 }; hstring fromWideStringWithSize{ wideString.c_str(), 256 };}

Page 79: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�782018 Victor Ciura | @ciura_victor

winrt::hstring

Uri uri{ L"http://example.com" };

// uses hstring's conversion operator to std::wstring_viewstd::wstring domain { uri.Domain() };

hstring Uri::Domain() const;

operator std::wstring_view() const noexcept;

Page 80: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�792018 Victor Ciura | @ciura_victor

winrt::hstring

hstring theHstring;for (const auto & element : theHstring){ std::wcout << element;}

An hstring is a range, so you can use it with range-based for, or with std::for_each

Page 81: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�802018 Victor Ciura | @ciura_victor

winrt::hstring

winrt::hstring w{ L"Hello!" };

std::string c = winrt::to_string(w);WINRT_ASSERT(c == "Hello!");

w = winrt::to_hstring(c);WINRT_ASSERT(w == L"Hello!");

hstring is UTF16,but it plays nice with UTF-8 text

Page 82: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�812018 Victor Ciura

ATL CString

Platform::String

QString

wxString

const char*

const wchar_t*

XString

folly::fbstring

MFC CString

std::string

WTF::StringWTF::CString

winrt::hstring

BSTR

_bstr_t

CComBSTR

Page 83: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�822018 Victor Ciura | @ciura_victor

“I’m just a simple man trying to make my way in the universe.”

— Jango Fett

Page 84: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�832018 Victor Ciura

const char*XString std::string🔡 const char[N]

So we ended up with something like this...

↔ ↔

+ glue code

Page 85: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�842018 Victor Ciura

const char*XString std::string🔡 const char[N]

↔ ↔

⚙ String Algorithms

Page 86: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�852018 Victor Ciura | @ciura_victor

Enough string_view to hang ourselves

www.youtube.com/watch?v=xwP4YCP_0q0

CppCon 2018

I have a whole talk just on C++17 std::string_view

Page 87: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�862018 Victor Ciura

Is C++17 string_view the answer to all our string problems ?

Page 88: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�872018 Victor Ciura

A lightweight string-like view into an array of characters.

It can be constructed from a const char* (null terminated) or from a pointer and a length.

Intended to be used as glue code, to avoid large overload sets.

std::string_view

Page 89: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�882018 Victor Ciura

A string_view does not manage the storage that it refers to.

Lifetime management is up to the user (caller).

std::string_view

Page 90: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�892018 Victor Ciura

Convenience Conversions (and Gotchas)

• const char * automatically converts to std::string via constructor (not explicit)

• const char * automatically converts to std::string_view via constructor (not explicit)

• std::string automatically converts to std::string_view via conversion operator

• can construct a std::string from a std::string_view via constructor (explicit)

Page 91: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�902018 Victor Ciura

Design goal: avoid temporary std::string objects.

std::string_view was designed to interoperate with std::string 😈

Caveat: comes with some usage complexity (gotchas).

std::string_view

Page 92: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�912018 Victor Ciura | @ciura_victor

“It’s a trap!” – Admiral Ackbar

Page 93: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�922018 Victor Ciura | @ciura_victor

On COM...

Windows COM is 25 years old... and it shows this in many corners.

Yet it is relevant today more than ever, because Microsoft has bet its entire modern WinRT API on it.

With the advent of C++17, using COM objects and new WinRT APIs feels like a completely new experience.

Page 94: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�932018 Victor Ciura | @ciura_victor

“The dark side of the COM is a pathway to many abilities some

consider to be unnatural.”

— Chancellor Palpatine

Page 95: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�942018 Victor Ciura | @ciura_victor

One more thing...

Page 96: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�952018 Victor Ciura | @ciura_victor

xlang

NEW

Page 97: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�962018 Victor Ciura | @ciura_victor

xlang

cross-language cross-compiler cross-platform

generalization of WinRT

https://kennykerr.ca/2018/10/10/xlang/

Page 98: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�972018 Victor Ciura | @ciura_victor

application ( language B )

library ( language A )

Operating System ( Windows 7-10, Linux, MacOS, iOS )

xlang

language projection

API

Page 99: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�982018 Victor Ciura | @ciura_victor

xlang

open-source

https://github.com/Microsoft/xlang

WIP

experimental

Page 100: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�992018 Victor Ciura | @ciura_victor

xlang

https://github.com/Microsoft/xlang

a solid metadata reader ( ISO C++17 )

incredibly fast and portable abstraction over the ECMA-335 format (WinRT)

Goal: to reach feature parity with C++/WinRT

Page 101: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�1002018 Victor Ciura | @ciura_victor

What xlang project is NOT

https://github.com/Microsoft/xlang

xlang is not a port of the Windows Runtime, COM, DCOM

xlang will not port the Windows Runtime APIs

Page 102: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�1012018 Victor Ciura | @ciura_victor

“Great, kid. Don’t get cocky.” – Han Solo

Page 103: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�X

C++ Slack is your friend

CppLang Slack auto-invite: https://cpplang.now.sh/

https://cpplang.slack.com

2018 Victor Ciura | @ciura_victor

Page 104: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�X

Rob Irving @robwirving

Jason Turner @lefticus

2018 Victor Ciura | @ciura_victor

Page 105: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�X

Jon Kalb @_JonKalb

Phil Nash @phil_nash

http://cpp.chat

https://www.youtube.com/channel/UCsefcSZGxO9lTBqFbsV3sJg/

https://overcast.fm/itunes1378325120/cpp-chat

2018 Victor Ciura | @ciura_victor

Page 106: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

Victor Ciura Technical Lead, Advanced Installer www.advancedinstaller.com

November, 2018

These Aren't the COM Objects You're Looking For

@ciura_victor

Page 107: These Aren't the COM Objects You're Looking For Aren't the COM Obj… · These Aren't the COM Objects You're Looking For. X Abstract Windows COM is 25 years old. ... But, if you’re

�X

Questions🗣@ciura_victor

2018 Victor Ciura | @ciura_victor