【发布时间】:2018-03-12 15:31:22
【问题描述】:
我的函数声明已更新以适应 64 位和 32 位 Windows(VBA6 和 VBA7)。我想保留这两个声明,因为我们的一些客户仍在使用 Excel 2007。
问题在于,在打开应用程序时,VBA6 声明(在 Else 语句之后)似乎有时(尽管并非总是)在 Excel 2016(在 Office 365 上测试,64 位)中引发编译器错误,即使它永远不会被阅读。
(编译错误:此项目中的代码必须更新才能在 64 位系统上使用。请查看并更新 Declare 语句并使用 PtrSafe 属性对其进行标记。)
有什么办法可以避免吗?
#If VBA7 Then
Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare PtrSafe Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal
dwProcessId As Long) As Long
#Else
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
#End If
【问题讨论】:
-
have been updated to accommodate both 64-bit and 32-bit Windows- 他们没有。它们已被标记为PtrSafe关键字,但参数类型仍然错误 (Long)。它们会在 x64 上崩溃。 -
关于实际错误,我怀疑是
#else块中的函数。当您标记所有内容PtrSafe时,您可能错过了一个功能。