强制 .NET 互操作使用本地 COM DLL

发布于 2024-07-29 16:50:13 字数 504 浏览 5 评论 0原文

是否可以强制互操作程序集引用其关联的 COM DLL 的本地副本?

场景如下:

我有一个引用互操作程序集 (Interop.OTAClient.dll) 的 .NET 应用程序,该程序集是 COM DLL 的互操作程序(OTAClient.dll,它是 HP Quality Center 的自动化 API)。 我对 COM 不太了解,但据我了解,互操作程序集通过注册表中的 GUID 引用查找 COM 类,而不是指向特定文件。

我遇到的问题是,注册表项指向的 OTAClient.dll 副本会被不同版本覆盖,具体取决于我刚刚在浏览器中登录的 QC 版本,并且这些 DLL 的不同版本与彼此。 .NET 应用程序只会连接到特定版本的 QC,因此我不能让 COM DLL 以这种方式变化。

任何建议将不胜感激,因为这种行为确实令人恼火。 我见过关于 COM 互操作问题的其他问题,但它们似乎都是关于强制使用互操作 DLL 的本地版本而不是 GAC 中的版本,而不是涉及实际 COM DLL 的这一特定场景。

Is it possible to force an interop assembly to reference a local copy of its associated COM DLL?

Here's the scenario:

I have a .NET app that references an interop assembly (Interop.OTAClient.dll), which is the interop for a COM DLL (OTAClient.dll, which is the automation API for HP Quality Center). I'm not very knowledgable on COM, but as I understand it the interop assembly looks up the COM classes via GUID references in the registry, rather than pointing to a specific file.

The issue I have is that the copy of OTAClient.dll that the registry keys point to gets overwritten by different versions depending on which version of QC I've just logged into in a browser, and the different versions of these DLLs are not compatible with each other. The .NET app will only be connecting to a specific version of QC, so I cannot have the COM DLL varying in this way.

Any suggestions would be greatly appreciated, as this behaviour is really irritating. I've seen other questions on COM interop issues, but they all seem to be about forcing a local version of the interop DLL to be used instead of one in the GAC, rather than this particular scenario involving the actual COM DLL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不弃不离 2024-08-05 16:50:13

帕维尔为我指明了正确的方向,所以我将把他的答案标记为答案。 为了其他人的利益,我做了以下事情:

  1. 添加了对原始 OTAClient.dll 的引用,并让 Visual Studio 生成互操作库。
  2. 在“解决方案资源管理器”中右键单击引用的库,然后单击“属性”。 然后将“隔离”设置为“真”。 这会导致 VS 生成一个清单文件,告诉您的程序在本地查找 COM 库,而不是在注册表中列出的库。
  3. 具体到我的场景 - 我还必须引用 Quality Center 中的 WebClient.dll 并将“隔离”设置为 True。 使用 OTA API 的应用程序不会直接使用它,但似乎由 OTAClient.dll 引用。

这样,您就可以登录和注销其版本与您的应用程序使用的版本不同的 QC 实例,而不会破坏它。 就我而言,我有一个 v9 的本地 QC 实例,用于特定于项目的自动化(出于各种原因,它经过大量定制以满足我们的需求,有大量的空间用于屏幕截图存储等),并且我的应用程序连接到该实例。 但是,对于手动测试,我还需要在 IE 中使用登录到位于其他地方的 v9.2 实例。 如果我之前登录过 v9.2 实例,那么我必须在 IE 中打开 v9 实例并让它重新下载控件,然后才能再次运行我的应用程序......但现在我不需要。 :)

Pavel pointed me in the right direction, so I will be marking his as the answer. For the benefit of everyone else, here's what I did:

  1. Added a reference to the original OTAClient.dll and let Visual Studio generate the interop library.
  2. Right-click on the referenced library in Solution Explorer and click Properties. Then set Isolated to True. This causes VS to generate a manifest file telling your program to look locally for your COM library instead of at the one listed in the registry.
  3. Specific to my scenario - I also had to reference WebClient.dll from Quality Center and set Isolated to True for that also. This is not used directly by apps using the OTA API, but seems to be referenced by OTAClient.dll.

This way, you can be logging in and out of QC instances whose versions differ from the one used by your app without breaking it. In my case, I have a local QC instance that is v9 and used for project-specific automation (for various reasons, it's heavily customised to meet our needs, has lots of space for screenshot storage etc), and to which my application connects. However, for manual testing I also need to login using in IE to a v9.2 instance located elsewhere. If I had previously logged into the v9.2 instance, I would then have to open the v9 instance in IE and let it redownload the controls before running my app again... and now I don't. :)

迷路的信 2024-08-05 16:50:13

一个小示例显示了将isolated属性设置为true后Web.config如何变化,这将帮助其他人理解当我们将isolated属性设置为true时实际发生的情况。 VS 实际上在您的代码中输入了几行,以便它将使用特定 CLSID 的 com Dll。

实际上,我在同一台服务器上确实有两个 .Net 应用程序,其中一个应用程序使用 Quality Center 10.0 Dll,其他应用程序升级到 Quality Center ALM 11.0。 所以在同一台服务器上我们不能注册同名的DLL。

A small example showing how Web.config changes after we set Isolated property to true, would help others in understanding what is actually happening at back when we set Isolated property to true. VS actually enters few lines in your code, so that it will use com Dll of specific CLSID.

Actually I do have two .Net applications on the same server, where one application uses Quality center 10.0 Dll and Other is upgraded to quality center ALM 11.0. So on the same server we can not register a DLL with same name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文