如何将 VBA 代码保存到 DLL 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4933997/
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 to save VBA code to DLL library?
提问by Skuta
For security purpose, I want to save my macros from VBA in Excel workbook to DLL library file I'd be calling from the file later.
出于安全目的,我想将 Excel 工作簿中的 VBA 中的宏保存到 DLL 库文件中,稍后我将从该文件中调用。
This way it is possible to deploy any version of my macros to company's PCs but noone would ever see the real code behind it.
这样就可以将我的任何版本的宏部署到公司的 PC 上,但没有人会看到它背后的真正代码。
How is this possible? I've read on converting to C++ and creating DLL. Is there any step-by-step solution for this?
这怎么可能?我已经阅读了转换为 C++ 和创建 DLL 的内容。是否有任何分步解决方案?
采纳答案by Charles Williams
You can protect your VBA, code by converting it to a password-protected XLA and obfuscating the code. but the password security is weak. Its easy to make the XLA work with all Excel versions.
您可以通过将 VBA 代码转换为受密码保护的 XLA 并混淆代码来保护您的 VBA 代码。但密码安全性较弱。很容易使 XLA 与所有 Excel 版本一起使用。
An easy alternative is to convert to a compiled VB6 Automation addin. Security is good, its easy to convert, it will handle both UDFs and Command addins. But it won't work with Excel 2010-64 or early versions of Excel like XL 2000. You will need a VB6 compiler, which can be hard to find.
一个简单的替代方法是转换为已编译的 VB6 自动化插件。安全性好,易于转换,它可以处理 UDF 和命令插件。但它不适用于 Excel 2010-64 或早期版本的 Excel(如 XL 2000)。您将需要一个 VB6 编译器,这可能很难找到。
A harder alternative is to convert to .NET. If you use one of the many obfuscators then security is reasonable. Performance will be poor unless you use one of the tools that allow .NET access via the XLL interface, such as Excel DNA or Addin Express. 64-bit capability is also available.
更难的选择是转换为 .NET。如果您使用众多混淆器之一,那么安全性是合理的。除非您使用允许通过 XLL 接口访问 .NET 的工具之一,例如 Excel DNA 或 Addin Express,否则性能会很差。还提供 64 位功能。
If performance and security is critical you would need to convert to a C++ XLL, but this is hard work when coming from VBA.
如果性能和安全性至关重要,您将需要转换为 C++ XLL,但如果来自 VBA,这是一项艰巨的工作。
回答by Chris Rae
First off, let me warn you against this in general. We did this once with a corporate app and the time it took to port was approximately the same as the time it took to write the app originally. A step-by-step guide won't exist for this because you're basically rewriting the app from scratch.
首先,让我警告你不要这样做。我们曾用一个公司应用程序做过一次,移植所用的时间与最初编写应用程序所用的时间大致相同。不存在针对此的分步指南,因为您基本上是从头开始重写应用程序。
Secondly, not everything your Excel VBA can do is necessarily available in COM, or XLL. The events available are different; in the case of XLL there are entirely new structures to use for all of the data types. In COM there are no UDFs; in XLL there are no macro actions. You will no longer be able to attach code to worksheets (only Excel sessions) and the install procedures are much more complex. I would spent a long time researching what your VBA does exactly before embarking down one particular path.
其次,并非您的 Excel VBA 可以执行的所有操作都必须在 COM 或 XLL 中可用。可用的事件不同;在 XLL 的情况下,所有数据类型都有全新的结构。在 COM 中没有 UDF;在 XLL 中没有宏操作。您将无法再将代码附加到工作表(仅限 Excel 会话),并且安装过程要复杂得多。在走上一条特定的道路之前,我会花很长时间研究你的 VBA 究竟做了什么。
It depends how large your app is, but I would encourage you to invest the money in getting watertight licenses drawn up and doing some basic code obfuscation (as per the original comments) rather than starting from scratch with a different architecture.
这取决于您的应用程序有多大,但我鼓励您投资于制定防水许可证并进行一些基本的代码混淆(根据原始评论),而不是从头开始使用不同的架构。
回答by Patrick Honorez
You did not mention your target Excel version, but I will give a general view.
你没有提到你的目标 Excel 版本,但我会给出一个总体看法。
- Make sure your code is appropriate for an AddIn. E.g: instead of ThisWorkBook, use ActiveWorkbook.
- Also decide if you need to create a menu or use shortcut keys, and modify your code accordingly, and protect it.
- In VBE, change to IsAddin property to True.
- "Save As" your file and select Excel add-in. Alternatively, just save it and rename the extension from .xls to .xla
- Now either inform your users to "open" that .xla, or reference it in your projects, or put it in the autostart folder or....
- You're done
- 确保您的代码适用于插件。例如:使用 ActiveWorkbook 代替 ThisWorkBook。
- 还要决定是否需要创建菜单或使用快捷键,并相应地修改代码并保护它。
- 在 VBE 中,将 IsAddin 属性更改为 True。
- “另存为”您的文件并选择 Excel 加载项。或者,只需保存它并将扩展名从 .xls 重命名为 .xla
- 现在,要么通知您的用户“打开”该 .xla,要么在您的项目中引用它,或将其放在自动启动文件夹中或...
- 你完成了
http://www.ozgrid.com/VBA/excel-add-in-create.htm
http://excel.tips.net/Pages/T002927_Creating_AddIns.html
http://msdn.microsoft.com/en-us/library/aa140936(v=office.10).aspx
http://www.ozgrid.com/VBA/excel-add-in-create.htm
http://excel.tips.net/Pages/T002927_Creating_AddIns.html
http://msdn.microsoft.com/en-us/图书馆/aa140936(v=office.10).aspx
回答by magallanes
a) You can save your macro in a separate file and protect it with password but, exist program for "recover password" that can break such protection.
a) 您可以将宏保存在单独的文件中并用密码保护它,但是存在可以破坏这种保护的“恢复密码”程序。
b) While is possible to do a c++ dll, is smart to do instead using visual basic 6 because the syntax is pretty similar to vba. In vb6 you can create a COM component, register it and later add to you excel sheet.
b) 虽然可以做一个 c++ dll,但使用 Visual Basic 6 是明智的,因为语法与 vba 非常相似。在 vb6 中,您可以创建一个 COM 组件,注册它,然后添加到您的 Excel 表中。
回答by NikoDeem
Converting VBA code to C++ is a pretty arduous process, since C++ and VBA are very different languages, but such a conversion with compilation to a DLL file really does provide the best VBA code protection out there.
将 VBA 代码转换为 C++ 是一个相当艰巨的过程,因为 C++ 和 VBA 是非常不同的语言,但是这种编译为 DLL 文件的转换确实提供了最好的 VBA 代码保护。
If you want a tool that does exactly that, I would recommend the VBA Compiler for Excel. It converts your VBA code to C and compiles that C code into a DLL file.
如果你想要一个完全做到这一点的工具,我会推荐VBA Compiler for Excel。它将您的 VBA 代码转换为 C 并将该 C 代码编译成一个 DLL 文件。
It's easier and faster than converting your VBA code to C by hand. VBA Compiler website has a simple 10-step guide on how to compile VBA code into a Windows DLL file https://vbacompiler.com/how-to-compile/and it doesn't even require any knowledge of C or C++.
这比手动将 VBA 代码转换为 C 更容易、更快捷。VBA 编译器网站有一个简单的 10 步指南,介绍如何将 VBA 代码编译成 Windows DLL 文件https://vbacompiler.com/how-to-compile/,它甚至不需要任何 C 或 C++ 知识。