2. Metadata in Windows Runtime
Providing a way to use metadata in
Windows Runtime is a very important feature in Windows 8 style
application development. It is an indispensable resource to make the
architecture more robust, the APIs more discoverable, and, in the long
run, developers more productive. Well, the different development
technologies behind Windows are a bit scattered by means of metadata
format they utilize:
- The Win32 API is a large collection of data structures and
operations that can be accessed through Dynamic Link Libraries (DLLs)
sitting in the System32 folder under your Windows installation directory. These libraries include kernel32.dll, user32.dll, netapi32.dll, avicap32.dll, and many more. DLLs have many operation entry points. However, having only plain .dll files, you cannot tell what operations they contain, and how to invoke them, because they are not self-descriptive.
- The component object model (COM) that Microsoft introduced in 1993
was a binary standard that allowed developers to write the interfaces
of objects in Interface Definition Language (IDL). An .idl file can be compiled into a Type Library (.tbl file) that represents metadata related to COM objects.
- The .NET Framework promoted type metadata into a first-class citizen. In .NET, types are the objects that provide you with utilizable operations, and type metadata
describes the available operations. Assembly files (the binary files
resulting from the compilation of the source code written in any of the
.NET languages) are self-descriptive. They contain the metadata about
types and operations encapsulated into the assembly.
The way .NET handles metadata is magnificent!
However, metadata can be bound only to managed types. Many Win32
operations simply do not have a .NET type wrapping them. Using these
types from managed code requires creating a definition that substitutes
the missing metadata. Such definitions are laborious to describe, and
you need documentation about the operation you intend to use. One such
an example is the definition of the capCreateCaptureWindow operation that can be found in the avicap32.dll:
[DllImport("avicap32.dll", EntryPoint="capCreateCaptureWindow")]
static extern int capCreateCaptureWindow(
string lpszWindowName, int dwStyle,
int X, int Y, int nWidth, int nHeight,
int hwndParent, int nID);
If you create a wrong definition (for example, you use double X instead of int X),
your source code will compile, but will raise a runtime error, causing
you to spend a lot of effort to find and troubleshoot this issue.
Metadata Format
The designers of Windows Runtime were
inspired by the .NET Framework when architecting the metadata
subsystem. They decided to use the metadata format utilized by .NET
assemblies, which has already proven its adequacy over the past ten
years. The designers chose the format used by .NET Framework 4.5 (the
newest version of .NET, released in tandem with Windows 8).
Metadata files provide information about each of
the APIs available in Windows Runtime. They are installed with Windows
8, and so they can be found on each computer with Windows 8
independently, even if that machine is used for development or just for
another business. These files are machine-readable, and you can examine
their content, as you learn in the following exercise.
Peeking into Windows Runtime Metadata
Windows Runtime metadata files are located under the Windows installation folder. They have a.winmd extension, and you can examine their content with the ILDASM utility.
To examine the metadata files, follow these steps:
1. In the Start screen, click the Desktop tile, and then, from the taskbar, select Windows Explorer.
2. Navigate to the System32\WinMetadata
folder under your Windows installation directory. If your operating
system is installed to the default location, you can find this folder
under C:\Windows. If you provided a different location during the setup of Windows, choose that folder.
3. This folder lists more than a hundred files, each with a name starting with Windows, and having the .winmd extension, as shown in Figure 3. These files represent the metadata information of Windows Runtime APIs.
4. Scroll down to the Windows.Graphics
file. Right-click it and select the Open With command from the context
menu. A pop-up appears on the screen, and there you can associate an
application with the .winmd file type. Scroll down to the bottom of the pop-up, and select, “Look for an app on this PC,” as shown in Figure 4.
5. The Open With dialog box pops up on the screen. In the File Name text box, type the C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools
path (or navigate to this path using the Open With dialog box). Click
Open. The dialog box is populated with the files in this folder. Scroll
down to ildasm and click Open again.
6. The ILDASM utility opens and displays the metadata tree of the Windows.Graphics.winmd file. Expand the Windows.Graphics.Display node, and then the Windows.Graphics.Display.ResolutionScale node. You can discover all types defined in this metadata file, and also the enumeration values of the ResolutionScale type, as shown in Figure 5.
7. In the ILDASM application window, double-click the MANIFEST node. A new window opens to display the manifest information of the metadata file, as shown in Figure 6.
The manifest of a metadata file carries important information about the file and about the types described in the file. In Figure 9, you can see that there is a .module Windows.Graphics.winmd
entry with several lines following it. These items describe information
about the metadata file. You see three entries starting with .assembly extern, and these are references to other metadata information related to types used within the Windows.Graphics metadata file. The first reference, mscorlib, is the main system component of the .NET 4.5 Common Language Runtime (CLR). The other two, Windows.Foundation and Windows.Storage, respectively, are other .windm files — as can be inferred from the windowsruntime modifier tag they have.
8. Open other metadata files from the System32\WinMetadata folder, and examine their structure. When you have finished, close all open ILDASM instances.
How It Works
The System32\WinMetadata folder contains essential .windm files. In Step 5, you associated the .winmd file extension with the ILDASM utility. In Step 6, you peeked into the metadata of the Windows.Graphics.Display.ResolutionScale type. In Step 7, you examined how the .winmd manifest describes dependencies on external metadata files.