Watching the //BUILD stuff, I saw that WinRT API's can be consumed by C code:
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?
Watching the //BUILD stuff, I saw that WinRT API's can be consumed by C code:
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?
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.
T^
are references to managed objects in C++/CLI. The above is rather C++/CX, which reuses (most) of the same syntax for WinRT. In this case, T^
are smart pointers to WinRT objects - more specifically, to interfaces inheriting from IInspectable
(which in turn inherits from IUnknown
, so they are also COM objects). When you copy them around, AddRef
and Release
are called as needed, and same when they go out of scope. If you dynamic_cast
some T^
to some other U^
, this will actually do a QueryInterface
. There are also T%
types, which are to T^
as U&
is to U*
.
Sep 17, 2011 at 6:43
ComPtr<T>
is there, but it would only let you get rid of Release()
call. I couldn't find any HSTRING
wrapper. I think I'll leave the sample as is, so that the correspondence between C and C++ versions is more obvious.
Sep 19, 2011 at 8:19
__x_ABI_CWindows_CUI_CXaml_CControls_CITextBlock
. This is what has confused me when trying out your example. When writing the comment, though, what I had in mind is a minimal, complete, pure C program -- which would certainly fit the OP's question and deserve copious amounts of gratitude from fans of C.
Nov 22, 2012 at 13:38