Visar inlägg med etikett Windows Runtime. Visa alla inlägg
Visar inlägg med etikett Windows Runtime. Visa alla inlägg

torsdag 19 juni 2014

C++/CX gotchas #3: Private interfaces (continued)

As an addendum to the last post, I've realized that there is a lot of sense in the way Microsoft themselves seem to implement "private interfaces". We only need to look to IBuffer to see how, since an IBuffer implementation is required to also implement the (pure COM) interface IBufferByteAccess.

Sadly, this means C++/CX is unsuitable since it does not support mixed WinRT/COM classes. The most straight forward way to use private interfaces is therefore to switch to WRL instead.

Even better, you can consume and author these private interfaces in the .NET projection of WinRT, thanks to the very good COM interop support in the CLR.

All you need to do is to make sure the method order and signatures match, and that the interface declarations have the same IID. Let's see how, using the IBufferByteAccess interface on the C++ side (this is from robuffer.h):

    [uuid(905a0fef-bc53-11df-8c49-001e4fc686da)]
    struct IBufferByteAccess : public IUnknown
    {
        STDMETHOD(Buffer)(_Outptr_result_buffer_(_Inexpressible_("size given by different API")) byte **value) = 0;
    };

The C# counterpart of this declaration can be: 

    [ComImport]
    [Guid("905a0fef-bc53-11df-8c49-001e4fc686da")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IBufferByteAccess
    {
        void Buffer([Out] out IntPtr value);
    }

You can in fact cast an IBuffer directly to this interface. No unsafe code needed.

Also interesting is that the C# method signatures are somewhat flexible. The following is also valid:

    [ComImport]
    [Guid("905a0fef-bc53-11df-8c49-001e4fc686da")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IBufferByteAccess
    {
        IntPtr Buffer { get; }
    }

In fact, looking at Microsofts own System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer class in DotPeek shows that they use this form. 

Now, why does this even work? Not sure, but I suspect it's NOT to do with the COM interop functionality being clever - since there IS no metadata beyond the GUID here. No TLB or winmd is involved. Rather, I think it's because such a property getter has the exact same signature as the Buffer method with an [Out] parameter does.

söndag 15 september 2013

C++/CX gotchas #2: Private interfaces

Microsoft really locked down what you can express in the Windows Runtime ABI.

How do you access implementation specific stuff in a public ref class in C++/CX, assuming you know the underlying implementation is yours?

Here a ficticious example:

public interface class IFoo
{
    IAsyncAction^ SomethingAsync();

    void SomethingElse(IVector<int>^ y);    
};

public ref class Foo sealed : IFoo
{
public:
    Foo(int x);

    IAsyncAction^ SomethingAsync();

    void SomethingElse(IVector<int>^ y);    
};

Here, what you see is what you get. There is a class Foo which implements IFoo. That's a great "outer face", but not a good "inner face".

Let's assume I have a larger system which deals with IFoo objects. How do I take an IFoo^ reference and turn it into something where I can access internals and implementation details?

I could of course do dynamic_cast<Foo^>(someIFoo). This would either get me a valid Foo^, or nullptr. Great. But not very scalable. I'd have to treat every concrete implementation separately. It gets ugly fast.

Sadly there is no way to let this public ref class have a base class of any sort in the current Windows Runtime environment. Published/public ref classes must be sealed, and standard C++ classes can't be bases of ref classes at all. I can stuff all kinds of internal and private code in Foo, but there's no obvious way to access it once the object has been cast to IFoo^ and been across the ABI border.

What I really want is a private interface, but there is no support for that. Frustrating! Then I came up with a soluation, of sorts... by staring at the interface declaration and trying to think like a burglar.

I made a class called AsyncActionWrapper<T>, which implements IAsyncAction and holds two things internally:
- A real IAsyncAction^.
- A hidden "payload".

The wrapper implements all the IAsyncAction methods, and simply passes them on to the inner IAsyncAction. Thus the object appears perfectly normal, but there is an extra method there, if you know about it:

T GetPayload() { return m_payload; }

I make sure that some, or all, implementations of the method SomethingAsync wrap their result in this type of object, and hide a useful payload.

Thus I can have IFoo objects, implemented by me, or implemented by the user, and then when they are passed back in to my API, I call SomethingAsync... and then immediately cast the returned IAsyncAction:

auto asyncAction = someFoo->SomethingAsync();
auto wrapper = dynamic_cast<AsyncActionWrapper<IHiddenInterface^>^>(asyncAction);

if(wrapper)
{
    IHiddenInterface^ payload = wrapper->GetPayload();
    payload->SecretMethod("Win!");
}

Voila, private interface.

But there are drawbacks naturally. I have to either be sure that SomethingAsync does something very benign, since I'm calling it with ulterior motives (but do have to call it), or that it starts work I'd have wanted to start anyway.

There are countless variations of the trick, and obviously it doesn't have to involve async actions at all. It just happened to fit this occasion. You could just as well wrap an IVector or something else, any interface reference should do just as well.

C++/CX gotchas #1: Careful in the ctor

I've been working within the relatively new world of Windows Runtime/RT, C++/CX, WRL etc lately. It's a somewhat treacherous sea and the maps are sketchy yet (there's not much to find on Google), so I think I'll publish some tips, tricks and gotchas based on what I've found.

In no particular order, here's point number one:

Don't assume too much in the constructor.

Or rather, "in the initializer list".

One of the benefits of C++/CX is that it hides away much of the COM/WinRT plumbing. Your class gets IUnknown, IInspectable implemented automatically, and you don't normally see the intermediate code that's generated.

In this case, I needed weak references to my objects, and as it turns out, there is something called IWeakReferenceSource which is automatically implemented by all ref classes. From what I can tell, you won't get weak reference support unless your object has that plumbing.

In this case, I had set up a sort of fake aggregated "base class" (due to the Windows Runtime limitations) called Base in order to share code between my concrete classes. Each concrete class, e.g. Foo, had a reference to its Base member, and there was also the need for Base to refer back to the Foo. In the Windows Runtime world of reference counting, that means neither object will get destroyed and just lead to a memory leak (or worse, if they aren't just sitting idle).

This is well known of course, and Base needed a weak reference to Foo:

private ref class Base sealed
{
public:
    Base(Foo^ foo) 
        m_weakFoo(foo)
    {
    }

private:
    Platform::WeakReference m_weakFoo;
};

public ref class Foo sealed
{
public:
    Foo() : 
        m_base(ref new Base(this))
    {
    }

private:
    Base^ m_base;
};

This failed. From Base's perspective, it just got nullptr back from m_weakFoo.Resolve<Foo>().
Head-scratching time ensued.

Observing it in the debugger showed something interesting. Turns out the initializer list is a bit too early to rely on the ref class to be properly set up. Especially the IWeakReferenceSource related members of the class looked quite uninitialized in the debugger. Oops.

And it seems obvious that order of initialization matters... but I've forgotten enough C++/COM to not remember the rules of thumb. I had just wrongly assumed that C++/CX would hold my hand all the way.

So with that lesson learned, I moved the m_base initialization into the constructor body instead:

  Foo() 
  {
      m_base = ref new Base(this);
  }

...and it worked perfectly. (Still does in fact.)

I now stay away from doing any kind of complex logic in the initializer list, at least on my ref classes. 

Simple value initialization should be fine, and really, anything which is pure C++ should work as well, but I figure its better to be safe than sorry.

Feedback welcome, as always.