Created
December 26, 2018 21:19
-
-
Save hymkor/152e35cf5452fc61848e80beb2b0655c to your computer and use it in GitHub Desktop.
Windows のバージョンチェック → しかし、Windows10 は認証されていないコードには Windows 8と名乗ってしまうから意味なかったー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dos | |
import ( | |
"syscall" | |
"unsafe" | |
) | |
const ( | |
verBuildNumber = 0x0000004 | |
verMajorVersion = 0x0000002 | |
verMinorVersion = 0x0000001 | |
verPlatformId = 0x0000008 | |
verProductType = 0x0000080 | |
verServicePackMajor = 0x0000020 | |
verServicePackMinor = 0x0000010 | |
verSuiteName = 0x0000040 | |
verEqual = 1 | |
verGreater = 2 | |
verGreaterEqual = 3 | |
verLess = 4 | |
verLessEqual = 5 | |
errorOldWinVersion syscall.Errno = 1150 | |
) | |
type OSVersionInfoEx struct { | |
osVersionInfoSize uint32 | |
MajorVersion uint32 | |
MinorVersion uint32 | |
buildNumber uint32 | |
platformId uint32 | |
csdVersion [128]uint16 | |
servicePackMajor uint16 | |
servicePackMinor uint16 | |
SuiteMask uint16 | |
productType byte | |
reserve byte | |
} | |
var ( | |
Windows10 = &OSVersionInfoEx{MajorVersion: 10} | |
Windows81 = &OSVersionInfoEx{MajorVersion: 6, MinorVersion: 3} | |
Windows8 = &OSVersionInfoEx{MajorVersion: 6, MinorVersion: 2} | |
Windows7 = &OSVersionInfoEx{MajorVersion: 6, MinorVersion: 1} | |
WindowsVista = &OSVersionInfoEx{MajorVersion: 6} | |
) | |
var procVerSetConditionMask = kernel32.NewProc("VerSetConditionMask") | |
var procVerifyVersionInfo = kernel32.NewProc("VerifyVersionInfoW") | |
func (vi OSVersionInfoEx) Verify() bool { | |
var m1, m2 uintptr | |
var mask uintptr = verMajorVersion | |
m1, m2, _ = procVerSetConditionMask.Call(m1, m2, verMajorVersion, verGreaterEqual) | |
if vi.MinorVersion > 0 { | |
m1, m2, _ = procVerSetConditionMask.Call(m1, m2, verMinorVersion, verGreaterEqual) | |
mask |= verMinorVersion | |
} | |
vi.osVersionInfoSize = uint32(unsafe.Sizeof(vi)) | |
r, _, err := procVerifyVersionInfo.Call( | |
uintptr(unsafe.Pointer(&vi)), | |
mask, | |
m1, m2) | |
println(err.Error()) | |
return r != 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dos_test | |
import ( | |
"github.com/zetamatta/nyagos/dos" | |
"testing" | |
) | |
func TestVerify(t *testing.T) { | |
if dos.Windows10.Verify() { | |
println("Windows10") | |
} else if dos.Windows81.Verify() { | |
println("Windows8.1") | |
} else if dos.Windows8.Verify() { | |
println("Windows8") | |
} else if dos.Windows7.Verify() { | |
println("Windows7") | |
} else if dos.WindowsVista.Verify() { | |
println("WindowsVista") | |
} else { | |
println("Unknown Windows") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment