简体   繁体   English

注册COM Interop和ComVisible(true)

[英]Register for COM Interop and ComVisible(true)

I have the common library which will be used by multiple projects. 我有一个可供多个项目使用的公共库。 I have Signed it with Strong name in order to manage the multiple versions across the project. 我已使用“强名称”对其进行了签名,以便管理整个项目中的多个版本。 In this library, I have couple of classes with [ComVisble(true)] property. 在这个库中,我有几个带有[ComVisble(true)]属性的类。 However, I don't want to register this library for COM interop. 但是,我不想为COM互操作注册此库。 But I am getting compilation error which asks me to "Please register your assembly for COm Interop". 但是我收到了编译错误,要求我“请为COm Interop注册您的程序集”。

I am new to COM in .Net. 我是.Net中的COM新手。 I assume that only if I have [ComRegisterFunction] in my library, I need to register it for COM interop. 我假设只有在我的库中有[ComRegisterFunction]时,才需要为COM互操作注册它。 Correct me if I am wrong. 如果我错了,请纠正我。

If I register it with COM Interop, then Strong naming will not be helpful in maintaining the multiple versions across the project. 如果我在COM Interop中注册它,那么强命名将无助于维护项目中的多个版本。

Any help in this would be greatly appreciated. 任何帮助,将不胜感激。

However, I don't want to register this library for COM interop. 但是,我不想为COM互操作注册此库。

You have to register it, that's the way the client program finds your DLL. 您必须注册它,这是客户端程序找到DLL的方式。 The client code uses a number when it creates the object (the CLSID guid), the registry tells which DLL implements that number. 客户端代码在创建对象(CLSID guid)时使用数字,注册表告诉哪个DLL实现了该数字。 Technically the client program can use a manifest and its own local copy of your DLL but that is not under your control. 从技术上讲,客户端程序可以使用清单和它自己的DLL本地副本,但这不在您的控制之下。

I assume that only if I have [ComRegisterFunction] in my library, 我假设只有在我的库中有[ComRegisterFunction]时,

It is very rarely required. 它很少需要。 Only necessary if additional registry keys need to be written beyond the standard ones that Regasm.exe writes. 仅在需要编写超出Regasm.exe编写的标准注册表项的其他注册表项时才需要。 It does not in any way address a versioning problem. 它不能以任何方式解决版本问题。

then Strong naming will not be helpful 那么强命名将无济于事

A strong name permits you to put the assembly in the GAC. 强大的名称可以使您将程序集放入GAC。 That actually is helpful, it avoids a new version of a DLL file overwriting an old version and ensures that an old client program that was not recompiled can still find that old version of your DLL. 这实际上有帮助的,它可以避免DLL文件的新版本覆盖旧版本,并确保未重新编译的旧客户端程序仍然可以找到该DLL的旧版本。 It is not the only technique, you can also give new versions of the DLL a different file name so they can live together in a single directory without overwriting each other. 这不是唯一的技术,您还可以为DLL的新版本提供不同的文件名,以便它们可以一起生活在单个目录中,而不会彼此覆盖。 Not a very solid way to do it, programmers tend to take shortcuts when they have to fight a build system. 这不是很可靠的方法,程序员在必须与构建系统对抗时往往会采取捷径。 That's a very dangerous shortcut to take. 这是一个非常危险的捷径。 The GAC is often needed anyway, the CLR will have trouble locating dependent assemblies. 无论如何,还是经常需要GAC,CLR很难找到相关的程序集。

Do keep in mind that the GAC only solves one of the DLL Hell problems associated with COM. 请记住,GAC仅解决了与COM相关的DLL Hell问题之一 Another very important rule in COM that interfaces and coclasses are immutable. COM中的另一个非常重要的规则是接口和共同类是不可变的。 If you make a change to one of them then you must give them a different number. 如果您更改其中一个,则必须给他们一个不同的号码。 This ensures that the client program cannot accidentally create an incompatible object. 这样可以确保客户端程序不会意外创建不兼容的对象。 Very important, versioning problems are extraordinarily hard to diagnose when the client program uses early binding. 非常重要的是,当客户端程序使用早期绑定时,很难诊断版本问题。 Getting a different number is automatic in .NET if you don't explicitly use the [Guid] attribute on a [ComVisible] type. 如果未在[ComVisible]类型上显式使用[Guid]属性,则在.NET中自动获得一个不同的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM