将ref class对象(Uri ^)作为本机指针参数传递(IUriRuntimeClass *)

时间:2017-11-14 02:40:38

标签: c++ uwp c++-cx midl

我的代码中有以下midl方法:

interface IMyClass : IInspectable { HRESULT Func1([in] Windows.Foundation.Uri *uri); }

它生成以下界面方法:

IMyClass : public IInspectable { public:virtual HRESULT STDMETHODCALLTYPE Func1( /* [in] */ __RPC__in_opt ABI::Windows::Foundation::IUriRuntimeClass *uri) = 0; }

界面在App端实现,其对象传递给我的代码,我可以在其中查看'只有界面 我想知道调用Func1并将Windows::Foundation::Uri对象作为参数传递的最佳方式是什么?

由于C2664错误,简单地传递ref class对象并不起作用 Windows::Foundation::Uri^ u = ref new Uri(...); IMyClassObj->Func1(u); // error cannot convert argument 1 from Windows::Foundation::Uri ^' to 'ABI::Windows::Foundation::IUriRuntimeClass *

我可以用reintrepret_casting实现我的目标:
Windows::Foundation::Uri^ u = ref new Uri(...); ABI::Windows::Foundation::IUriRuntimeClass* uu = reinterpret_cast<ABI::Windows::Foundation::IUriRuntimeClass*>(u); MyClassObj->Func1(u); // this works fine

在这种情况下,reinterpret_cast是否正确接近?或者是否有任何其他方式将Uri ^对象作为IUriRuntimeClass *参数传递?

1 个答案:

答案 0 :(得分:0)

执行此操作的稍微更简洁的方法是尽快将其包装在ComPtr中,以便在抛出任何异常时获得正确的行为。(您的方法今天可能很简单,但它可能会得到稍后会更复杂。)

这样的事情:

#include <wrl/client.h>
#include <windows.foundation.h>

// Dummy method that just prints out the URI to the debug console
HRESULT MyFunc(ABI::Windows::Foundation::IUriRuntimeClass* uri)
{
    HSTRING str{};
    HRESULT ret{ S_OK };
    if (SUCCEEDED(ret = uri->get_AbsoluteUri(&str)))
        OutputDebugString(WindowsGetStringRawBuffer(str, nullptr));

    WindowsDeleteString(str);
    return ret;
}

void Test()
{
    using namespace Microsoft::WRL;

    // Create the ref class
    auto uri = ref new Windows::Foundation::Uri(L"http://www.bing.com");

    // Wrap in a dummy IUnknown wrapper. In theory you could use 
    // IInspectable or even IUriRuntimeClass but if you're going to 
    // copy-paste the code elsewhere, IUnknown is the "safest" thing you
    // can reinterpret_cast<>() to.
    ComPtr<IUnknown> iUnknown{ reinterpret_cast<IUnknown*>(uri) };

    // Try to cast it to the IUriRuntimeClass, and call our method if 
    // it succeeds
    ComPtr<ABI::Windows::Foundation::IUriRuntimeClass> iUri{};
    if (SUCCEEDED(iUnknown.As(&iUri)))
        MyFunc(iUri.Get());
}