我需要从电子表格中运行的64位VBA代码获取Excel 2013 x64窗口句柄 . 有几个选项可以做到这一点:
-
阅读
Application.Hwnd
(MSDN Application.Hwnd Property (Excel)) -
调用从user32导入的FindWindow,例如如在此接受的答案中所述:What are the differences between VBA 6.0 and VBA 7.0?:
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
问题是 Application.Hwnd
返回 Long
,即32位(我在64位环境中用 MsgBox TypeName(Application.Hwnd)
验证了这一点),而 FindWindow
返回 LongPtr
,在Office x64中为64位长 .
这是否意味着无法信任 Application.Hwnd
属性在64位环境中始终是正确的?
1 回答
不,这不是真的 .
LongPtr
只是一种可变数据类型,在32位版本上是4字节数据类型,在64位版本的Office 2010上是8字节数据类型 .你可以阅读更多关于
LongPtr
Here的信息如果上述链接死亡......
LongPtr
(32位系统上的长整数,64位系统上的LongLong整数)变量存储为32位系统上的-2,147,483,648 to 2,147,483,647
值的带符号32位(4字节)数字;并且在64位系统上签名为64位(8字节)的数字,范围为-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
.Note
LongPtr
不是真正的数据类型,因为它在32位环境中转换为Long,在64位环境中转换为LongLong
. 使用LongPtr
可以写入可以在32位和64位环境中运行的 portable code . 使用LongPtr
作为指针和句柄 .Suggested for further Reading :
Compatibility Between the 32-bit and 64-bit Versions of Office 2010
以下链接精美地解释了它 . Interprocess Communication Between 32-bit and 64-bit Applications