如何将 COM 公开的 .NET 项目添加到 VB6(或 VBA)引用对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/742851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Add a COM-Exposed .NET Project to the VB6 (or VBA) References Dialog?
提问by Mike Rosenblum
I have created a .NET assembly that is exposed to COM according to the exceptional article Build and Deploy a .NET COM Assemblyby Phil Wilson.
我根据Phil Wilson的特殊文章Build and Deploy a .NET COM Assembly创建了一个暴露给 COM 的 .NET 程序集。
And everything works fine in the sense that the .NET assembly is properly registered for COM, and compiled COM code can call it without any trouble.
一切正常,因为 .NET 程序集已为 COM 正确注册,并且编译后的 COM 代码可以毫无困难地调用它。
The only odd thing is that developing against the COM-exposed .NET assembly when using VB 6.0 or VBA requires that the programmer 'browse' to the exact file location of the associated .tlb file, after which everything works just fine. That is, the class library is not showing up directly within the References dialog box, so you must browse to the file location.
唯一奇怪的是,在使用 VB 6.0 或 VBA 时针对暴露于 COM 的 .NET 程序集进行开发需要程序员“浏览”到关联的 .tlb 文件的确切文件位置,之后一切正常。也就是说,类库不会直接显示在“引用”对话框中,因此您必须浏览到文件位置。
Again, the COM Interop aspects do work 100%; however, I would think that there must be some setting that would make the library directly visible within the References dialog for VB 6.0 and VBA.
同样,COM Interop 方面确实 100% 有效;但是,我认为必须有一些设置可以使库在 VB 6.0 和 VBA 的“引用”对话框中直接可见。
Does anyone know what this setting would be? Or should this be happening automatically for me just by having it registered?
有谁知道这个设置是什么?或者这是否应该仅通过注册就自动发生?
Much thanks in advance for any advice...
非常感谢您的任何建议...
Mike
麦克风
Edit/Update
编辑/更新
To answer jpoh's question about whether I'm using the /codebase switch, I'm using a .msi Setup Package, and not explicitly using RegAsm. The assembly is being correctly registered, which can be seen by the fact that within HKCR\CLSID{myGUID}\InprocServer32, the 'CodeBase' key correctly holds the full path to the assembly. Compiled COM components execute against this dll just fine, it's only when developing against it using VB 6.0 or VBA that they do not appear within the references dialog box. I therefore need to 'browse' to the correct file location, after which it works 100% fine.
要回答 jpoh 关于我是否使用 /codebase 开关的问题,我使用的是 .msi 安装包,而不是明确使用 RegAsm。程序集已正确注册,这可以从以下事实中看出:在 HKCR\CLSID{myGUID}\InprocServer32 中,“CodeBase”键正确保存了程序集的完整路径。编译后的 COM 组件可以很好地针对这个 dll 执行,只有在使用 VB 6.0 或 VBA 对其进行开发时,它们才会出现在引用对话框中。因此,我需要“浏览”到正确的文件位置,之后它可以 100% 正常工作。
Update #2
更新 #2
Upon further research, it appears that, although the class GUIDs are being registered properly, my .tlb file is not being registered. I have no idea why not. Registering the .tlb file should put some registry entries for the interface on which my class is based at HKCR\Interface{myInterfaceGUID}, but this is not occurring. Strange that this lack of registration does not seem to affect the dll's ability to function, other than its discoverability within VB6's and VBA's references dialog.
经过进一步研究,似乎虽然类 GUID 已正确注册,但我的 .tlb 文件并未注册。我不知道为什么不。注册 .tlb 文件应该在 HKCR\Interface{myInterfaceGUID} 中放置我的类所基于的接口的一些注册表项,但这并没有发生。奇怪的是,这种缺乏注册似乎并没有影响 dll 的功能,除了它在 VB6 和 VBA 的引用对话框中的可发现性。
The properties for my .tlb file within the Setup Project do seem to be correct: the 'PackageAs' property is set to 'vsdpaDefault' and the 'Register' property is set to 'vsdrfCOM'. I am puzzled as to why this would not be successfully installed on the target machine.
安装项目中我的 .tlb 文件的属性似乎是正确的:“PackageAs”属性设置为“vsdpaDefault”,“Register”属性设置为“vsdrfCOM”。我很困惑为什么这不能成功安装在目标机器上。
Update #3
更新 #3
Ok, it turns out that the Setup Project is notbeing built successfully... despite it reporting that the "Build Succeeded".
好吧,事实证明安装项目没有成功构建......尽管它报告“构建成功”。
There is actually a build warning (amazingly, a warning, not an error) being reported that it as "Unable to create registration information for file name 'DotNetLibrary3.tlb'". Since this was a warning, and not an error, the compile was stating "Build Succeeded" and the Error List was notopening up.
实际上有一个构建警告(令人惊讶的是,警告,而不是错误)被报告为“无法为文件名‘DotNetLibrary3.tlb’创建注册信息”。由于这是一个警告而不是错误,编译显示“构建成功”并且错误列表没有打开。
Tracking this down, it seems that this can be an issue when attempting to create a Setup Project when Vista is your development machine, as described here:
跟踪这一点,当 Vista 是您的开发机器时,尝试创建安装项目时,这似乎可能是一个问题,如下所述:
COM typelib registration problem in VS2008 Setup project
There is a somewhat manual fix described here:
这里描述了一些手动修复:
Feedback: Unable to create registration information for file named 'filename'
I have not tried the fix yet, but I will tomorrow and I'll report back if this solves it.
我还没有尝试过修复,但我明天会尝试,如果解决了,我会回来报告。
Update #4
更新 #4
That did not work out so well... It seems that running RegCap.exe as suggested in that article does not work when running on Vista. Since RegCap is actually run internally by the setup project itself on creating the .msi, this is not terribly surprising. In short, the setup project was almost certainly failing because the RegCap command that it was calling was failing... So calling RegCap directly is of no help.
效果不是很好... 似乎在 Vista 上运行时,按照该文章中建议的方式运行 RegCap.exe 不起作用。由于 RegCap 实际上在创建 .msi 时由安装项目本身在内部运行,因此这并不奇怪。简而言之,安装项目几乎肯定失败了,因为它调用的 RegCap 命令失败了……所以直接调用 RegCap 没有帮助。
The bottom line is that this is simply a bug when attempting to create a setup package on Vista. Or, perhaps it is a combination of Visual Studio 2008 and Vista, I'm not sure. Attempting the same exact approach is to create a setup project on Visual Studio 2005 running on Windows XP had absolutely no problems whatsoever.
最重要的是,这只是尝试在 Vista 上创建安装包时的一个错误。或者,也许它是 Visual Studio 2008 和 Vista 的组合,我不确定。尝试完全相同的方法是在 Windows XP 上运行的 Visual Studio 2005 上创建一个安装项目,绝对没有任何问题。
There very well may be fixes to get this running right on Vista and/or Visual Studio 2008, but I could not track it down. Much more efficient for me was to build using Visual Studio 2005 on Windows XP to generate the COM registration requirements, and then to import them into my Visual Studio 2008 setup project. These can be exported as .REG files via regasm using a /regfile switch against the dll and using RegCap (running on W'XP!) against the .tlb file. Since my COM interfaces will not be changing, I only have to do this once.
可能有很多修复程序可以在 Vista 和/或 Visual Studio 2008 上正确运行,但我无法找到它。对我来说更有效的是在 Windows XP 上使用 Visual Studio 2005 生成 COM 注册要求,然后将它们导入到我的 Visual Studio 2008 安装项目中。这些可以通过 regasm 导出为 .REG 文件,对 dll 使用 /regfile 开关,对 .tlb 文件使用 RegCap(在 W'XP 上运行!)。因为我的 COM 接口不会改变,所以我只需要做一次。
Hopefully this issue in Visual Studio 2008 when running on Vista will be corrected at some point, but if not, hopefully this post will be of some value to someone else who finds him or herself in the same situation...
希望在 Vista 上运行时 Visual Studio 2008 中的这个问题会在某个时候得到纠正,但如果没有,希望这篇文章对那些发现自己处于相同情况的其他人有一定的价值......
See Also:
也可以看看:
如何在自动化服务器列表中安装和注册用 VB.NET 编写的 COM Server for Excel?
-- Mike
——迈克
回答by jpoh
Did your regasm
command include the /codebase
flag?
您的regasm
命令是否包含/codebase
标志?
回答by John Thewlis
I know this was a long time ago, but I can't find any sensible answers. I had a similar problem and the resolution was to run Visual Studio as the same user who installed it. If I run it as a different user everything except the COM registrations works.
我知道这是很久以前的事了,但我找不到任何合理的答案。我遇到了类似的问题,解决方案是以安装它的同一用户身份运行 Visual Studio。如果我以不同的用户身份运行它,除了 COM 注册之外的一切都有效。
回答by Simon A
The Typelib registry entries will be missing in the registry. You need to use regtlibv12 [path]MyTypeLib.tlb from the Microsoft.NET\framework\ folder for the version of .NET you built the assembly with. After doing this, the additional registry entries will exist, and VBA references can find it. Alternatively for Windows 10 I use regtlib.exe [path]MyTypeLib.tlb in the windows folder You must then reharvest the asesembly for your installer so it now has the typelib entries. I personally use wix installers, and use 'heat' for this purpose. Regasm /regfile will not do it. Use heat on both the .dll and the .tlb to get both the classid and the typelib registry entries
注册表中将缺少 Typelib 注册表项。您需要将 Microsoft.NET\framework\ 文件夹中的 regtlibv12 [path]MyTypeLib.tlb 用于构建程序集的 .NET 版本。执行此操作后,将存在其他注册表项,并且 VBA 引用可以找到它。或者,对于 Windows 10,我在 windows 文件夹中使用 regtlib.exe [path]MyTypeLib.tlb 然后您必须重新收集安装程序的程序集,以便它现在具有 typelib 条目。我个人使用 wix 安装程序,并为此使用“热”。Regasm /regfile 不会这样做。在 .dll 和 .tlb 上使用 heat 来获取 classid 和 typelib 注册表项
回答by Atario
It doesn't give the warning if you run VS2008 under Vista, but not elevated. Elevated is the way we (Vista users) learn to always use the thing, but for this, for some reason, it likes it better the other way.
如果你在 Vista 下运行 VS2008,它不会发出警告,但没有提升。提升是我们(Vista 用户)学习始终使用该事物的方式,但为此,出于某种原因,它更喜欢另一种方式。
N.B.: while elevated, mine launched some sort of install for SQL Server Tools that I was eventually able to get past, but when running not elevated, it tries to do this again and fails with permission problems. It doesn't seem to affect the actual build result, though.
注意:虽然提升了,但我的 SQL Server 工具启动了某种安装,我最终能够通过,但是当没有提升运行时,它尝试再次执行此操作并因权限问题而失败。不过,它似乎不会影响实际的构建结果。