windows 如何在与excel文件相同的目录中使用dll
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971296/
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 use dll's in the same directory as an excel file
提问by Rook
This is somewhat related to my other question.
这与我的另一个问题有些相关。
I've been using a dll to acompany an excel spreadsheet. Everything is currently working with the dll and excel is using it just fine. But is it possible to specify that a dll resides in the same directory as the excel file when declaring functions?
我一直在使用 dll 来制作 excel 电子表格。目前一切都在使用 dll 并且 excel 使用它就好了。但是在声明函数时是否可以指定dll与excel文件位于同一目录中?
Declare Sub FortranCall Lib "Fcall.dll" (r1 As Long, ByVal num As String)
Unfortunetly this doesn't work, I have to use something like:
不幸的是,这不起作用,我必须使用类似的东西:
Declare Sub FortranCall Lib "C:\temp\Fcall.dll" (r1 As Long, ByVal num As String)
This works, but is going to cause headaches when distributing to my office mates. Placing the dll in c:\windows\system32 etc. is not really an option either.
这有效,但在分发给我的办公室伙伴时会引起头痛。将 dll 放在 c:\windows\system32 等中也不是真正的选择。
采纳答案by ewbi
Here are three possibilitiesfor dynamically loading/calling into DLLs from VBA, including links to relevant info and some sample code. Can't say I've ever had to use any of the solutions described there, but it seems like a reasonable exploration of the options in light of VBA's need for a static path.
以下是从 VBA 动态加载/调用 DLL 的三种可能性,包括指向相关信息和一些示例代码的链接。不能说我曾经不得不使用那里描述的任何解决方案,但鉴于 VBA 需要静态路径,这似乎是对选项的合理探索。
- Create a new module at runtime (you could import a .bas file from disk, no need to hard-code the module with string literals), using the VBIDE Extensibility API. Drawback: no compile-time validation; you'll need to use stringly-typed
Application.Run
calls to invoke it. Requires trusted programmatic access to the VBIDE API (i.e. you allow VBA to execute code that generates code that is then executed... like macro viruses do). - Use the
LoadLibrary
Win32 API... and now you've got pointers and addresses: this scary code(.zip download) is essentially a huge unmaintainable hack that uses assembly language to enable invoking the API functions by name. Looks like it only works for a subset of supported Win32 API functions though. - Change the DLL search path, but then that alsorequires dynamic code added at run-time, so might as well go with the above.
- 使用 VBIDE 扩展性 API 在运行时创建一个新模块(您可以从磁盘导入 .bas 文件,无需使用字符串文字对模块进行硬编码)。缺点:没有编译时验证;您需要使用字符串类型的
Application.Run
调用来调用它。需要对 VBIDE API 的可信编程访问(即您允许 VBA 执行生成代码的代码,然后执行该代码......就像宏病毒所做的那样)。 - 使用
LoadLibrary
Win32 API... 现在你已经有了指针和地址:这个可怕的代码(.zip 下载)本质上是一个巨大的无法维护的黑客,它使用汇编语言来启用按名称调用 API 函数。看起来它只适用于受支持的 Win32 API 函数的子集。 - 更改 DLL 搜索路径,但这也需要在运行时添加动态代码,因此不妨采用上述方法。
Here's another potential solutionthat suggests programmatically updating the PATH environment variable prior to calling into your DLL. Not a bad idea, if it works, as you could add this to you workbook open event.
这是另一个潜在的解决方案,它建议在调用 DLL 之前以编程方式更新 PATH 环境变量。不错的主意,如果可行,因为您可以将其添加到您的工作簿打开事件中。
Good luck!
祝你好运!
回答by Ricardo Affinito
The way I generally take care of this is by adding:
我通常处理这个问题的方法是添加:
Dim CurrentPath As String
CurrentPath = CurDir()
ChDir (ThisWorkbook.Path)
To: Private Sub Workbook_Open()
至:私有子 Workbook_Open()
回答by Rusty
ChDir() should do the trick. It might not work on network folders though.
ChDir() 应该可以解决问题。不过,它可能不适用于网络文件夹。
Declare Sub FortranCall Lib "Fcall.dll" (r1 As Long, ByVal num As String)
...
Dim CurrentPath As String
CurrentPath = CurDir()
ChDir (ThisWorkbook.Path)
Call FortranCall(r, n)
ChDir (CurrentPath) ' Change back to original directory
Now keep your .dll in the same folder as your workbook.
现在将您的 .dll 保存在与您的工作簿相同的文件夹中。
回答by Assaf Lavie
You can put the DLL in some directory and add it to the path EnVar.
您可以将 DLL 放在某个目录中并将其添加到路径 EnVar。
回答by Ken Paul
ActiveWorkbook.Path
gives you the full path to the folder containing the currently-active workbook. So try this:
ActiveWorkbook.Path
为您提供包含当前活动工作簿的文件夹的完整路径。所以试试这个:
Declare Sub FortranCall Lib ActiveWorkbook.Path & "\Fcall.dll" (r1 As Long, ByVal num As String)