Using WinRT from C?

前端 未结 1 769
执笔经年
执笔经年 2020-12-12 23:40

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

\"enter

1条回答
  •  [愿得一人]
    2020-12-13 00:15

    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.

    0 讨论(0)
提交回复
热议问题