微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb获取cpuid 硬盘物理序列号和逻辑盘序列号 的方法

一、使用WMI获取

通过WMI获得硬盘和cpu的物理序列号(VB.net)

''获得硬盘序列号

Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_diskDrive")



Dim Uint32 As UInt32

For Each cmicWmiObj As ManagementObject In cmicWmi.Get

Uint32 = cmicWmiObj("signature")

Next

TextBox1.Text = Uint32.ToString





''获得cpu序列号

Dim Wmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Processor")



Dim Uint32 As String

For Each WmiObj As ManagementObject In Wmi.Get

Uint32 = WmiObj("ProcessorId")

Next

TextBox1.Text = Uint32



''获得硬盘总容量

Dim Wmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_diskDrive")



Dim Uint64 As UInt64

For Each WmiObj As ManagementObject In Wmi.Get

Uint64 = WmiObj("size")

Next

TextBox1.Text = Uint64.ToString

首先,引用System.Management;然后在代码中Imports System.Management;

private void GetInfo()
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Response.Write ("cpu序列号:"+cpuInfo.ToString ());
}

//获取硬盘ID
String HDid;
ManagementClass cimobject1 = new ManagementClass("Win32_diskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach(ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
Response.Write ("硬盘序列号:"+HDid.ToString ());
}


//获取网卡硬件地址
9558821702001755616


ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach(ManagementObject mo in moc2)
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address/t{0}"+mo["MacAddress"].ToString());
mo.dispose();
}
}

二、获取物理硬盘的id

Private Const MAX_IDE_DRIVES As Long = 4
Private Const IDENTIFY_BUFFER_SIZE As Long = 512
Private Const DFP_SEND_DRIVE_COMMAND As Long = &H7C084
Private Const DFP_RECEIVE_DRIVE_DATA As Long = &H7C088

Private Type GETVERSIONOUTParaMS
bVersion As Byte ' Binary driver version.
bRevision As Byte ' Binary driver revision.
bReserved As Byte ' Not used.
bIDEDeviceMap As Byte ' Bit map of IDE devices.
fCapabilities As Long ' Bit mask of driver capabilities.
dwReserved(3) As Long ' For future use.
End Type
Private Type IDEREGS
bFeaturesReg As Byte ' Used for specifying SMART "commands".
bSectorCountReg As Byte ' IDE sector count register
bSectorNumberReg As Byte ' IDE sector number register
bCylLowReg As Byte ' IDE low order cylinder value
bCylHighReg As Byte ' IDE high order cylinder value
bDriveHeadReg As Byte ' IDE drive/head register
bCommandReg As Byte ' Actual IDE command.
End Type

Private Type SENDCMDINParaMS
cBufferSize As Long ' Buffer size in bytes
irDriveRegs As IDEREGS ' Structure with drive register values.
bDriveNumber As Byte ' Physical drive number to send
bReserved(2) As Byte ' Reserved for future expansion.
dwReserved(3) As Long ' For future use.
bBuffer(0) As Byte ' Input buffer.
End Type
Private Const IDE_ATAPI_ID As Long = &HA1 ' Returns ID sector for ATAPI.
Private Const IDE_ID_FUNCTION As Long = &HEC ' Returns ID sector for ATA.
Private Const IDE_EXECUTE_SMART_FUNCTION As Long = &HB0 ' Performs SMART cmd.
Private Type DRIVERSTATUS
bReserved(1) As Byte ' Reserved for future expansion.
dwReserved(1) As Long ' Reserved for future expansion.
End Type

Private Type SENDCMDOUTParaMS
cBufferSize As Long ' Size of bBuffer in bytes
drvstatus As DRIVERSTATUS ' Driver status structure.
bBuffer(0) As Byte ' Buffer of arbitrary length in which to store the data read from the ' drive.
End Type


Private Type ATTRTHRESHOLD
bAttrID As Byte ' Identifies which attribute
bWarrantyThreshold As Byte ' Triggering value
bReserved(9) As Byte '
End Type

Private Type IDSECTOR
wGenConfig As Integer
wNumCyls As Integer
wReserved As Integer
wNumHeads As Integer
wBytesPerTrack As Integer
wBytesPerSector As Integer
wSectorsPerTrack As Integer
wvendorUnique(2) As Integer
sSerialNumber(19) As Byte
wBufferType As Integer
sFirmwareRev(7) As Byte
sModelNumber(39) As Byte
End Type

Private Const VER_PLATFORM_WIN32s As Long = 0
Private Const VER_PLATFORM_WIN32_WINDOWS As Long = 1
Private Const VER_PLATFORM_WIN32_NT As Long = 2
Private Type OsveRSIONINFO
dwOsversionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type

Private Declare Function GetVersionEx Lib "KERNEL32" Alias "GetVersionExA" (lpVersioninformation As OsveRSIONINFO) As Long
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
Private Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileA" (ByVal lpFileName As String,ByVal dwDesiredAccess As Long,ByVal dwShareMode As Long,ByVal lpSecurityAttributes As Long,ByVal dwCreationdisposition As Long,ByVal dwFlagsAndAttributes As Long,ByVal hTemplateFile As Long) As Long
Private Declare Function DeviceIoControl Lib "KERNEL32" (ByVal hDevice As Long,ByVal dwIoControlCode As Long,lpInBuffer As Any,ByVal nInBufferSize As Long,lpOutBuffer As Any,ByVal nOutBufferSize As Long,lpBytesReturned As Long,ByVal lpOverlapped As Long) As Long
Private Declare Sub copyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any,Source As Any,ByVal Length As Long)
Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Private m_diskInfo As IDSECTOR

Private Function OpenSMART(ByVal nDrive As Byte) As Long
Dim hSMARTIOCTL&,hd$
Dim VersionInfo As OsveRSIONINFO
VersionInfo.dwOsversionInfoSize = Len(VersionInfo)
GetVersionEx VersionInfo
Select Case VersionInfo.dwPlatformId
Case VER_PLATFORM_WIN32s
OpenSMART = hSMARTIOCTL
Case VER_PLATFORM_WIN32_WINDOWS
hSMARTIOCTL = CreateFile("//./SMARTVSD",CREATE_NEW,0)
Case VER_PLATFORM_WIN32_NT
If nDrive < MAX_IDE_DRIVES Then
hd = "//./PhysicalDrive" & nDrive
hSMARTIOCTL = CreateFile(hd,GENERIC_READ Or GENERIC_WRITE,FILE_SHARE_READ Or FILE_SHARE_WRITE,OPEN_EXISTING,0)
End If
End Select
OpenSMART = hSMARTIOCTL
End Function

Private Function DoIDENTIFY(ByVal hSMARTIOCTL As Long,pSCIP As SENDCMDINParaMS,pScop() As Byte,ByVal bIDCmd As Byte,ByVal bDriveNum As Byte,lpcbBytesReturned As Long) As Boolean
pSCIP.irDriveRegs.bDriveHeadReg = &HA0 Or ((bDriveNum And 1) * 2 ^ 4)
pSCIP.irDriveRegs.bCommandReg = bIDCmd
pSCIP.bDriveNumber = bDriveNum
DoIDENTIFY = CBool(DeviceIoControl(hSMARTIOCTL,DFP_RECEIVE_DRIVE_DATA,pSCIP,32,pScop(0),528,lpcbBytesReturned,0))
End Function

Public Function GetdiskInfo(ByVal nDrive As Byte) As Long
Dim hSMARTIOCTL&,cbBytesReturned&
Dim VersionParams As GETVERSIONOUTParaMS
Dim scip As SENDCMDINParaMS
Dim scop() As Byte
Dim OutCmd As SENDCMDOUTParaMS
Dim bDfpDriveMap As Byte
Dim bIDCmd As Byte ' IDE or ATAPI IDENTIFY cmd
Dim udisk As IDSECTOR
m_diskInfo = udisk
hSMARTIOCTL = OpenSMART(nDrive)
If hSMARTIOCTL <> INVALID_HANDLE_VALUE Then
Call DeviceIoControl(hSMARTIOCTL,DFP_GET_VERSION,ByVal 0,VersionParams,Len(VersionParams),cbBytesReturned,0)
bIDCmd = IIf((VersionParams.bIDEDeviceMap / 2 ^ nDrive And &H10),IDE_ATAPI_ID,IDE_ID_FUNCTION)
ReDim scop(LenB(OutCmd) + IDENTIFY_BUFFER_SIZE - 1) As Byte
If DoIDENTIFY(hSMARTIOCTL,scip,scop,bIDCmd,nDrive,cbBytesReturned) Then
copyMemory m_diskInfo,scop(LenB(OutCmd) - 4),LenB(m_diskInfo)
CloseHandle hSMARTIOCTL
GetdiskInfo = 1
Exit Function
End If
CloseHandle hSMARTIOCTL
GetdiskInfo = 0
End If
End Function

Public Function GetHDlist() As String
If GetdiskInfo(0) = 1 Then
GetHDlist = "硬盘物理系列号:" & Trim(StrConv(m_diskInfo.sSerialNumber,vbUnicode))
'GetHDlist = "硬盘型号:" & StrConv(m_diskInfo.sModelNumber,vbUnicode)
Else
GetHDlist = "读取错误"
End If
End Function

Private Sub Command1_Click()
MsgBox GetHDlist()
End Sub

逻辑盘序列号:



Private Declare Function GetVolumeinformation& Lib "KERNEL32" _
Alias "GetVolumeinformationA" (ByVal lpRootPathName As String,_
ByVal pVolumeNameBuffer As String,ByVal nVolumeNameSize As Long,_
lpVolumeSerialNumber As Long,lpMaximumComponentLength As Long,_
lpFileSystemFlags As Long,ByVal lpFileSystemNameBuffer As String,_
ByVal nFileSystemNameSize As Long)
Private Const MAX_FILENAME_LEN = 256


Public Function DriveSerial(ByVal sDrv As String) As Long
Dim RetVal As Long
Dim str As String * MAX_FILENAME_LEN
Dim str2 As String * MAX_FILENAME_LEN
Dim a As Long
Dim b As Long
GetVolumeinformation sDrv & ":/",str,MAX_FILENAME_LEN,RetVal,_
a,b,str2,MAX_FILENAME_LEN
DriveSerial = RetVal
End Function

Private Sub Command1_Click() Dim mCode As Long mCode = DriveSerial("C") machine.Text = "c:" & mCode & " " machine.Text = machine.Text & "d:" & DriveSerial("D")End Sub

原文地址:https://www.jb51.cc/vb/261875.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐