如何使代码兼容 32 位和 64 位

Posted

技术标签:

【中文标题】如何使代码兼容 32 位和 64 位【英文标题】:How to make code compatible with 32bit and 64bit 【发布时间】:2021-02-08 17:33:42 【问题描述】:

我需要以下内容才能兼容 32 位和 64 位。

我知道我需要使用 PtrSafe 并将 long 更改为 long ptr,但不知道我在做什么。

Option Explicit

Public Declare Function TzSpecificLocalTimeToSystemTime _
               Lib "Kernel32.dll" (ByRef lpTimeZone As TIME_ZONE_INFORMATION, _
                                   ByRef lpLocalTime As SYSTEMTIME, _
                                   ByRef lpUniversalTime As SYSTEMTIME) _
                                   As Long

Public Type TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(31) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(31) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
End Type

Private mudtTZI As TIME_ZONE_INFORMATION


Public Function LocalTimeToUTC(utcTime As Date) As Date
    Dim localTime As Date
    Dim dteFileTime As FILETIME
    Dim dteLocalSystemTime As SYSTEMTIME
    Dim dteSystemTime As SYSTEMTIME

End Function

【问题讨论】:

【参考方案1】:

为 x86 或 x64 机器动态创建特定声明。有关上下文,请参阅here。像这样的:

#If VBA7 Then
Private Declare PtrSafe Function TzSpecificLocalTimeToSystemTime _
               Lib "Kernel32.dll" (ByRef lpTimeZone As TIME_ZONE_INFORMATION, _
                                   ByRef lpLocalTime As SYSTEMTIME, _
                                   ByRef lpUniversalTime As SYSTEMTIME) _
                                   As Long
#Else
Public Declare Function TzSpecificLocalTimeToSystemTime _
               Lib "Kernel32.dll" (ByRef lpTimeZone As TIME_ZONE_INFORMATION, _
                                   ByRef lpLocalTime As SYSTEMTIME, _
                                   ByRef lpUniversalTime As SYSTEMTIME) _
                                   As Long
#End If

【讨论】:

以上是关于如何使代码兼容 32 位和 64 位的主要内容,如果未能解决你的问题,请参考以下文章

为 Windows 编写跨平台(32 位和 64 位兼容)程序(如 .NET 中的 AnyCPU)

如何在我的应用程序中包含 64 位和 32 位本机代码

32位和64位的区别

如何同时支持32位和64位组件自动安装

office32位和64位的区别,用户如何选择

sqlite 32位和64位数据库语句识别的区别