简体   繁体   English

编译一个支持VBA中声明函数的DLL

[英]Compile a dll that supports declare function in VBA

SO newb here so feel free to let me know of any etiquette violations! 所以新手在这里,所以随时让我知道任何礼节违规!

I am new to library compilation and have been researching (mostly on this forum) building libraries that can be referenced from office and other programs supporting vba scripting. 我是图书馆编译的新手,并且一直在研究(主要是在这个论坛上)构建库,这些库可以从办公室和其他支持vba脚本的程序中引用。 I have found a few good answers. 我找到了一些好的答案。 I have successfully executed the solution from this thread: 我已成功执行此线程的解决方案:

A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6? 一个简单的C#DLL - 如何从Excel,Access,VBA,VB6中调用它?

which involves creating an instance of a C# class in VBA and then calling a method of the class (the dll must be 'regasm'ed). 这涉及在VBA中创建一个C#类的实例,然后调用该类的方法(该DLL必须是'regasm'ed)。 I am very interested in the solution outlined on this thread: 我对此主题中概述的解决方案非常感兴趣:

Programmatically set DLL search path in VBA macro 以编程方式在VBA宏中设置DLL搜索路径

in which the top upvoted answer (from Panda-34) seems to show a means to use a pure Declare Function rather than object creation. 其中顶级upvoted答案(来自Panda-34)似乎显示了使用纯声明函数而不是对象创建的方法。 If I understand Panda's answer correctly, this method is able to load a dll into the vba project without setting a reference in tools->references (it employs a nifty trick with ChDir). 如果我正确理解Panda的答案,这个方法可以将一个dll加载到vba项目中,而无需在tools-> references中设置引用(它使用ChDir的一个漂亮的技巧)。

I am able to get the DeclareFunction to pass the vba compilaton, but when execution gets to a line using the function, I get a "Cannot determine entry point" type message. 我能够让DeclareFunction传递vba compilaton,但是当执行使用该函数到达一行时,我得到一个“无法确定入口点”类型的消息。 All the forum solutions to this error involve using regasm to register the classes in the dll and then set a reference so that an instance of the class can be created. 此错误的所有论坛解决方案都涉及使用regasm在dll中注册类,然后设置引用以便可以创建类的实例。 My questions are: 我的问题是:

1) In Panda's solution does the dll still need regasm? 1)在熊猫的解决方案中,dll还需要regasm吗?

2) Is using C# to implement this solution impossible because C# functions are always wrapped in classes? 2)使用C#实现这个解决方案是不可能的,因为C#函数总是包含在类中? I have been starting to wonder whether the problem is that you cannot Declare Function on a method that is not visible at the global level of the dll because it may be ambiguous - Class1 and Class2 in Test.dll may both have a HelloWorld() member. 我一直在开始怀疑问题是你是否无法在dll的全局级别上看不到的方法上声明函数,因为它可能不明确 - Test.dll中的Class1和Class2都可能都有一个HelloWorld()成员。 If that is the case, I like C++ better anyway (my machine has csc and not cl so I've been learning some C#). 如果是这样的话,我更喜欢C ++(我的机器有csc而不是cl,所以我一直在学习一些C#)。

I'm sorry if I should not have started a new thread for this question. 如果我不应该为这个问题开始一个新线程,我很抱歉。 I am brand new to SO, so I don't have permission to comment on Panda's answer, and it specifically says not to ask another question about someone's answer with your answer, so I took that to mean start a new thread... 我是SO的新手,所以我没有权限评论Panda的回答,并且它特别说不要用你的答案提出另一个关于某人回答的问题,所以我认为这意味着开始一个新线程......

C# does not support all features of MSIL. C#不支持MSIL的所有功能。 Global functions is one of them. 全球职能就是其中之一。 So AFAIK you can't have global functions in C#, it's just not part of the language. 所以AFAIK你不能在C#中拥有全局函数,它只是语言的一部分。 You need Managed C++ for ex. 你需要Managed C ++ ex。 As for my experience, the best way is to use COM friendly c# assembly registered with regasm and the codebase switch. 至于我的经验,最好的方法是使用注册了regasm和codebase开关的COM友好c#程序集。

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

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