如何保证程序运行时系统时间不被修改!
2004-03-02 10:24
627 查看
当任何程序或用户修改系统时间的时候,系统会将 WM_TIMECHANGE 消息到所有的进程,我们的程序可以捕获到该消息,然后将系统时间恢复到修改前的状态,这样就可以在我们的程序运行时系统时间的正确性,代码如下:
'窗体form1(需要一个timer控件,interval=1000):
Private Sub Form_Load()
Timer1_Timer
RegisterWindow Me.hwnd '为窗口设置子类
End Sub
Private Sub Form_Unload(Cancel As Integer)
unRegisterWindow Me.hwnd '取消窗口的子类
End Sub
Private Sub Timer1_Timer()
OldTime = Now
End Sub
'模块modle1:
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Public OldTime As String
Public ChangeFlag As Boolean
Public Const WM_TIMECHANGE As Long = &H1E '当系统的时间变化时发送此消息给所有顶级窗口
Public oldproc As Long
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Function SetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long
Public Function RegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
oldproc = SetWindowLong(hwnd, -4, AddressOf WinProc)
End If
End Function
Public Function unRegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
SetWindowLong hwnd, -4, oldproc
End If
End Function
Public Function WinProc(ByVal hwnd As Long, ByVal msg As Long, ByVal lpara As Long, ByVal wpara As Long) As Long
Dim i, mytt
If msg = WM_TIMECHANGE And ChangeFlag = False Then '系统时间被修改了而且不是本程序修改的
ChangeFlag = True '本程序要修改系统时间
Call SetToOldTime '修改系统时间
Exit Function
End If
ChangeFlag = False
WinProc = CallWindowProc(oldproc, hwnd, msg, lpara, wpara)
End Function
Public Function SetToOldTime() As String '将时间恢复到设置前的状态
Dim tmp As String
tmp = OldTime '从保存的时间中取出修改前的系统时间
Dim lpSystemTime As SYSTEMTIME
lpSystemTime.wYear = Year(tmp) '取出年份
lpSystemTime.wMonth = Month(tmp) '取出月份
lpSystemTime.wDayOfWeek = -1
lpSystemTime.wDay = Day(tmp) '取出日
lpSystemTime.wHour = Hour(tmp) '取出小时
lpSystemTime.wMinute = Minute(tmp) '取出分钟
lpSystemTime.wSecond = Second(tmp) '取出秒
lpSystemTime.wMilliseconds = 0
'set the new time
SetLocalTime lpSystemTime
End Function
'到此程序就完成了,运行一下试试
'我的邮件:ppgg2002@sina.com
'QQ:55051552
'MSN:mmcgzs@hotmail.com
'窗体form1(需要一个timer控件,interval=1000):
Private Sub Form_Load()
Timer1_Timer
RegisterWindow Me.hwnd '为窗口设置子类
End Sub
Private Sub Form_Unload(Cancel As Integer)
unRegisterWindow Me.hwnd '取消窗口的子类
End Sub
Private Sub Timer1_Timer()
OldTime = Now
End Sub
'模块modle1:
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Public OldTime As String
Public ChangeFlag As Boolean
Public Const WM_TIMECHANGE As Long = &H1E '当系统的时间变化时发送此消息给所有顶级窗口
Public oldproc As Long
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Function SetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long
Public Function RegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
oldproc = SetWindowLong(hwnd, -4, AddressOf WinProc)
End If
End Function
Public Function unRegisterWindow(hwnd As Long) As Long
If hwnd <> 0 Then
SetWindowLong hwnd, -4, oldproc
End If
End Function
Public Function WinProc(ByVal hwnd As Long, ByVal msg As Long, ByVal lpara As Long, ByVal wpara As Long) As Long
Dim i, mytt
If msg = WM_TIMECHANGE And ChangeFlag = False Then '系统时间被修改了而且不是本程序修改的
ChangeFlag = True '本程序要修改系统时间
Call SetToOldTime '修改系统时间
Exit Function
End If
ChangeFlag = False
WinProc = CallWindowProc(oldproc, hwnd, msg, lpara, wpara)
End Function
Public Function SetToOldTime() As String '将时间恢复到设置前的状态
Dim tmp As String
tmp = OldTime '从保存的时间中取出修改前的系统时间
Dim lpSystemTime As SYSTEMTIME
lpSystemTime.wYear = Year(tmp) '取出年份
lpSystemTime.wMonth = Month(tmp) '取出月份
lpSystemTime.wDayOfWeek = -1
lpSystemTime.wDay = Day(tmp) '取出日
lpSystemTime.wHour = Hour(tmp) '取出小时
lpSystemTime.wMinute = Minute(tmp) '取出分钟
lpSystemTime.wSecond = Second(tmp) '取出秒
lpSystemTime.wMilliseconds = 0
'set the new time
SetLocalTime lpSystemTime
End Function
'到此程序就完成了,运行一下试试
'我的邮件:ppgg2002@sina.com
'QQ:55051552
'MSN:mmcgzs@hotmail.com
相关文章推荐
- 如何利用windosAPI计算程序运行时间 不使用VC的库,也就是说不跨系统,跨编译器
- 如何保证同时只有一个程序在系统中运行
- 技巧(5)- 如何让这个程序在系统里同时只运行一个(如果这个exe程序已经运行了,就不再运行)
- 如何修改Linux系统时间 ubuntu
- 如何修改Linux系统时间 ubuntu
- VC获取系统时间、程序运行时间
- 如何使Linux系统上的程序开机后自动运行
- VC获取系统时间、程序运行时间
- [CF Skills]如何在预定的时间运行你的程序
- 如何修改Vista系统的默认语言以及系统时间区域,格式
- android 命令修改时间或程序修改系统时间
- linux系统如何察看、修改系统时间
- 如何在系统登陆桌面运行程序
- Linux如何修改系统时间和时区
- 如何在Windows系统中设置Python程序定时运行
- 如何动态的显示程序运行的时间
- VC获取系统时间、程序运行时间
- WOT百城沙龙成都站,一起来聊如何从底层设计保证系统平稳运行?
- andoid如何修改系统时间
- ARMSYS2410-MULTIUAR 如何让系统启动时自动运行用户程序