3

For WinRT, IDL now supports constructs such as this:

[marshaling_behavior(agile)]
[threading(both)]
[activatable(0x06020000)]
[version(0x06020000)]
[static(Windows.Networking.Sockets.IDatagramSocketStatics, 0x06020000)]
runtimeclass DatagramSocket
{
    [default] interface Windows.Networking.Sockets.IDatagramSocket;
    interface Windows.Foundation.IClosable;
}

I'm curious about the static attribute. What does it mean? How does it relate to the interfaces listed inside the body of the runtimeclass?

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235

2 Answers2

8

Static methods on a winrt interface are implemented as interfaces off the class factory for that class.

For this case, you should call (much winrt overhead elided):

ComPtr<IDatagramSocketStatics> factory;
HRESULT hr = RoGetActivationFactory(<HSTRING for Windows.Networking.Sockets.DatagramSocket>, __iidof(IDatagramSocketStatics), &factory.GetAddressOf());
hr = factory->DatagramFactoryMethod(<Parameters>);

As I mentioned, this is pseudo-code, but it should be sufficient to see how to call the static methods.

Larry Osterman
  • 16,086
  • 32
  • 60
  • Your comment is useful as well, as it tells how to invoke statics; Hans' answer literally answered my question of what the static attributes means, so I accepted his answer. – Martin v. Löwis Oct 16 '12 at 16:51
  • The reason I gave a separate response was to express the binary behavior associated with the `Windows.Foundation.Metadata.StaticAttribute` attribute - the binary behavior is not at all clear from the attribute. – Larry Osterman Oct 17 '12 at 05:01
5

COM does not support the notion of static methods of a class, all methods must be instance methods since interface methods are abstract. The attribute allows the language projection to emulate static behavior of a method. Specifically the DatagramSocket.GetEndpointPairsAsync() overloads.

Notable as well is that COM also doesn't support overloads, also solved with an attribute. The methods of IDatagramSockeStatics have the [overload] attribute, the real name of the 2nd overload is GetEndpointPairsWithSortOptionsAsync().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536