Imports System.Management
Imports System.Net
Public Class Form1
Private Function GETHWID() As String
Dim cpuInfo As String = String.Empty
Dim mc As New ManagementClass("win32_processor")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo As ManagementObject In moc
If cpuInfo = "" Then
cpuInfo = mo.Properties("processorID").Value.ToString()
Exit For
End If
Next
GETHWID = cpuInfo
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = GETHWID()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Text = "BFEBFBFF0001067B" Then 'หมายเลขเครื่องที่ 1 ที่สามารถใช้งานได้
Timer1.Enabled = False
Label6.ForeColor = Color.Green
Label6.Text = "ยินดีต้อนรับ..กรุณากรอกชื่อและรหัสผ่านเพื่อเข้าใช้"
ElseIf Label1.Text = "ASDDDFERT15691VD" Then 'หมายเลขเครื่องที่ 2 ที่สามารถใช้งานได้
Timer1.Enabled = False
Label6.ForeColor = Color.Green
Label6.Text = "ยินดีต้อนรับ..กรุณากรอกชื่อและรหัสผ่านเพื่อเข้าใช้"
Else 'ถ้าหมายเลขเครื่องไม่ตรงตามที่ตั้งไว้ให้แสดงข้อความ Error และปิดส่วนที่ใช้งาน
Timer1.Enabled = False
TextBox1.Enabled = False
TextBox2.Enabled = False
Button1.Enabled = False
Label6.ForeColor = Color.Red
Label6.Text = "คอมพิวเตอร์เครื่องนี้ไม่ได้รับสิทธิ์ให้ใช้โปรแกรมนี้ !!"
End If
End Sub
End Class
แบบที่ 1 get the harddrive serial number หรือหมายเลขพวก ฮาร์ดดีส นั้นเอง คิดว่าฮาร์ดดิส แต่ละลูกหมายเลขไม่น่าจะซ้ำกัน
Code (VB.NET)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = GetDriveSerialNumber()
End Sub
Public Function GetDriveSerialNumber() As String
Dim DriveSerial As Integer
'Create a FileSystemObject object
Dim fso As Object = CreateObject("Scripting.FileSystemObject")
Dim Drv As Object = fso.GetDrive(fso.GetDriveName(Application.StartupPath))
With Drv
If .IsReady Then
DriveSerial = .SerialNumber
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With
Return DriveSerial.ToString("X2")
End Function
End Class
----------------------------------------------------------------------------------------------------------
หรือแบบที่ 2 HWID ที่เอา Processor Id [Cpu] + VolumeSerial Id [Hdd] + MotherBoard Id [Mb] + MACAddress [mac]
ถ้ามันยังซ้ำกันอยู่ก็ไม่รู้จะว่ายังไงแล้ว
Code (VB.NET)
Imports System.Management
Imports System
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hw As New clsComputerInfo
Dim hdd As String
Dim cpu As String
Dim mb As String
Dim mac As String
cpu = hw.GetProcessorId()
hdd = hw.GetVolumeSerial("C")
mb = hw.GetMotherBoardID()
mac = hw.GetMACAddress()
'MsgBox(cpu & " " & hdd & " " & mb & " " & mac)
Dim hwid As String = Strings.UCase(hw.getMD5Hash(cpu & hdd & mb & mac))
' MessageBox.Show(Strings.UCase(hwid))
Label1.Text = hwid
End Sub
End Class
Public Class clsComputerInfo
Friend Function GetProcessorId() As String
Dim strProcessorId As String = String.Empty
Dim query As New SelectQuery("Win32_processor")
Dim search As New ManagementObjectSearcher(query)
Dim info As ManagementObject
For Each info In search.Get()
strProcessorId = info("processorId").ToString()
Next
Return strProcessorId
End Function
Friend Function GetMACAddress() As String
Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim MACAddress As String = String.Empty
For Each mo As ManagementObject In moc
If (MACAddress.Equals(String.Empty)) Then
If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()
mo.Dispose()
End If
MACAddress = MACAddress.Replace(":", String.Empty)
Next
Return MACAddress
End Function
Friend Function GetVolumeSerial(Optional ByVal strDriveLetter As String = "C") As String
Dim disk As ManagementObject = New ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", strDriveLetter))
disk.Get()
Return disk("VolumeSerialNumber").ToString()
End Function
Friend Function GetMotherBoardID() As String
Dim strMotherBoardID As String = String.Empty
Dim query As New SelectQuery("Win32_BaseBoard")
Dim search As New ManagementObjectSearcher(query)
Dim info As ManagementObject
For Each info In search.Get()
strMotherBoardID = info("SerialNumber").ToString()
Next
Return strMotherBoardID
End Function
Friend Function getMD5Hash(ByVal strToHash As String) As String
Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = md5Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
End Class