Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WinRT from C?

Watching the //BUILD stuff, I saw that WinRT API's can be consumed by C code:

enter image description here

I am rather excited about a fresh C API available to Win32 developers.

Where can I find information on the C WinRT API? How is it better than the existing Win32 C API?

like image 927
Andrew Avatar asked Sep 15 '11 19:09

Andrew


People also ask

How to install CppWinRT?

Open the project in Visual Studio, click Project > Manage NuGet Packages... > Browse, type or paste Microsoft. Windows. CppWinRT in the search box, select the item in search results, and then click Install to install the package for that project.

What is C# WinRT?

C#/WinRT is a NuGet-packaged toolkit that provides Windows Runtime (WinRT) projection support for the C# language. A projection assembly is an interop assembly, which enables programming WinRT APIs in a natural and familiar way for the target language.

What is WinRT application?

WinRT allows developers to create safe "sandboxed" touchscreen applications available from the Microsoft Store. WinRT apps support both the x86 and ARM architectures and multiple programming languages, including C/C++, C#, Visual Basic and JavaScript. WinRT was augmented by the Universal Windows Platform (see UWP).

Is WinRT based on Win32?

WinRT can be described as an API at the same level as Win32. The only difference with Win32 is that WinRT is exposed to all application developers.


1 Answers

WinRT is fundamentally COM, so using WinRT components from C is like using COM components from C. Like before, you get .idl files for all WinRT components, and also .h files produced from those .idl files. The .h files include both C++ and C declarations (wrapped in #ifdef __cplusplus as needed). You can just #include them and start hacking away.

It's not exactly neat, though, e.g. something like this C++/CX:

Windows::UI::Xaml::Controls::TextBlock^ tb = ...; tb->Text = "Foo"; 

which is equivalent to this vanilla C++:

Windows::UI::Xaml::Controls::ITextBlock* tb = ...; HSTRING hs; HRESULT hr = WindowsStringCreate(L"Foo", 3, &hs); // check hr for errors hr = tb->set_Text(hs); // check hr for errors tb->Release(); 

would be written in C as:

__x_Windows_CUI_CXaml_CControls_CITextBlock* tb = ...; HRESULT hr; HSTRING hs; hr = WindowsCreateString(L"Foo", 3, &hs); // check hr for errors hr = __x_Windows_CUI_CXaml_CControls_CITextBlock_put_Text(tb, hs); // check hr for errors IUnknown_Release(tb); 

Look inside "C:\Program Files (x86)\Windows Kits\8.0\Include\winrt" in Developer Preview to see the .idl and .h files.

like image 197
Pavel Minaev Avatar answered Sep 19 '22 19:09

Pavel Minaev