插件窝 干货文章 Windows XP下运行Go程序报错“Failed to find GetTickCount64 procedure in kernel32”怎么办?

Windows XP下运行Go程序报错“Failed to find GetTickCount64 procedure in kernel32”怎么办?

GetTickCount Windows 使用 GetTickCount64 920    来源:    2025-03-13

在Windows XP下运行Go程序时,报错“Failed to find GetTickCount64 procedure in kernel32”是因为GetTickCount64函数在Windows XP中不可用。GetTickCount64是Windows Vista及更高版本中引入的API函数,用于获取系统启动以来的毫秒数。Windows XP只支持GetTickCount,它返回一个32位的毫秒数,大约每49.7天会溢出一次。

解决方案

  1. 使用GetTickCount代替GetTickCount64: 如果你有Go程序的源代码,可以修改代码,使用GetTickCount代替GetTickCount64GetTickCount在Windows XP中是可用的。

    例如,如果你在Go代码中直接调用了GetTickCount64,可以将其替换为GetTickCount

    package main
    
    import (
       "syscall"
       "fmt"
    )
    
    func main() {
       kernel32 := syscall.NewLazyDLL("kernel32.dll")
       getTickCount := kernel32.NewProc("GetTickCount")
       ret, _, _ := getTickCount.Call()
       fmt.Printf("Tick count: %d\n", ret)
    }
    
  2. 使用条件编译: 如果你希望代码在Windows XP和更高版本的Windows上都能运行,可以使用条件编译来根据操作系统版本选择调用GetTickCountGetTickCount64

    例如:

    package main
    
    import (
       "syscall"
       "fmt"
       "runtime"
    )
    
    func main() {
       kernel32 := syscall.NewLazyDLL("kernel32.dll")
       var tickCount uint64
    
       if runtime.GOOS == "windows" {
           // Check if Windows version is XP or earlier
           if isWindowsXPOrEarlier() {
               getTickCount := kernel32.NewProc("GetTickCount")
               ret, _, _ := getTickCount.Call()
               tickCount = uint64(ret)
           } else {
               getTickCount64 := kernel32.NewProc("GetTickCount64")
               ret, _, _ := getTickCount64.Call()
               tickCount = uint64(ret)
           }
       }
    
       fmt.Printf("Tick count: %d\n", tickCount)
    }
    
    func isWindowsXPOrEarlier() bool {
       // Implement logic to check if the OS is Windows XP or earlier
       // For example, you can use the `GetVersion` API to determine the OS version
       return true // Placeholder, implement actual logic
    }
    
  3. 升级操作系统: 如果可能的话,考虑将操作系统升级到Windows Vista或更高版本。Windows XP已经停止支持,可能存在安全风险,并且许多现代软件和库不再支持Windows XP。

  4. 使用兼容性库: 如果你无法修改源代码,可以尝试使用兼容性库或工具来模拟GetTickCount64的行为。不过,这种方法可能会引入额外的复杂性和潜在的性能问题。

总结

在Windows XP下运行Go程序时遇到“Failed to find GetTickCount64 procedure in kernel32”错误,主要是因为GetTickCount64函数在Windows XP中不可用。可以通过使用GetTickCount代替GetTickCount64、使用条件编译、升级操作系统或使用兼容性库来解决这个问题。