这个指针类型防水吗?

2024-02-29

我正在尝试设计一种自定义类型,可以在需要窗口句柄或其他类型指针的 API 中使用,并且适用于 VBA 可以运行的所有系统。这是我所得到的:

#If (Win64 = 1) And (VBA7 = 0) Then
    Public Type LongLong '64-bit systems pre-VBA7 wouldn't have had LongLong
        LoPart As Long
        HiPart As Long
    End Type
#End If

Public Type Pointer 'could alternatively make a LongPtr type for pre VBA7 systems only
    #If VBA7 Then
        Address As LongPtr 'should always be correct right?
    #ElseIf Win64 Then
        Address As LongLong 'should never exist as VBA6 and lower are 32 or 16 bit
    #ElseIf Win16 Then
        Address As Integer '16 bit address, is this correct?
    #Else
        Address As Long '32 Bit pre-VBA7 system
    #End If
End Type

逻辑是这样的:

  1. Only the bitness of the Office host matters, not of the operating system. Is this definitely correct, I'm not sure?
    • Win64/32/16误导性地指的是 Office 版本(而不是 Windows 版本 - 操作系统 - 正如其名称所暗示的那样)。
  2. VBA7 introduced the LongPtr type which evaluates to LongLong in 64 bit hosts, Long in 32 bit hosts - Mac or Windows it should just work (I don't know what it does on a 16 bit host, but can VBA7 even be run on that?). So that's the first check
    • VBA7 是 VBA 的最后一个版本,因此无需检查是否有比该版本更现代的版本。但出于兴趣,有什么办法吗?
  3. Next I check for bitness of a pre-VBA7 host; I don't think it can ever be 64, but just in case, that requires a custom LongLong type (since that's only defined in VBA7)
    • 有趣的是Win64也适用于 Mac - 这个名字确实具有误导性
  4. 类似的检查发生在 16 位和 32 位上 - 只是它们不需要自定义类型(我假设Integer是在 16 位 VBA 中定义的,并且它是正确使用的数据类型 - 我从未遇到过它,所以我无法真正检查)

现在的一个问题是LongLong类型标记 64 位错误VBA7系统;我认为因为LongLong这些系统上已存在,因此不是类型的有效名称。但是#If Win64 And VBA7 = 0检查应该排除此类系统中的整个定义,所以我真的不知道为什么这是一个问题 - 我问过question https://stackoverflow.com/q/56827029/6609896关于那个。

不管怎样,代码仍然按预期运行;任何类型的变量LongLong只是默认为内置函数而不是 VBA7 中的我的 - 我只是得到Public Type LongLong在编辑器中突出显示红色,这有点痛苦。解决方法是重命名它并避免冲突(但也会改变语义,并且意味着LongLong不能在其他地方使用)。或者将指针重新定义为

Public Type Pointer
    #If VBA7 Then
        Address As LongPtr 'should always be correct right?
    #ElseIf Win64 Then
        AddressLo As Long
        AddressHi As Long
    #ElseIf Win16 Then
        Address As Integer '16 bit
    #Else
        Address As Long '32 Bit
    #End If
End Type

这会稍微改变界面。


那么这种类型适用于所有系统,Mac Windows、32 64 位、VBA7 VBA6 等吗?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

这个指针类型防水吗? 的相关文章

随机推荐