How to: Use winmdidl.exe and midlrt.exe to create .h files from windows metadata
Winmdidl.exe and midlrt.exe enable COM-level interaction between native C++ code and Windows Runtime components. Winmdidl.exe takes as input a .winmd file that contains metadata for a Windows Runtime component and outputs an IDL file. Midlrt.exe converts that IDL file into header files that the C++ code can consume. Both tools run on the command line.
You use these tools in two main scenarios:
Creating custom IDL and header files so that a C++ app written by using the Windows Runtime Template Library (WRL) can consume a custom Windows Runtime component.
Generating proxy and stub files for user-defined event types in a Windows Runtime Component. For more information, see Raising Events in Windows Runtime Components.
These tools are required only for parsing custom .winmd files. The .idl and .h files for Windows operating system components are already generated for you. By default in Windows 8.1, they are located in \Program Files (x86)\Windows Kits\8.1\Include\winrt\.
The following example shows a winmdidl command at a Visual Studio x86 command prompt. It specifies an output directory, and a file that contains special banner text to add to the generated .idl file.
C:\Program Files (x86)\Microsoft Visual Studio 12.0>winmdidl /nologo /outdir:c:\users\giraffe\documents\ /banner:c:\users\giraffe\documents\banner.txt "C:\Users\giraffe\Documents\Visual Studio 2013\Projects\Test_for_winmdidl\Debug\Test_for_winmdidl\test_for_winmdidl.winmd"
The next example shows the console display from winmdidl that indicates that the operation succeeded.
Next, midlrt is run on the generated IDL file. Notice that the metadata_dir argument is specified after the name of the .idl file. The path of \WinMetadata\ is required—it's the location for windows.winmd.
C:\Program Files (x86)\Microsoft Visual Studio 12.0> midlrt "c:\users\mblome\documents\test_for_winmdidl.idl" /metadata_dir "C:\Windows\System32\WinMetadata"
The output file from a winmdidl operation has the same name as the input file but has the .idl file name extension.
If you are developing a Windows Runtime component that will be accessed from the WRL, you can specify winmdidl.exe and midlrt.exe to run as post-build steps so that the .idl and .h files are generated on each build. For an example, see Raising Events in Windows Runtime Components.