This document discusses creating Windows Runtime components using managed languages like C# and Visual Basic. It covers the basics of Windows Runtime components, rules for creating them in .NET, and how to implement asynchronous methods, events, and exceptions. While components enable cross-language use, there is a performance cost to using the CLR. The document recommends considering alternatives like class libraries for performance-critical code only used in Windows Store apps.
The Future of Software Development - Devin AI Innovative Approach.pdf
Creating and Consuming Windows Runtime Components
1. Creating Windows Runtime Components
Mirco Vanini
WinRT Components
Microsoft® MVP Windows Embedded
2. Agenda
Libraries for Windows Store Apps
Recap - WinRT Internal
Projection
Windows Runtime Components via C#
Understand the Cost
When to Create Managed Windows
Runtime Components
Q&A
Links
Contatti
3. Libraries for Windows Store Apps
Class Library (Windows Store Apps)
.NET library with classes and methods that are
available for Windows Store apps.
Portable Class Library
Only a subset of .NET library is available
(depends on target platform).
Windows Runtime Component
Consumed in any language that supports
WinRT
4. WinRT – architecture
Windows
Metadata &
Namespace
Language Projection
Windows Core
Windows Runtime Core
XAML Storage …Network
UI Pickers MediaControls
Metro style app
Runtime
Broker
Language
Support
(CLR, WinJS,
CRT)
Web Host
(HTML, CSS,
JavaScript))
5. WinRT - object
COM Based !
o IUnknown
• AddRef
• Release
• QueryInterface
o IDispatch
o IInspectable
• GetIids
• GetRuntimeClassName
• GetTrustLevel
o Windows Metadata (WinMD) - ECMA-335
Object
6. Windows Runtime APIs
Available to all programming languages
Requires a language neutral type
system
Windows
Runtime
JavaScript
C#/VBC++
7. APIs in multiple languages
Many languages use PascalCasing for names
of types, methods, properties, and events
JavaScript has well established naming
conventions
o “Types” are PascalCased
o Methods and Properties are camelCased
o Events are all lowercase
Windows Runtime uses PascalCasing for types
and members
Windows Runtime maps JavaScript
methods, properties and event names to its
conventions
9. Windows Runtime types - Strings
Immutable or mutable?
o Immutable – JavaScript, .NET; Mutable – C++
Null
o JavaScript: null is an object, string is a type
o C++: std::string has no 'null' semantics
o .NET System.String: reference type has a 'null'
distinguished value
As a result…
o Windows Runtime: string's immutable, no null
representation
10. Windows Runtime types - References
Target languages handle pointers and
references differently
o C++: All types can be passed by value or by
reference
o .NET: Objects are passed by reference, value types
by value
o JavaScript: Objects passed by reference, numbers
passed by value
o Windows Runtime: Objects (Interfaces) passed by
reference, all other types passed by value
As a result…
o Windows Runtime: method parameters are [in] or
[out], never [in, out]
13. Runtime Callable Wrapper
The common language runtime exposes COM
objects through a proxy called the runtime
callable wrapper (RCW).
The runtime creates exactly one RCW for each
COM object, regardless of the number of
references that exist on that object.
The runtime maintains a single RCW per
process for each object
The standard wrapper enforces
built-in marshaling rules. For
example, when a .NET client passes
a String type as part of an argument
to an unmanaged object, the
wrapper converts the string to a
BSTR type. Should the COM object
return a BSTR to its managed
caller, the caller receives a String
14. Windows Runtime Components via C#/VB
With the .NET Framework 4.5, you can use
managed code to create your own Windows
Runtime types, packaged in a Windows
Runtime component.
You can use your component in Windows Store
apps with C++, JavaScript, Visual Basic, or C#
15. Traditional COM in Managed Code
ComVisible(true), Guid("06D7901C-9045-4241-B8A0-39A1AC0F8618")]
public interface IWindowsApiSample
{
string HelloWorld();
}
[ComVisible(true), [ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IWindowsApiSample))]
public class WindowsApiSample : IWindowsApiSample
{
public string HelloWorld() {
return "Hello, World!";
}
}
16. Windows Runtime in managed code
Sample
public sealed class MyClassLibrary
{
public string HelloWorld()
{
return "Hello, World!";
}
}
17. Windows Runtime Components Rules
Public classes must be sealed.
All public types must have a root namespace
that matches the assembly name.
The fields, parameters, and return values of all
the public types and members in your
component must be Windows Runtime types.
A public class or interface cannot:
o Be generic.
o Implement an interface that is not a Windows
Runtime interface.
o Derive from types that are not in the Windows
Runtime.
.NET Framework mappings of Windows Runtime types
19. Windows Runtime Components Rules
Using the Windows Runtime from JavaScript
and managed code:
o The Windows Runtime can be called from either
JavaScript or managed code.
o Windows Runtime objects can be passed back and
forth between the two.
o Events can be handled from either side.
o The ways you use Windows Runtime types in the two
environments differ in some details, because
JavaScript and the .NET Framework support the
Windows Runtime differently.
20. Windows Runtime Components Rules
You can throw any exception type that is
included in the .NET for Windows Store apps -
supported APIs.
You can't declare your own public exception
types in a Windows Runtime component.
The way the exception appears to the caller
depends on the way the calling language
supports the Windows Runtime.
o In JavaScript, the exception appears as an object in
which the exception message is replaced by a stack
trace.
o In C++, the exception appears as a platform
exception.
22. Windows Runtime Components Rules
You can return a managed type, created in
managed code, to JavaScript as if it were the
corresponding Windows Runtime type.
o When a managed type implements multiple
interfaces, JavaScript uses the interface that appears
first in the list.
o If you pass an unassigned JavaScript variable as a
string argument, what you get is the string
"undefined".
24. Windows Runtime Components Rules
You can declare events by using the standard
.NET Framework event pattern or other
patterns used by the Windows Runtime.
o When you expose an event in the Windows
Runtime, the event argument class inherits from
System.Object. It doesn't inherit from
System.EventArgs, as it would in the .NET
Framework, because EventArgs is not a Windows
Runtime type.
o If you declare custom event accessors for your
event, you must use the Windows Runtime event
pattern. Custom events and event accessors in
Windows Runtime Components
26. Windows Runtime Components Rules
To implement an asynchronous method in your
component, add "Async" to the end of the
method name and return one of the Windows
Runtime interfaces that represent
asynchronous actions or operations:
o IAsyncAction
o IAsyncActionWithProgress<TProgress>
o IAsyncOperation<TResult>
o IAsyncOperationWithProgress<TResult, TProgress>
27. Windows Runtime Components Rules
For asynchronous actions and operations that
do not support cancellation or progress
reporting, you can use the AsAsyncAction or
AsAsyncOperation<TResult> extension method
to wrap the task in the appropriate interface.
29. Understand the Cost
Creating Windows Runtime Components using
managed languages is a powerful feature.
It’s important to understand the cost when
you’re using it in projects:
o Windows Store apps built using native code don’t
require the CLR to run.
o The code written for the managed Windows Runtime
Component will be compiled just-in-time (JIT).
o The GC might pause your app while it’s performing
work.
30. When to Create Managed Windows Runtime
Components
When to Create Managed Windows Runtime
Components:
o Consumable from all of the Windows Store
languages.
o Your team is more experienced in C# or Visual Basic
than in C++
o You have existing algorithms written in a managed
language
o …
31. When to Create Managed Windows Runtime
Components
If you are creating a component for use only in
Windows Store apps with Visual Basic or
C#, and the component does not contain
Windows Store controls, consider using the
Class Library (Windows Store apps) template
instead of the Windows Runtime Component
template !!!
Alternatives for Managed Projects:
o Class library for Windows Store
o Portable Class Library
o Extension SDK
33. Links
Creating Windows Runtime Components in C#
and Visual Basic
Walkthrough: Creating a simple component in C#
or Visual Basic and calling it from JavaScript.
Windows Runtime Components in a .NET World
34. NetMF@Work
31 Maggio 2013 ore 09:00
Sede Microsoft
Via Avignone 10, Roma
Un’intera giornata per parlare di
.NET Micro Framework e .NET
Gadgeteer, dall’introduzione alla
realizzazione di soluzioni IoT reali
per un mondo di device
interconessi