在Excel VBA中将Windows API调用转换为64位 [英] Convert Windows API call to 64-bit in Excel VBA
问题描述
我正在32位Excel中使用此Windows API调用,并且工作正常:
I'm using this Windows API call in 32-bit Excel, and it's working fine:
Declare Function WNetGetConnection Lib "MPR.DLL" _
Alias "WNetGetConnectionA" ( _
ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
lSize As Long) As Long
有人告诉我,我们公司的一小部分用户很快就会使用64位Excel.我没有使用64位Excel的权限,也找不到附近的同事.
I'm told that a small percentage of our corporate users will soon be going to 64-bit Excel. I don't have access to 64-bit Excel and I haven't been able to find any coworkers nearby who do.
根据此文件,如果我没看错的话...
According to this file, if I'm reading it correctly...
http://www.cadsharp.com/docs/Win32API_PtrSafe.txt
...对于64位Excel,这是该函数的正确语法:
...this is the correct syntax of that function for 64-bit Excel:
Declare PtrSafe Function WNetGetConnection Lib "MPR.DLL" _
Alias "WNetGetConnectionA" ( _
ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
lSize As Long) As Long
但这是正确的吗?唯一的区别是添加了PtrSafe.它是否应该指向MPR.DLL的其他64位版本?另外,lSize应该是LongLong而不是Long吗?
But is that correct? The only difference is adding PtrSafe. Should it point to a different, 64-bit version of MPR.DLL? Also, should lSize be LongLong rather than Long?
推荐答案
但这是正确的吗?
But is that correct?
是的.但是,请不要相信我.获取Office的副本并进行测试.
Yes. But don't take my word for it. Get a copy of Office and test it.
唯一的区别是添加了PtrSafe.它是否应该指向MPR.DLL的其他64位版本?
The only difference is adding PtrSafe. Should it point to a different, 64-bit version of MPR.DLL?
不.Windows为您的进程选择适当的DLL.对于标准WinAPI组件,您应该只使用DLL名称,而不能使用完整路径.Windows将选择正确的一个.
No. Windows chooses the appropriate DLL for the bitness of your process. For standard WinAPI components, you should only use the DLL name, and never a full path. Windows will pick the correct one.
还,lSize应该是LongLong而不是Long吗?
Also, should lSize be LongLong rather than Long?
应为 ByRef Long
. WNetGetConnection
文档显示函数原型:
It should be ByRef Long
. The WNetGetConnection
documentation shows the function prototype:
DWORD WNetGetConnection(
_In_ LPCTSTR lpLocalName,
_Out_ LPTSTR lpRemoteName,
_Inout_ LPDWORD lpnLength
);
DWORD
是一个32位无符号整数. 1 LPDWORD
是指向 DWORD
的指针,在VB(6/A)中它是 ByReflpnLength As Long
.
DWORD
is a 32 bit unsigned integer.1 LPDWORD
is a pointer to a DWORD
, which in VB(6/A) is ByRef lpnLength As Long
.
作为奖励:
- 在Windows API中,
LONG
也是32位.无论您运行的是32位还是64位,这都不会改变.(sizeof(DWORD)== sizeof(LONG)== sizeof(INT)
) - VBA数据类型
- In the Windows API
LONG
is also 32 bit. This does not change whether you're running 32 or 64 bit. (sizeof(DWORD) == sizeof(LONG) == sizeof(INT)
) - VBA Data Types
1 有趣的事实:VBA没有无符号整数类型,因为VB6没有它们.VB6于98年问世.
1Fun Fact: VBA doesn't have unsigned integer types, since VB6 didn't have them. VB6 came out in '98.
截至2016年10月7日,Office 2016的全新从未使用过的单用户许可证的价格约为150美元.也可以通过MSDN订阅程序获得.许多高级零售商也提供该产品.(我故意不发布链接.)
这篇关于在Excel VBA中将Windows API调用转换为64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!