Skip to content

Review COM interface declarations for conflicting ComImport/ComVisible attributes #1878

Description

@weltkante

During review of PR #1868 I noticed IOleInPlaceActiveObject was declared with both [ComImport] and [ComVisible(true)]. This is wrong, ComVisible(true) is for interfaces owned by the assembly, ComImport is for external interfaces.

In desktop framework this probably has little consequence since the assemblies come preinstalled, but now that its possible to deploy WinForms with an application it may more likely be processed by tools like regasm or TLB generators, which attempt to register COM interop owned by the assembly.

When ComVisible(true) is present this will cause registry corruption by overwriting the true owners registry entries (usually owned by Windows OS) and replacing them by .NET based values. For interfaces I believe this mostly replaces the ProxyStubClsid32 pointing to another implementation than the original entries used to have.

There should be a review for conflicting ComImport/ComVisible(true) entries, or better, just review ComVisible(true) in general and make sure it is only placed on classes/interfaces owned by WinForms.

Activity

  1. weltkante commented on Sep 12, 2019

    @weltkante
    ContributorAuthor

    a quick search for ComVisible(true) yields 238 matches in 182 files, not all of them technically wrong (for example it is placed on control classes which are certainly implemented and owned by WinForms)

    However considering that the codebase comes from Desktop Framework I wonder if there should be any ComVisible(true) attributes left in place at all, even for classes and interfaces actually owned by WinForms. The ComVisible WinForms GUIDs registered globally always used to be Desktop Framework, I wonder how many of the ComVisible classes/interfaces owned by WinForms actually need to be ComVisible in .NET Core for supported interop scenarios. Maybe most (or all) of them can be removed?

  2. added this to the 5.0 milestone on Sep 12, 2019
  3. added
    📚 documentation
    enhancement
    on Sep 12, 2019
  4. modified the milestones: 5.0 Previews 1-4, 5.0 on Apr 20, 2020
  5. ghost added
    🚧 work in progress
    on May 4, 2020
  6. ghost removed
    🚧 work in progress
    on May 5, 2020
  7. weltkante commented on May 6, 2020

    @weltkante
    ContributorAuthor

    @hughbe @RussKie I don't think this is fixed, I still find plenty of wrong [ComVisible(true)] attributes. Needs to be reopened or creating a follow-up issue for proper cleanup. I also noticed new [ComVisible(true)] attributes were being added in accessibility PRs, flagged them, but I think this needs a bit care in reviews, at least for the current time being.

  8. reopened this on May 7, 2020
  9. JeremyKuhne commented on May 11, 2020

    @JeremyKuhne
    Member

    However considering that the codebase comes from Desktop Framework I wonder if there should be any ComVisible(true) attributes left in place at all, even for classes and interfaces actually owned by WinForms. The ComVisible WinForms GUIDs registered globally always used to be Desktop Framework, I wonder how many of the ComVisible classes/interfaces owned by WinForms actually need to be ComVisible in .NET Core for supported interop scenarios. Maybe most (or all) of them can be removed?

    If classes had [ComVisible] on Framework I'd prefer to keep the attribute unless we can clearly document that it has some negative impact.

    The auditing is a must for 5.0, I don't want to see us inadvertently exposing this attribute in unintended places.

  10. weltkante commented on May 11, 2020

    @weltkante
    ContributorAuthor

    If classes had [ComVisible] on Framework I'd prefer to keep the attribute

    I scrolled through all remaining ComVisible which can be found by "search references" in VS and they all (except one) are specified without GUID, meaning they get a generated GUID. I don't remember if I checked previously, but the generated GUID seems to be different from Desktop Framework, so having these ComVisible left over is probably acceptable since they cause no conflicts, but I still think most (if not all) ComVisible attributes are redundant.

    They certainly don't contribute to compatibility with Desktop Framework since there is no shared TLB (Desktop Framework has a TLB and primary interop assembly for WinForms, you'd have to be compatible with those if you wanted that level of compatibility). So users of WinForms via COM have to rewrite all their interop code anyways and this time they don't have a TLB to import either. So while I don't think its harmful it also doesn't seem to provide any value, and it will make AOT efforts like corert potentially more complicated if they have to predeclare CCWs for objects never intended to be accessible from COM.

    That said its probably still good to remove ComVisible later if you don't feel confident doing it for 5.0 - unless people start taking a dependency on them, but I don't know why anyone would be doing that.

    unless we can clearly document that it has some negative impact.

    The only negative impacts I currently can think of involve sharing a CLSID/IID with Desktop Framework, those cases should definitely be fixed.

    There is one ComVisible + GUID remaining on IWin32Window which is shared with Desktop Framework, the registry has a TLB entry pointing to the Desktop Framework version. .NET Core should probably have its own GUID or switch to ComImport, .NET Core is not the owner of that IID.

  11. 13 remaining items

  12. RussKie commented on Jun 2, 2020

    @RussKie
  13. RussKie commented on Jun 2, 2020

    @RussKie
    Contributor

    Ah found it under dotnet/runtime#37039

  14. JeremyKuhne commented on Jun 2, 2020

    @JeremyKuhne
    Member

    Given that most AccessibleObject implementations are not createable from COM

    I'd prefer removing the [ComVisible] attributes on these accessibility objects. I'd rather try and shed this and consider pulling it back if someone genuinely has a need for it (highly unlikely). If we can't come up with a semi-plausible use case for things with [ComVisible] I'd rather see us remove the attribute. It is likely to save us more time in the long run by not having to keep analyzing it- or surface use cases we're totally not aware of (again, highly unlikely).

  15. RussKie commented on Jun 3, 2020

    @RussKie
    Contributor

    We had a private discussion with Jeremy, and concluded that we should remove all [ComVisible] decorations.
    If/when we hear from our customers that some use-case requires the decoration - we'll add it back, and have the use-case documented.

    @weltkante please let me know if you want to do it, or I'll set it up for grabs.
    Thank you

  16. 199621616 commented on Jun 3, 2020

    @199621616

    @JeremyKuhne
    示例一:医疗保险系统对接的COM互操作:
    保险系统接口规范提供PB调用示例:

    //创建地纬嵌入式接口对象,调用com组件
    int vi Oleobject sei                                 
    sei =create Oleobject     //创建ole对象seiproxy
    vi= sei.connecttonewobject('sei3') //连接com组件 if vi <> 0 then    
    messagebox('错误','创建地纬嵌入式接口sei对象失败!')    
    return end if

    我们使用.NetFramework4.6.1调用方式:
    Type type = Type.GetTypeFromProgID("sei3", true);
    dynamic COM = Activator.CreateInstance(type);

    使用同样的方式,在.NetCore3.1.4上运行返回异常:
    Creating an instance of the COM component with CLSID {D5D8D5B2-CCD5-4EEC-BE61-0CA13A1940C7} from the IClassFactory failed due to the following error: ffffdf8f 0xFFFFDF8F.


    Example 1: com interoperability of medical insurance system docking:

    The insurance system interface specification provides Pb call examples:

    //Create the ground latitude embedded interface object and call COM component

    int vi Oleobject sei                                 

    SEI = create oleobject / / create OLE object seiproxy

    vi= sei.connecttonewobject ('sei3 ') / / connect COM component if VI < > 0 then

    MessageBox ('error ',' failed to create SEI object of ground latitude embedded interface! ')    

    return end if

    We use. Netframework 4.6.1 to call:

    Type type = Type.GetTypeFromProgID ("sei3", true);

    dynamic COM = Activator.CreateInstance (type);

    Using the same method, run return exception on. NETCORE 3.1.4:

    Creating an instance of the COM component with CLSID {D5D8D5B2-CCD5-4EEC-BE61-0CA13A1940C7} from the IClassFactory failed due to the following error: ffffdf8f 0xFFFFDF8F.

  17. weltkante commented on Jun 3, 2020

    @weltkante
    ContributorAuthor

    please let me know if you want to do it

    @RussKie I'll put a PR up later today

  18. RussKie commented on Jun 3, 2020

    @RussKie
    Contributor

    Awesome 👍

  19. JeremyKuhne commented on Jun 3, 2020

    @JeremyKuhne
    Member

    @199621616 I've created dotnet/runtime#37362 to follow up on your comment as it appears that you have a general COM interop issue.

    cc: @AaronRobinsonMSFT

  20. ghost added
    🚧 work in progress
    on Jun 3, 2020
  21. ghost removed
    🚧 work in progress
    on Jun 17, 2020
  22. ghost added
    🚧 work in progress
    on Jun 17, 2020
  23. ghost removed
    🚧 work in progress
    on Jun 17, 2020
  24. removed this from the 5.0 milestone on Jun 17, 2020
  25. ghost locked as resolved and limited conversation to collaborators on Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

📚 documentationarea-COMenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions