1
0

api_windows.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2023 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package winver
  15. import (
  16. "fmt"
  17. "runtime"
  18. "unsafe"
  19. "golang.org/x/sys/windows"
  20. )
  21. const (
  22. _VER_BUILDNUMBER = 0x00000004
  23. _VER_GREATER_EQUAL = 3
  24. _VER_MAJORVERSION = 0x00000002
  25. _VER_MINORVERSION = 0x00000001
  26. _VER_SERVICEPACKMAJOR = 0x00000020
  27. _WIN32_WINNT_VISTA = 0x0600
  28. _WIN32_WINNT_WIN7 = 0x0601
  29. _WIN32_WINNT_WIN8 = 0x0602
  30. _WIN32_WINNT_WINBLUE = 0x0603
  31. _WIN32_WINNT_WINXP = 0x0501
  32. )
  33. type _OSVERSIONINFOEXW struct {
  34. dwOSVersionInfoSize uint32
  35. dwMajorVersion uint32
  36. dwMinorVersion uint32
  37. dwBuildNumber uint32
  38. dwPlatformId uint32
  39. szCSDVersion [128]uint16
  40. wServicePackMajor uint16
  41. wServicePackMinor uint16
  42. wSuiteMask uint16
  43. wProductType byte
  44. wReserved byte
  45. }
  46. func _HIBYTE(wValue uint16) byte {
  47. return byte(wValue >> 8)
  48. }
  49. func _LOBYTE(wValue uint16) byte {
  50. return byte(wValue)
  51. }
  52. var (
  53. kernel32 = windows.NewLazySystemDLL("kernel32.dll")
  54. ntdll = windows.NewLazySystemDLL("ntdll.dll")
  55. procVerSetConditionMask = kernel32.NewProc("VerSetConditionMask")
  56. procRtlVerifyVersionInfo = ntdll.NewProc("RtlVerifyVersionInfo")
  57. )
  58. func _RtlVerifyVersionInfo(versionInfo *_OSVERSIONINFOEXW, typeMask uint32, conditionMask uint64) int32 {
  59. var r uintptr
  60. if unsafe.Sizeof(uintptr(0)) == unsafe.Sizeof(uint64(0)) {
  61. r, _, _ = procRtlVerifyVersionInfo.Call(uintptr(unsafe.Pointer(versionInfo)), uintptr(typeMask), uintptr(conditionMask))
  62. } else {
  63. switch runtime.GOARCH {
  64. case "386":
  65. r, _, _ = procRtlVerifyVersionInfo.Call(uintptr(unsafe.Pointer(versionInfo)), uintptr(typeMask), uintptr(conditionMask), uintptr(conditionMask>>32))
  66. case "arm":
  67. // Adjust the alignment for ARM.
  68. r, _, _ = procRtlVerifyVersionInfo.Call(uintptr(unsafe.Pointer(versionInfo)), uintptr(typeMask), 0, uintptr(conditionMask), uintptr(conditionMask>>32))
  69. default:
  70. panic(fmt.Sprintf("winver: GOARCH=%s is not supported", runtime.GOARCH))
  71. }
  72. }
  73. return int32(r)
  74. }
  75. func _VerSetConditionMask(conditionMask uint64, typeMask uint32, condition byte) uint64 {
  76. if unsafe.Sizeof(uintptr(0)) == unsafe.Sizeof(uint64(0)) {
  77. r, _, _ := procVerSetConditionMask.Call(uintptr(conditionMask), uintptr(typeMask), uintptr(condition))
  78. return uint64(r)
  79. } else {
  80. r1, r2, _ := procVerSetConditionMask.Call(uintptr(conditionMask), uintptr(conditionMask>>32), uintptr(typeMask), uintptr(condition))
  81. return uint64(r1) | (uint64(r2) << 32)
  82. }
  83. }