赞 | 451 |
VIP | 56 |
好人卡 | 75 |
积分 | 423 |
经验 | 124650 |
最后登录 | 2024-11-15 |
在线时间 | 7599 小时 |
Lv5.捕梦者 (管理员) 老黄鸡
- 梦石
- 0
- 星屑
- 42344
- 在线时间
- 7599 小时
- 注册时间
- 2009-7-6
- 帖子
- 13506
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 fux2 于 2013-12-15 14:21 编辑
最近看到有同学求,于是写了这个
感谢@orzfly @EngShun 的帮助。
Windows7及以上系统需要以管理员身份运行
范例程序内自带权限申请。
↓这是一个用于测试的范例
获取机器码.rar
(574.96 KB, 下载次数: 303)
↑这是一个用于测试的范例
2013年12月14日22:25:26
修复了一个在1.9.3 ruby版本以下结果会返回nil的BUG
调用的时候用
testid = Fux2::SystemInfo.GetCPUid p testid msgbox testid if defined?(msgbox)
testid = Fux2::SystemInfo.GetCPUid
p testid
msgbox testid if defined?(msgbox)
即可获得本机第一个可用硬盘的SerialNumber
以下是代码
理论上插到任何地方都没问题。- # 获取本机硬盘序列 by fux2
- # 感谢orzfly
- # 感谢EngShun
- module Fux2
- module SystemInfo
- VER_PLATFORM_WIN32_NT = 0x2
- MAX_DRIVER = 8
- GENERIC_READ = 0x80000000
- GENERIC_WRITE = 0x40000000
- FILE_SHARE_READ = 0x1
- FILE_SHARE_WRITE = 0x2
- OPEN_EXISTING = 0x3
- CREATE_NEW = 0x1
- INVALID_HANDLE_VALUE = -0x1
- DFP_GET_VERSION = 0x74080
- DFP_SEND_DRIVE_COMMAND = 0x7C084
- DFP_RECEIVE_DRIVE_DATA = 0x7C088
- SMART_ENABLE_SMART_OPERATIONS = 0xD8
- SMART_CYL_LOW = 0x4F
- SMART_CYL_HI = 0xC2
- IDE_EXECUTE_SMART_FUNCTION = 0xB0
- IDE_ID_FUNCTION = 0xEC
- IDENTIFY_BUFFER_SIZE = 512
- OUTPUT_DATA_SIZE = IDENTIFY_BUFFER_SIZE + 16
- GetVersion = Win32API.new('kernel32','GetVersionExA','p','l')
- CreateFile = Win32API.new('kernel32','CreateFileA','plllpll','l')
- CloseHandle = Win32API.new('kernel32','CloseHandle','p','l')
- DeviceIoControl = Win32API.new('kernel32','DeviceIoControl','plplplpp','l')
- GetLastError = Win32API.new('kernel32','GetLastError','v','l')
- CopyMemory = Win32API.new('kernel32','RtlMoveMemory','ppl','v')
-
- def self.IsWinNT4Plus
- buffer = [148].pack("l") + "\0" * 144
- if GetVersion.call(buffer) == 1
- if buffer.unpack("c*")[16] == VER_PLATFORM_WIN32_NT && buffer[4,4].unpack("l")[0] >= 4
- return true
- else
- return false
- end
- end
- raise "error"
- end
-
- def self.SmartGetVersion(hDrive)
- buffer = "\0" * 4
- gvop = "\0" * 24
- ret = DeviceIoControl.call(hDrive,DFP_GET_VERSION,nil,0,gvop,24,[0].pack("l"),nil)
- return ret == 0 ? false : true
- end
-
- def self.SmartOpen(drvNumber)
- ret = INVALID_HANDLE_VALUE
- if self.IsWinNT4Plus
- ret = CreateFile.call("\\\\.\\PhysicalDrive"+drvNumber.to_s,
- [GENERIC_READ | GENERIC_WRITE].pack("l").unpack("l")[0],
- [FILE_SHARE_READ | FILE_SHARE_WRITE].pack("l").unpack("l")[0],
- 0,OPEN_EXISTING,0,0)
-
- else
- ret = CreateFile.call("\\\\.\\SMARTVSD",0,0,0,CREATE_NEW,0,0)
- end
- return ret
- end
-
- def self.SmartCheckEnabled(hDrive,drvNumber)
- scip = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- scop = "\0" * 20
- scip[1] = SMART_ENABLE_SMART_OPERATIONS
- scip[2] = 1
- scip[3] = 1
- scip[4] = SMART_CYL_LOW
- scip[5] = SMART_CYL_HI
- scip[6] = 0xA0
- unless self.IsWinNT4Plus
- scip[6] = scip[6] | ((drvNumber & 1) * 16)
- end
- scip[7] = IDE_EXECUTE_SMART_FUNCTION
- scip[9] = drvNumber
- scip = scip.pack("lcccccccccccclllll")
- ipsize = 0
- scip.each_byte{ipsize+=1}
- opsize = 0
- scop.each_byte{opsize+=1}
- ret = DeviceIoControl.call( hDrive,
- DFP_SEND_DRIVE_COMMAND,
- scip,
- ipsize - 4,
- scop,
- opsize - 4,
- [0].pack("l"),
- nil)
- return ret == 0 ? false : true
- end
-
- def self.IdentifyDrive(hDrive,idCmd,drvNumber)
- scip = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- idsec = "\0" * 256
- bArrOut = "\0" * OUTPUT_DATA_SIZE
- scip[0] = IDENTIFY_BUFFER_SIZE
- scip[9] = drvNumber
-
- scip[1] = 0
- scip[2] = 1
- scip[3] = 1
- scip[4] = 0
- scip[5] = 0
- scip[6] = 0xA0
- unless self.IsWinNT4Plus
- scip[6] = scip[6] | ((drvNumber & 1) * 16)
- end
- scip[7] = idCmd
- scip = scip.pack("lcccccccccccclllll")
- ipsize = 0
- scip.each_byte{ipsize+=1}
- ret = DeviceIoControl.call( hDrive,
- DFP_RECEIVE_DRIVE_DATA,
- scip,
- ipsize - 4,
- bArrOut,
- OUTPUT_DATA_SIZE,
- [0].pack("l"),
- nil)
- if ret != 0
- CopyMemory.call(idsec, bArrOut, 256)
- serialNumber = self.SwapBytes(idsec[36,20])
- return serialNumber
- end
- return false
- end
-
- def self.SwapBytes(b)
- b = b.unpack("C*")
- b.each_index do |i|
- next if i%2!=0
- b[i],b[i+1] = b[i+1],b[i]
- end
- return b.pack("C*")
- end
-
- def self.GetDriveInfo(drvNumber)
- hDrive = self.SmartOpen(drvNumber)
- if hDrive != INVALID_HANDLE_VALUE
- if self.SmartGetVersion(hDrive)
- if self.SmartCheckEnabled(hDrive,drvNumber)
- ret = self.IdentifyDrive(hDrive, IDE_ID_FUNCTION, drvNumber)
- if ret
- return ret
- end
- end
- end
- end
- return nil
- raise "error"
- end
- def self.GetCPUid
- cpuid = nil
- MAX_DRIVER.times do |drvNumber|
- cpuid = self.GetDriveInfo(drvNumber)
- if cpuid
- break
- end
- end
- return cpuid
- end
- end
- end
复制代码 |
评分
-
查看全部评分
|