简体   繁体   English

COM访问VB.Net dll,无需强名称签名

[英]COM access to VB.Net dll without strong name signing

I'm converting a VB6 dll to VB.Net using Visual Studio 2008 Express. 我正在使用Visual Studio 2008 Express将VB6 dll转换为VB.Net。 I want to use the same .dll to integrate with Excel via Excel-DNA, but also to be available via COM (I need to be able to call it from VBScript and VBA). 我想使用相同的.dll通过Excel-DNA与Excel集成,但也希望通过COM可用(我需要能够从VBScript和VBA调用它)。

If I leave the assembly unsigned, I have access to all of the ExcelDNA functionality but no COM access. 如果我不对程序集进行签名,则可以访问所有ExcelDNA功能,但不能访问COM。

If I sign the assembly with a strong name, then when I try to build the .dll I get the following error: 如果我使用强名对程序集进行签名,那么当我尝试构建.dll时,会出现以下错误:

Unable to emit assembly: Referenced assembly 'ExcelDna.Integration' does not have a strong name 无法发出程序集:引用的程序集“ ExcelDna.Integration”没有强名称

What are my options? 我有什么选择?

You don't have to strong-name a [ComVisible] assembly. 您不必对[ComVisible]程序集进行强命名。 It is only required when you want to install it in the GAC. 仅当您要将其安装在GAC中时才需要。 Not strictly necessary although not a bad idea to fight DLL Hell. 并非绝对必要,尽管与DLL Hell打交道不是一个坏主意。 You need to register it with Regasm.exe using the /codebase option. 您需要使用/ codebase选项在Regasm.exe中进行注册。 Visual Studio already does that automatically, although the option might be missing in the Express edition. 尽管Express版中可能没有该选项,但Visual Studio已经自动执行该操作。

Fixing the second problem shouldn't be hard either. 解决第二个问题也不难。 Just rebuild the Excel-DNA solution from the source code you can download from Codeplex . 只需从可从Codeplex下载的源代码中重建Excel-DNA解决方案即可。

Excel-DNA has an option to expose your .NET classes to COM directly, so you can use them directly from VBA as regular COM classes. Excel-DNA具有将您的.NET类直接公开给COM的选项,因此您可以直接从VBA中将它们用作常规COM类。

To do this your class must be ComVisible (or the whole assembly must be ComVisible), and you must mark the ExternalLibrary as ComServer='true' in the .dna file, something like: 为此,您的类必须是ComVisible(或整个程序集必须是ComVisible),并且必须在.dna文件中将ExternalLibrary标记为ComServer ='true',例如:

<DnaLibrary RuntimeVersion='v4.0' />
  <ExternalLibrary Path='MyAddIn.dll' ComServer='true' />
</DnaLibrary>

Then you have some options for registration: either call "Regsvr32.exe MyAddInDna.xll" or in the AutoOpen of your add-in, call ExcelDna.Integration.ComServer.RegisterServer() 然后,您有一些注册选项:调用“ Regsvr32.exe MyAddInDna.xll”或在外接程序的自动打开中,调用ExcelDna.Integration.ComServer.RegisterServer()

Both of these set the registry entries so that the .xll file becomes the COM server for your classes. 这两个设置注册表项,以便.xll文件成为您的类的COM服务器。 You can then access them late-bound from VBA, with CreateObject("MyNameSpace.MyClass"). 然后,您可以使用CreateObject(“ MyNameSpace.MyClass”)从VBA后期访问它们。

You need another step to set up a type library (to give you intellisense in VBA). 您还需要另一步骤来设置类型库(以便在VBA中提供智能感知)。 For this you generate the type library with tlbexp.exe. 为此,使用tlbexp.exe生成类型库。 The regsvr32 / ComServer.RegisterServer call will find the type library and register it too. regsvr32 / ComServer.RegisterServer调用将找到类型库并进行注册。

You can also pack the .dll into your .xll file (adding a pack='true' attribute to the ExternalLibrary tag) and the type library will also be packed if present, and registered from the resource in the .xll file when you call Regsvr32.exe 您还可以将.dll打包到您的.xll文件中(向ExternalLibrary标记添加pack ='true'属性),类型库也将打包(如果存在),并在调用时从.xll文件中的资源中注册。 Regsvr32.exe

So the end result could be a single file .xll add-in that is both an Excel Add-In with UDFs, ribbons, RTD servers and also a COM server that you can access from VBA. 因此,最终结果可能是单个文件.xll加载项,它既是带有UDF,功能区,RTD服务器的Excel加载项,也是可以从VBA访问的COM服务器。

More info in the discussions here: http://exceldna.codeplex.com/discussions/252721 and here: http://groups.google.com/group/exceldna/browse_frm/thread/4c5a71efbe96d885 . 有关讨论的更多信息,请参见: http : //exceldna.codeplex.com/discussions/252721和此处: http : //groups.google.com/group/exceldna/browse_frm/thread/4c5a71efbe96d885

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

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