52

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?

10
  • 6
    @Mark The extensions are not required, they just make your life easier. Sep 15, 2011 at 20:00
  • 4
    @Andrew You will have to use WinRT for UI in Metro apps, Win32 is not really an option. Some Win32 calls are allowed, but not most of the UI-related ones. Sep 15, 2011 at 20:23
  • 2
    @Lothar : A quick Google search led to the Visual Studio team's blog, which confirms the C99's suport won't get better. Search for "C99" in the article, and you'll see VC11 won't support new features when compared to VC10. blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
    – paercebal
    Sep 16, 2011 at 8:30
  • 4
    It would be rather masochistic to use WinRT from C. Sep 17, 2011 at 7:45
  • 3
    The world is full of C compilers. MS doesn't have a C compiler and has not had one for years and years. Why would they start now? Sep 17, 2011 at 8:19

1 Answer 1

73

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.

11
  • 3
    can you explain the use of the circumflex (^) here? i thought it was for references to managed objects, but aren't these COM pointers? how would this code look in /real/ C++ ?
    – Spongman
    Sep 17, 2011 at 4:06
  • 11
    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
  • 3
    Isn't there a ComPtr class that can be used and which would make it almost identical to the ^ version? Sep 19, 2011 at 7:19
  • 3
    @Filip 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
  • 4
    @PavelMinaev Ah! I took another look at the headers, and currently the definition from your example is: __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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.