如何使.dll可用于C#和Excel VBA

时间:2022-02-12 05:28:38

I have a DLL that I want to use in C# projects as well as Excel VBA. I used regasm.exe to register the DLL and create a TLB . I was now able to reference it in VBA. My problem is, that I would need to my C# apps still can't find the DLL. I would have to add either a copy of the DLL or a .config - file in the folder of each C# app for them to work. I can see the reference in Visual Studio under COM, but get an error when trying to use that reference (something Active-X related, I can look it up if it's important).

我有一个DLL,我想在C#项目和Excel VBA中使用。我使用regasm.exe注册DLL并创建TLB。我现在能够在VBA中引用它。我的问题是,我需要我的C#应用​​程序仍然无法找到DLL。我必须在每个C#应用程序的文件夹中添加DLL的副本或.config - 文件才能使用它们。我可以在COM下看到Visual Studio中的引用,但在尝试使用该引用时会出错(Active-X相关的东西,如果它很重要,我可以查找它)。

So I tried using gacutil.exe (unregistered first with regasm.exe) and registered my DLL in GAC. Now I don't need the copy or .config, my C# apps work again. Excel VBA still needs the registration with regasm.exe though, I can't add a refrence after only using gacutil.exe.

所以我尝试使用gacutil.exe(首先使用regasm.exe取消注册)并在GAC中注册我的DLL。现在我不需要副本或.config,我的C#应用​​程序再次运行。 Excel VBA仍然需要使用regasm.exe进行注册,但是在使用gacutil.exe后我无法添加引用。

Here my question: How do you register a DLL when you want to use it for C# apps AND Excel VBA? Is it possible to use both regasm and gacutil without having any problems?

在这里我的问题:当你想将它用于C#apps和Excel VBA时,如何注册DLL?是否可以同时使用regasm和gacutil而没有任何问题?

2 个解决方案

#1


2  

After running regasm you can then use gacutil /i to install the assembly into the GAC. Once this is done, add a reference to the assembly to your .NET project using the Browse tab. Once the reference is added, locate the reference in the project and view the properties for that reference. Change "Copy Local" to "False".

运行regasm之后,您可以使用gacutil / i将程序集安装到GAC中。完成此操作后,使用“浏览”选项卡将对程序集的引用添加到.NET项目中。添加引用后,在项目中找到引用并查看该引用的属性。将“复制本地”更改为“错误”。

At run time your .NET project will look for the assembly locally and, when it cannot find it, will then check the GAC. Assuming you've run the gacutil command it will then locate it there.

在运行时,您的.NET项目将在本地查找程序集,当它找不到它时,将检查GAC。假设您已经运行了gacutil命令,那么它将在那里找到它。

#2


2  

A somewhat different approach is to incorporate your C# library in an Excel-DNA .xll add-in. This allows you to make the library available to VBA in Excel without registration or requiring admin rights, and the same add-in can include worksheet UDF functions, ribbon customization etc..

一种稍微不同的方法是将您的C#库合并到Excel-DNA .xll加载项中。这允许您在无需注册或需要管理员权限的情况下使库可用于Excel中的VBA,并且相同的加载项可以包括工作表UDF函数,功能区自定义等。

A detailed write-up of exposing C# libraries to Excel VBA via Excel-DNA by Mikael Katajamäki can be found here: http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna-no.html and http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna_16.html.

可以在此处找到通过MikaelKatajamäki通过Excel-DNA将C#库暴露给Excel VBA的详细说明:http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna- no.html和http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna_16.html。

#1


2  

After running regasm you can then use gacutil /i to install the assembly into the GAC. Once this is done, add a reference to the assembly to your .NET project using the Browse tab. Once the reference is added, locate the reference in the project and view the properties for that reference. Change "Copy Local" to "False".

运行regasm之后,您可以使用gacutil / i将程序集安装到GAC中。完成此操作后,使用“浏览”选项卡将对程序集的引用添加到.NET项目中。添加引用后,在项目中找到引用并查看该引用的属性。将“复制本地”更改为“错误”。

At run time your .NET project will look for the assembly locally and, when it cannot find it, will then check the GAC. Assuming you've run the gacutil command it will then locate it there.

在运行时,您的.NET项目将在本地查找程序集,当它找不到它时,将检查GAC。假设您已经运行了gacutil命令,那么它将在那里找到它。

#2


2  

A somewhat different approach is to incorporate your C# library in an Excel-DNA .xll add-in. This allows you to make the library available to VBA in Excel without registration or requiring admin rights, and the same add-in can include worksheet UDF functions, ribbon customization etc..

一种稍微不同的方法是将您的C#库合并到Excel-DNA .xll加载项中。这允许您在无需注册或需要管理员权限的情况下使库可用于Excel中的VBA,并且相同的加载项可以包括工作表UDF函数,功能区自定义等。

A detailed write-up of exposing C# libraries to Excel VBA via Excel-DNA by Mikael Katajamäki can be found here: http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna-no.html and http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna_16.html.

可以在此处找到通过MikaelKatajamäki通过Excel-DNA将C#库暴露给Excel VBA的详细说明:http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna- no.html和http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna_16.html。