Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting The System Time

Status
Not open for further replies.

dg043

Programmer
Apr 22, 2002
52
GB
Hi,
I am trying to write an application which corrects the system time. According to the Microsoft web site (MSDN), the way to do this from Visual Basic 6.0 is to include the following API-calling code:

Option Explicit

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 SetSystemTime Lib "kernel32" (lpSystemTime _
As SYSTEMTIME) As Long

Private Sub Form_Load()
Dim lReturn As Long
Dim lpSystemTime As SYSTEMTIME
lpSystemTime.wYear = 1996
lpSystemTime.wMonth = 6
lpSystemTime.wDayOfWeek = 5
lpSystemTime.wDay = 28
lpSystemTime.wHour = 9
lpSystemTime.wMinute = 42
lpSystemTime.wSecond = 0
lpSystemTime.wMilliseconds = 0
lReturn = SetSystemTime(lpSystemTime)
End Sub

This however, only works under VB 6.0. Whatever I do, I cannot get this code to work under Visual Basic .NET. I realise that there need to be some changes to the code to get i to compile. That is, you can't use Option Explicit under VB .NET, the word Structure has to be used instead of Type and the function declaration requires ByRef before the argument. So my code looks like this:

Private Structure SYSTEMTIME
Public wYear As Integer
Public wMonth As Integer
Public wDayOfWeek As Integer
Public wDay As Integer
Public wHour As Integer
Public wMinute As Integer
Public wSecond As Integer
Public wMilliseconds As Integer
End Structure

Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime _
As SYSTEMTIME) As Long

Private Sub Form_Load()
Dim lReturn As Long
Dim lpSystemTime As SYSTEMTIME
lpSystemTime.wYear = 1996
lpSystemTime.wMonth = 6
lpSystemTime.wDayOfWeek = 5
lpSystemTime.wDay = 28
lpSystemTime.wHour = 9
lpSystemTime.wMinute = 42
lpSystemTime.wSecond = 0
lpSystemTime.wMilliseconds = 0
lReturn = SetSystemTime(lpSystemTime)
End Sub

But it still doesn't work; producing a DLL error 87 (The parameter is invalid) when executing the last line.
Any ideas?

Dan Griffiths
Software Analyst
National Grid Transco (NGT)
 
Try the following changes:
Code:
[b]Public[/b] Structure SYSTEMTIME
Public wYear As [b]UInt16[/b]
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As [b]Boolean[/b]
 
Thanks Dimandja! That worked great!

Dan Griffiths
Software Analyst
National Grid Transco (NGT)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top