标题编程保护系统时间
栏目软件世界
作者张和据
发布2001年47期
大家知道,系统时间(日期和时间)对真实记录计算机的使用情况很有意义。许多共享软件也是根据系统时间确定其免费的试用期是否结束。因此,有必要保护系统时间,使系统时间不被随意修改。现在,我们只要用VB编写很简单的代码就能达到保护系统时间的目的。具体方法如下:
运行VB,在窗体上增加定时器控件TIMER1和TIMER2,双击窗体,编写如下代码:
Private Sub Form_Load()
Me.Hide
Timer1.Interval = 300
Timer2.Interval = 1000
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
在窗体上双击TIMER1,编写如下代码:
Private Sub Timer1_Timer()
Call getwingcap
wincaption = getwingcap
If (wincaption Like “*日期/时间*”) Then
Call savtime
Timer1.Enabled = False
Timer2.Enabled = True
End If
If(wincaption Like “*MS-DOS*”) Then
Call savtime
Timer1.Enabled = False
Timer2.Enabled = True
End If
End Sub
用同样的方法编写TIMER2的代码:
Private Sub Timer2_Timer()
countime = countime + 1
Call closetimewindows
If winHwnd = 0 Then
Date = savdata '恢复系统日期
Time = savtimer + TimeSerial(0, 0, countime) '恢复系统时间
Timer2.Enabled = False
Timer1.Enabled = True
End If
End Sub
在“工程”菜单中点击“添加模块”,然后,加入如下声明、函数和过程:
Declare Function FindWindow Lib “user32” Alias “FindWindowA”(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindowText Lib “user32” Alias “GetWindowTextA” ByVal hwnd As Long( ByVal lpString As String ByVal cch As Long) As Long
Declare Function GetForegroundWindow Lib “user32”() As Long
Public wincaption As String
Public winHwnd, countime As Long
Public savdata As Date
Public savtimer
Public Function getwingcap() '取更改日期/时间窗口标题
Dim strcap As String
strcap = String(255, 9)
inglen = Len(strcap)
k = GetWindowText(GetForegroundWindow, strcap, inglen)
getwingcap = strcap
End Function
Public Sub closetimewindows()''查找日期/时间窗口是否关闭
winHwnd = FindWindowvbNullString wincaption
End Sub
Public Sub savtime()''保存系统未被更改前的日期/时间
savdata = Date
savtimer = Time
countime = 0
End Sub
以上代码编写完毕后,将编译生成可执行文件加入到注册表的HKEY_LOCAL_
MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run键下,以后,每次系统启动时,它就自动在后台运行,始终保护系统时间不被更改。即使有人在MS-DOS窗口下更改日期和时间,也能够自动恢复到原来的日期和时间(在CMOS或纯DOS环境下更改时间,本程序无法保护)。