230 likes | 656 Views
WRL. Sridhar Madhugiri, Software Design Engineer Lukasz Chodorski, Software Design Engineer Microsoft Corporation. Agenda. WRL Overview. WRL Classes. Consuming WinRT. Project template. Demo. WRL Overview. WRL. Template based implementation. Consume and Author WinRT objects.
E N D
WRL Sridhar Madhugiri, Software Design Engineer Lukasz Chodorski, Software Design Engineer Microsoft Corporation
Agenda WRL Overview WRL Classes Consuming WinRT Project template Demo
WRL Template based implementation Consume and Author WinRT objects Exception Free Works at ABI level Developed specifically for WinRT
Why use WRL Porting COM components to Metro No exceptions requirement Interop with Com No C++ extensions requirement Fine grained control
Winrt Concepts WinRT Object Reference counted Implements IInspectable Instantiated through factory WeakReference support Delegate Callback Interface with method Delegate instance is WinRT object Event Delegate used as Event callback Server advertises events Client creates delegate and registers it to receive events Misc Reference count management Asynchronous operations
WRL Classes WinRT Object RuntimeClass InspectableClass ActivatableClass ActivationFactory Delegate Callback Event EventSource Callback Misc ComPtr, WeakPtr AsyncBase And many more
ComPtr, HString ComPtr<IActivationFactory>activationFactory; HRESULThr = GetActivationFactory( HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(), &activationFactory ); // Error handling ComPtr<IUriRuntimeClassFactory> uriActivationFactory; hr = activationFactory.As(&uriActivationFactory); // Error handling ComPtr<IUriRuntimeClass> uri; hr = uriActivationFactory->CreateUri( HString::MakeReference(L"http://www.microsoft.com").Get(), &uri ); // Error handling Wrappers::HStringdomainName; hr = uri->get_Domain(domainName.GetAddressOf()); // Error handling
Delegate ComPtr<IThreadPoolTimerStatics> timerFactory; HRESULThr = GetActivationFactory( Wrappers::HString::MakeReference(RuntimeClass_Windows_System_Threading_ThreadPoolTimer).Get(), &timerFactory ); // Error handling TimeSpan delay; delay.Duration = 200; ComPtr<IThreadPoolTimer> timer; hr = timerFactory->CreateTimer( Callback<ITimerElapsedHandler>( [](IThreadPoolTimer* timer) -> HRESULT { // Perform some action in separate thread returnS_OK; } ).Get(), delay, &timer );
Project template Available in Online template gallery Select Online/Templates/C++/WRL Class Library in New project dialog Does not work with Beta Template generated files Project file with correct settings IDL file with one interface and one runtimeclass Source files that implement the interface and runtime class Broiler plate code to implement standard exports
demo Default WRL project
Project files Project settings Preprocessor Define WINAPI_FAMILY=WINAPI_PARTITION_APP MIDL setting to generate WinMD Custom build step for mdmerge IDL namespace runtimeclass, interface exclusiveto, activatable Module.cpp Broilerplate code Wires exports to WRL activation infrastructure WRLClassLibrary1.cpp RuntimeClass Template parameters - interfaces for Object Implements- AddRef, Release, QI, GetIids WeakReference support InspectableClass Implements – GetRuntimeclassName, GetTrustLevel ActivatableClass Associates default Activation factory with class Wires factory to WRL activation infrastructure
demo Add method
Add Method IDL Add method to interface CPP Implement method in class __stdcall important
demo Add event
Add Event IDL Delegate definition – keyword delegate MIDL creates an interface with method that matches delegate signature Add eventadd and eventremove methods to interface Name of both methods should be same Fixed signature CPP EventSourcetemplated on delegate interface as member of class Add methods corresponding to eventadd and eventremove methods in interface Delegate represented by its interface name eventadd function has add_ prefix and eventremove has remove_ prefix In the implementation of add/remove call Add/Remove on EventSource Invoke event by calling InvokeAll on EventSource
demo Add non default constructor
Add Custom Constructor IDL Define constructor factory interface Add one or more constructor methods, exclusiveto attribute Out/retval parameter type has to match type specified with exclusiveto Specify factory interface as parameter to activatable attribute on runtimeclass CPP Implement RuntimeClassInitialize • Two phase construction • Default constructor cannot fail • Parameters same as the ones on constructor interface method Add factory class • Template on Constructor interface • Implement constructor interface methods • MakeAndInitialize • instantiates class & calls RuntimeClassInitialize Use ActivitableClassWithFactory • Similar to ActivitableClass
Conclusion Fairly easy to use Exceptions cannot cross ABI boundary Tedious
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.