jueves, 3 de septiembre de 2015

WinRT with C++ Standard vs C++/CX

Nota: Hoy he decidido escribir esta publicación del blog en inglés.
Note: Today I decided to write this blog post in English.

In a new application than I am developing for a company, I had to decide if to make use of C++/CX (C++ with Component Extension) or make all the main stuff in C++ standard and ABI/COM.

All of you than have had to work with COM (Component Object Model) and fighting with the interfaces, reference count, etc. known the tricky and heavy that it can become.

As an example of the easy approach using C++/CX, I am creating a new Uri object, like this:

auto uriEasyWay = ref new Windows::Foundation::Uri(http://www.manuelvillasur.com);
assert(wcscmp(uriEasyWay->AbsoluteUri->Data(), L"http://www.manuelvillasur.com/") == 0);

Now, I going to show you the more difficult approach using C++ Standard and  ABI/COM interfaces:

HSTRING_HEADER header = {};
HSTRING string = nullptr;

HRESULT hr = WindowsCreateStringReference(L"Windows.Foundation.Uri", 22, &header, &string);

if (SUCCEEDED(hr)) {
    ABI::Windows::Foundation::IUriRuntimeClassFactory * factory = nullptr;
    hr = RoGetActivationFactory(string, __uuidof(factory), reinterpret_cast<void **>(&factory));

    if (SUCCEEDED(hr)) {
        HSTRING stringUri = nullptr;
        hr = WindowsCreateStringReference(L"http://www.manuelvillasur.com", 24, &header, &stringUri);

        if (SUCCEEDED(hr)) {
            ABI::Windows::Foundation::IUriRuntimeClass * uri = nullptr;
            hr = factory->CreateUri(stringUri, &uri);

            if (SUCCEEDED(hr)) {
                HSTRING absUriString = nullptr;
                hr = uri->get_AbsoluteUri(&absUriString);
                if (SUCCEEDED(hr)) {
                    const WCHAR * uriString = WindowsGetStringRawBuffer(absUriString, nullptr);
                    assert(wcscmp(uriString, L"http://www.manuelvillasur.com/") == 0);

No hay comentarios:

Publicar un comentario