简体   繁体   中英

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. 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?

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). I am very interested in the solution outlined on this thread:

Programmatically set DLL search path in VBA macro

in which the top upvoted answer (from Panda-34) seems to show a means to use a pure Declare Function rather than object creation. 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).

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. 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. My questions are:

1) In Panda's solution does the dll still need regasm?

2) Is using C# to implement this solution impossible because C# functions are always wrapped in classes? 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. If that is the case, I like C++ better anyway (my machine has csc and not cl so I've been learning some 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...

C# does not support all features of 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. You need Managed C++ for ex. As for my experience, the best way is to use COM friendly c# assembly registered with regasm and the codebase switch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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