Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: ComObject.CreateInstance for ComWrappers #106912

Closed
Gaoyifei1011 opened this issue Aug 24, 2024 · 2 comments
Closed

[API Proposal]: ComObject.CreateInstance for ComWrappers #106912

Gaoyifei1011 opened this issue Aug 24, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Interop-coreclr
Milestone

Comments

@Gaoyifei1011
Copy link

Gaoyifei1011 commented Aug 24, 2024

Background and motivation

With the popularity of.native AOT use, more and more people are switching from the built-in com interop to the new ComWrappers. In the previous built-in Com Interop, we used the Activater.createInstance (Type.GetTypeFromCLsID(Guid guid)) method to create a system.__comobject object, The P/Invoke CoCreateInstance method can also be used to create COM objects, but after switching to ComWrappers, we can only use the P/Invoke CoCreateInstance method to create COM objects. So I think the.net team should at System.Runtime.InteropServices.Marshalling.Com provides a static method in the Object class, can be convenient to create a COM Object.
In CsWinRT, such static methods are already provided

随着 .Native AOT 使用的普及,越来越多的人从内置的 com interop 转换为新的 ComWrappers。在之前内置的 Com Interop 中,我们可以使用 Activator.CreateInstance(Type.GetTypeFromCLsID(Guid guid))这个方法创建 system.__comobject 对象,也可以 P/Invoke CoCreateInstance 方法来创建 COM 对象,然而切换到 ComWrappers 后,我们只能使用 P/Invoke CoCreateInstance 方法来创建 COM 对象。所以我认为 .NET 团队应该在 System.Runtime.InteropServices.Marshalling.ComObject 类中提供一个静态方法,可以方便快捷的创建 COM 对象。
在 CsWinRT 中,已经提供了这样的静态方法

Reference link in CsWinRT:microsoft/CsWinRT#1717

API Proposal

namespace System.Runtime.InteropServices.Marshalling

public enum ClassContext : uint
{
    InProcServer,
    LocalServer
}

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

    public static ComObject CoCreateInstance(in Guid classId, in Guid interfaceId, ClassContext classContext);
}

API Usage

// Fancy the value
ComObject c = ComObject.CreateInstance(in Guid classId, in Guid interfaceId, ClassContext classContext);
Or

T comobject = ComObject.CreateInstance<T>(in Guid classId, in Guid interfaceId, ClassContext classContext);

### Alternative Designs

None

### Risks

None
@Gaoyifei1011 Gaoyifei1011 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Aug 24, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Aug 24, 2024
@agocke agocke added this to the Future milestone Sep 11, 2024
@agocke agocke removed the untriaged New issue has not been triaged by the area owner label Sep 11, 2024
@agocke
Copy link
Member

agocke commented Sep 11, 2024

cc @jkoritzinsky

@agocke agocke added this to AppModel Sep 11, 2024
@AaronRobinsonMSFT
Copy link
Member

Duplicate of #106979

@AaronRobinsonMSFT AaronRobinsonMSFT marked this as a duplicate of #106979 Sep 29, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Interop-coreclr
Projects
Archived in project
Development

No branches or pull requests

3 participants