VBA 代码无法访问 System32 中的 DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6069167/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:16:25  来源:igfitidea点击:

VBA code can't access to DLL in System32

windowsvbawindows-7

提问by sfsn

My VBA code can't access to my DLL in the C:\Windows\System32 folder on Windows 7 64bit and Word 2010 32bit.

我的 VBA 代码无法访问 Windows 7 64 位和 Word 2010 32 位上 C:\Windows\System32 文件夹中的 DLL。

Private Declare Function my_func Lib "mydll.dll" (ByVal param As String) As Long

Public Sub MyFuncTest
  n = my_func("a")
End

I copied mydll.dll into C:\Windows\System32 and called MyFuncTest but got an error message like "Error 53: 'mydll.dll' not found".

我将 mydll.dll 复制到 C:\Windows\System32 并调用了 MyFuncTest,但收到一条错误消息,如“错误 53:'mydll.dll' 未找到”。

However, I changed the declaration in code to:

但是,我将代码中的声明更改为:

Private Declare Function my_func Lib "C:\Users\myname\Documents\mydll.dll" (ByVal param As String) As Long

then I copied mydll.dll into C:\Users\myname\Documents, my account's Documents folder, and MyFuncTest successfully executed.

然后我将 mydll.dll 复制到 C:\Users\myname\Documents,我的帐户的 Documents 文件夹中,并且 MyFuncTest 成功执行。

Changing "mydll.dll" to "C:\Windows\System32\mydll.dll" in the declaration did not work. And I tried accessing C:\Windows\System32\mydll.dll with FileSystemObject#FileExists method on the same VBA environment, but it returned False (not found).

在声明中将“mydll.dll”更改为“C:\Windows\System32\mydll.dll”不起作用。我尝试在同一 VBA 环境中使用 FileSystemObject#FileExists 方法访问 C:\Windows\System32\mydll.dll,但它返回 False(未找到)。

There was no problem on Windows XP and Word 2003.

在 Windows XP 和 Word 2003 上没有问题。

Can anyone help?

任何人都可以帮忙吗?

回答by David Heffernan

This is a 32 bit DLL and a 32 bit process running in the WOW64 emulator on 64 bit Windows. File redirectionis in play and so when a 32 bit process looks in system32it is actually redirected to the 32 bit system directory SysWOW64.

这是在 64 位 Windows 上的 WOW64 模拟器中运行的 32 位 DLL 和 32 位进程。文件重定向正在发挥作用,因此当 32 位进程查找时,system32它实际上被重定向到 32 位系统目录SysWOW64

The simple and quick solution is to move the DLL to C:\Windows\SysWOW64. However, as Cody Gray points out in a comment, it is not recommended for you to place application DLLs in the system directory. Normal practice is to place the DLLs in your application folder in the program files directory and make sure that folder is in the DLL search path when the DLL needs to be loaded.

简单快捷的解决方案是将 DLL 移动到C:\Windows\SysWOW64. 但是,正如 Cody Gray 在评论中指出的那样,不建议您将应用程序 DLL 放在系统目录中。通常的做法是将 DLL 放在程序文件目录中的应用程序文件夹中,并确保在需要加载 DLL 时该文件夹位于 DLL 搜索路径中。

回答by Anuraj

Seems like UAC is the problem. Try running the VBA script as Administrator. It may help you.

似乎 UAC 是问题所在。尝试以管理员身份运行 VBA 脚本。它可能会帮助你。

回答by osknows

A recent developer suggested the following fix which allowed a 32bit .dll to be registered on a 64 bit machine

最近的开发人员建议了以下修复程序,该修复程序允许在 64 位计算机上注册 32 位 .dll

1) Open a DOS command window.

1) 打开 DOS 命令窗口。

2) Navigate to C:\Windows\Microsoft.NET\Framework64\v2.0.50727

2) 导航到 C:\Windows\Microsoft.NET\Framework64\v2.0.50727

3) Enter the following and press enter. regasm /codebase "C:\Users\myname\Documents\mydll.dll"

3) 输入以下内容并按回车键。regasm /codebase "C:\Users\myname\Documents\mydll.dll"

回答by Hervé

I have had exactly the same problem yesterday. The program run on my machine, but not on others. In fact, the message fron Excel is wrong. He obviously find lhe dll file, but this dll is calling orher dll, missing in the system : MSVCR100D.dlland NTDLL.dll.I found that by using Dependency Walker free software, able to examine which dll are call by a dll.

我昨天遇到了完全相同的问题。该程序在我的机器上运行,但不在其他机器上运行。事实上,来自 Excel 的消息是错误的。他显然找到LHE dll文件,但是这个DLL是调用DLL椅子中,在系统中丢失:MSVCR100D.dllNTDLL.dll.我发现,通过使用的Dependency Walker的免费软件,能检查哪些动态库由DLL调用。