- Standard C++ data types and C++/WinRT
- Standard initializer lists
- Standard arrays and vectors
- Raw arrays, and pointer ranges
- winrt::array_view functions and operators
- IVector and standard iteration constructs
- C++ coroutines with asynchronous Windows Runtime APIs
- Windows Data Types for Strings
- Windows Data Types
Standard C++ data types and C++/WinRT
With C++/WinRT, you can call Windows Runtime APIs using Standard C++ data types, including some C++ Standard Library data types. You can pass standard strings to APIs (see String handling in C++/WinRT), and you can pass initializer lists and standard containers to APIs that expect a semantically equivalent collection.
Standard initializer lists
An initializer list (std::initializer_list) is a C++ Standard Library construct. You can use initializer lists when you call certain Windows Runtime constructors and methods. For example, you can call DataWriter::WriteBytes with one.
There are two pieces involved in making this work. First, the DataWriter::WriteBytes method takes a parameter of type winrt::array_view.
winrt::array_view is a custom C++/WinRT type that safely represents a contiguous series of values (it is defined in the C++/WinRT base library, which is %WindowsSdkDir%Include\ \cppwinrt\winrt\base.h ).
Second, winrt::array_view has an initializer-list constructor.
In many cases, you can choose whether or not to be aware of winrt::array_view in your programming. If you choose not to be aware of it then you won’t have any code to change if and when an equivalent type appears in the C++ Standard Library.
You can pass an initializer list to a Windows Runtime API that expects a collection parameter. Take StorageItemContentProperties::RetrievePropertiesAsync for example.
You can call that API with an initializer list like this.
Two factors are at work here. First, the callee constructs a std::vector from the initializer list (this callee is asynchronous, so it’s able to own that object, which it must). Second, C++/WinRT transparently (and without introducing copies) binds std::vector as a Windows Runtime collection parameter.
Standard arrays and vectors
winrt::array_view also has conversion constructors from std::vector and std::array.
So, you could instead call DataWriter::WriteBytes with a std::vector.
Or with a std::array.
C++/WinRT binds std::vector as a Windows Runtime collection parameter. So, you can pass a std::vector , and it will be converted to the appropriate Windows Runtime collection of winrt::hstring. There’s an extra detail to bear in mind if the callee is asynchronous. Due to the implementation details of that case, you’ll need to provide an rvalue, so you must provide a copy or a move of the vector. In the code example below, we move ownership of the vector to the object of the parameter type accepted by the async callee (and then we’re careful not to access vecH again after moving it). If you want to know more about rvalues, see Value categories, and references to them.
But you can’t pass a std::vector where a Windows Runtime collection is expected. This is because, having converted to the appropriate Windows Runtime collection of std::wstring, the C++ language won’t then coerce that collection’s type parameter(s). Consequently, the following code example won’t compile (and the solution is to pass a std::vector instead, as shown above).
Raw arrays, and pointer ranges
Bearing in mind the caveat that an equivalent type may exist in the future in the C++ Standard Library, you can also work directly with winrt::array_view if you choose to, or need to.
winrt::array_view has conversion constructors from a raw array, and from a range of T* (pointers to the element type).
winrt::array_view functions and operators
A host of constructors, operators, functions, and iterators are implemented for winrt::array_view. A winrt::array_view is a range, so you can use it with range-based for , or with std::for_each.
For more examples and info, see the winrt::array_view API reference topic.
IVector and standard iteration constructs
SyndicationFeed.Items is an example of a Windows Runtime API that returns a collection of type IVector (projected into C++/WinRT as winrt::Windows::Foundation::Collections::IVector ). You can use this type with standard iteration constructs, such as range-based for .
C++ coroutines with asynchronous Windows Runtime APIs
You can continue to use the Parallel Patterns Library (PPL) when calling asynchronous Windows Runtime APIs. However, in many cases, C++ coroutines provide an efficient and more easily-coded idiom for interacting with asynchronous objects. For more info, and code examples, see Concurrency and asynchronous operations with C++/WinRT.
Windows Data Types for Strings
Most string operations can use the same logic for Unicode and for Windows code pages. The only difference is that the basic unit of operation is a 16-bit character (also known as a wide character) for Unicode and an 8-bit character for Windows code pages. The Windows header files provide several type definitions that make it easy to create sources that can be compiled for Unicode or for Windows code pages.
Windows supports three sets of character and string data types: a set of generic type definitions that can compile for either Unicode or Windows code pages, and two sets of specific type definitions. One set of specific type definitions is for use with Unicode, and the other is for use with Windows code pages.
An application using generic data types can be compiled for Unicode simply by defining «UNICODE» before the #include statements for the header files, or during compilation. New Windows applications should use Unicode to avoid the inconsistencies of varied code pages and to simplify localization. They should be written with generic data types, and should define «UNICODE» in order to compile these types into Unicode types. In the few places where an application must work with 8-bit character data, it can make explicit use of the types for Windows code pages.
The ability to compile the generic types into types for Windows code pages exists mainly to support legacy applications. To compile for Windows code pages, the application just omits the UNICODE definition.
The following example shows the method used in the Windows header files to define the three sets of data types. For the implementation, see the Winnt.h header file.
The letter «T» in a type definition, for example, TCHAR or LPTSTR, designates a generic type that can be compiled for either Windows code pages or Unicode. The letter «W» in a type definition, for example, WCHAR or LPWSTR, designates a Unicode type. Because Windows code pages are of the older form, they have simple type definitions, such as CHAR and LPSTR. For a complete description of data types in Windows, see Windows Data Types.
Windows Data Types
The data types supported by Windows are used to define function return values, function and message parameters, and structure members. They define the size and meaning of these elements. For more information about the underlying C/C++ data types, see Data Type Ranges.
The following table contains the following types: character, integer, Boolean, pointer, and handle. The character, integer, and Boolean types are common to most C compilers. Most of the pointer-type names begin with a prefix of P or LP. Handles refer to a resource that has been loaded into memory.
For more information about handling 64-bit integers, see Large Integers.
Data type | Description | |
---|---|---|
APIENTRY | The calling convention for system functions. This type is declared in WinDef.h as follows: #define APIENTRY WINAPI | |
ATOM | An atom. For more information, see About Atom Tables. This type is declared in WinDef.h as follows: typedef WORD ATOM; | |
BOOL | A Boolean variable (should be TRUE or FALSE). This type is declared in WinDef.h as follows: typedef int BOOL; | |
BOOLEAN | A Boolean variable (should be TRUE or FALSE). This type is declared in WinNT.h as follows: typedef BYTE BOOLEAN; | |
BYTE | A byte (8 bits). This type is declared in WinDef.h as follows: typedef unsigned char BYTE; | |
CALLBACK | The calling convention for callback functions. This type is declared in WinDef.h as follows: #define CALLBACK __stdcall CALLBACK, WINAPI, and APIENTRY are all used to define functions with the __stdcall calling convention. Most functions in the Windows API are declared using WINAPI. You may wish to use CALLBACK for the callback functions that you implement to help identify the function as a callback function. | |
CCHAR | An 8-bit Windows (ANSI) character. This type is declared in WinNT.h as follows: typedef char CCHAR; | |
CHAR | An 8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts. This type is declared in WinNT.h as follows: typedef char CHAR; | |
COLORREF | The red, green, blue (RGB) color value (32 bits). See COLORREF for information on this type. This type is declared in WinDef.h as follows: typedef DWORD COLORREF; | |
CONST | A variable whose value is to remain constant during execution. This type is declared in WinDef.h as follows: #define CONST const | |
DWORD | A 32-bit unsigned integer. The range is 0 through 4294967295 decimal. This type is declared in IntSafe.h as follows: typedef unsigned long DWORD; | |
DWORDLONG | A 64-bit unsigned integer. The range is 0 through 18446744073709551615 decimal. This type is declared in IntSafe.h as follows: typedef unsigned __int64 DWORDLONG; | |
DWORD_PTR | An unsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. (Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows.) This type is declared in BaseTsd.h as follows: typedef ULONG_PTR DWORD_PTR; | |
DWORD32 | A 32-bit unsigned integer. This type is declared in BaseTsd.h as follows: typedef unsigned int DWORD32; | |
DWORD64 | A 64-bit unsigned integer. This type is declared in BaseTsd.h as follows: typedef unsigned __int64 DWORD64; | |
FLOAT | A floating-point variable. This type is declared in WinDef.h as follows: typedef float FLOAT; | |
HACCEL | A handle to an accelerator table. This type is declared in WinDef.h as follows: typedef HANDLE HACCEL; | |
HALF_PTR | Half the size of a pointer. Use within a structure that contains a pointer and two small fields. This type is declared in BaseTsd.h as follows:
| |
HANDLE | ||
POINTER_UNSIGNED | ||
SERVICE_STATUS_HANDLE |