How to use WinRT API in Desktop Application ?

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

First of all the title of this post is not correct. The right title is “How to use WinRT API in Windows Application?”. Due the marketing overkill of IT consummation pressure,  there are more and more people who thinks that all applications have UI and can be by default named “Desktop applications”. Such marketing did a brainwash which teach us to make a difference between Apps (WinRT) and classic Desktop applications (none WinRT).

To make this clear, we have two kind of windows applications now: WinRT (Apps) and System32 applications. Both are running on windows. Desktop applications are very small subset of system32 applications (.NET, C++, Pascal etc., etc. 600E+06 applications on earth)  in number of use cases and turnover in money. When you say I know WinRT only, you target very small subset of Windows technologies.

Nevertheless, WinRT will probably become a replacement for many existing System32 API. Exactly this was the idea of WinFX, which has been introduced as I remember in year 2006. Unfortunately not everybody liked .NET.

WinRT stands for runtime and no question as long you create Windows Store application it will run on Windows Runtime (WinRT). But not many people know that WinRT has been designed to possibly replace system 32 platform (in some future scenarios). That means, you can build system32 application which uses WinRT API as System32 API would be used. In my opinion, WinRT is in such scenarios not a runtime. It is API, or even better Windows API.

To make this clear., I will show in this post how to build a .NET application (system32 (app developers call it “desktop-app”)) which will reference some WinRT API and do something with it. It is not clear to me why this scenario is not the first class citizen in Visual Studio, but we can make it.

Create Console Application on Windows 8 in Visual Studio 2012. Right mouse click on Project File and unload it. Then open the .proj file as XML and append following:

image

This element will notify the environment to build the system 32 application explicitly for Windows 8. That means such application will not run on Windows 7, Vista etc..
Now, save the project file and load it again (right mouse click –> load).

Finally go to add reference and brows to folder C:\program file(x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd. You will see few files in this folder, but only one is interesting in this scenario. Please add reference to Windows.winmd. This meta-file contains the metadata of the subset of WinRT which can be used in System32 applications.

image

If you try to add the reference to this meta-file without of changing of the project target platform as shown above, you will get following error:

image

 

Please note that the WinRT API can be used as it is, but with some compiler limitations related to events (at least). Next time more about this.


Posted Sep 14 2012, 12:30 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.