CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Add 'CoCreateInstance' helper

Open Sergio0694 opened this issue 1 year ago • 1 comments
trafficstars

This PR adds the following APIs:

namespace WinRT;

public enum ClassContext : uint
{
    InProcServer,
    LocalServer
}

public class IInspectable
{
    public static T CoCreateInstance<T>(in Guid classId, in Guid interfaceId, ClassContext classContext)
        where T : class;
}

These can be used to easily create WinRT objects from out-of-proc COM servers.

For instance, for a Windows Package Manager type:

PackageManager packageManager = IInspectable.CoCreateInstance<PackageManager>(
    CLSID_PackageManager,
    IID_IPackageManager,
    ClassContext.LocalServer);

Sergio0694 avatar Aug 16 '24 07:08 Sergio0694

As the IID is in the metadata for types, could we reference it that way instead of needing to declare it explicitly? (Sadly not true for CLSID)

shmuelie avatar Oct 28 '24 18:10 shmuelie

Closing this, we're not really planning on adding new functionality to 2.x at this point. We can re-evaluate in the future whether we'd want a public API like this in 3.0, though the API surface and implementation would have to be different for sure.

Sergio0694 avatar Oct 30 '25 17:10 Sergio0694