利用硬盘序列号计算软件注册码。
在模块中加入下列声明:
Public Declare Function GetVolumeInformation Lib "kernel32" _
Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long
'得到某一磁盘分区的信息,如C:
窗体代码如下:
Option Explicit
Private Regid, Localid As Long
Private Sub CmdLocalID_Click()
'根据C盘序列号得到原ID
Dim Driver, VolName, Fsys As String
Dim volNumber, MCM, FSF As Long
Driver = "c:\"
Dim res As Long
res = GetVolumeInformation(Driver, VolName, 127, volNumber, MCM, FSF, Fsys, 127)
'volNumber是C盘序列号
Localid = volNumber / 2 + 123456789
Text1.Text = Localid
End Sub
Private Sub CmdRegID_Click()
'根据原ID算出注册ID
If IsNumeric(Text1.Text) Then
Regid = CLng(Text1.Text) / 4 * 3 + 987654321
Else
'error
End If
Text2.Text = Regid
End Sub
Private Sub CmndCheckID_Click()
'验证注册ID
Dim Driver, VolName, Fsys As String
Dim volNumber, MCM, FSF As Long
Driver = "c:\"
Dim res As Long
res = GetVolumeInformation(Driver, VolName, 127, volNumber, MCM, FSF, Fsys, 127)
Dim Tid As Long
Tid = volNumber / 2 + 123456789
If Regid = Tid / 4 * 3 + 987654321 Then
MsgBox "正确!"
Else
MsgBox "错误!"
End If
End Sub
为便于演示,我在窗体上用了两个文本框三个按钮,请根据情况灵活使用,可以在用户端算出原ID,发给你,你算出注册ID再发给用户,验证当然是在用户端啦。ID算法很简单,只作示范。此注册ID只能在这一台机器上使用,对于非高手用户来说是足够啦。
注:如用户格式化C:盘后需重新获得ID。
……