Jump to content

SetLocalTime function (DLL)


Recommended Posts

I am reading dealing with dll in autoit and I came accross this exercise. The setLocalTime function in the kernel32.dll is supposed to set the local time of the machine (at least that's how i understand it). My code runs and echos the new time in the msgbox but the time fails to change on the system. Am i doing anything wrong?

$setTime = DllStructCreate("WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds")

DllStructSetData($setTime, "wHour", 3)
DllStructSetData($setTime, "wMinute", 35)
DllStructSetData($setTime, "wYear", 1608)
DllCall("Kernel32.dll", "int", "SetLocalTime", "ptr", DllStructGetPtr($setTime))

MsgBox(64, "Dll Time Function", "The time is " & DllStructGetData($setTime, "wHour")& ':' & DllStructGetData($setTime, "wMinute") & " PM")

 

Link to comment
Share on other sites

Try to check last error...

 

#include <WinAPI.au3>
#include <WinAPIDiag.au3>
Local $setTime = DllStructCreate("WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds")

DllStructSetData($setTime, "wHour", 3)
DllStructSetData($setTime, "wMinute", 35)
DllStructSetData($setTime, "wYear", 1608)
Local $aRet=DllCall("Kernel32.dll", "int", "SetLocalTime", "ptr", DllStructGetPtr($setTime))


if @error Then
    MsgBox(16,"Error","Error DllCall")
    Exit
EndIf

If $aRet[0]=0 Then
Local $iError=_WinAPI_GetLastError()
MsgBox(16,"Error SetLocalTime",_WinAPI_GetErrorMessage($iError))
Else
MsgBox(64,"Info","Everything should be OK")
EndIf

Saludos

Link to comment
Share on other sites

Thanks to you all. I figured out the problem with your help. All the parameters of the SYSTEMTIME struct needed be filled with the dllstructsetdata before anything happened. The final script required admin to run. The finished code is so.

#RequireAdmin
#include <WinAPI.au3>
#include <WinAPIDiag.au3>
Local $setTime = DllStructCreate("WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds")

DllStructSetData($setTime, 1, 2012)
DllStructSetData($setTime, 2, 6)
DllStructSetData($setTime, 4, 2)
DllStructSetData($setTime, 5, 11)
DllStructSetData($setTime, 6, 54)
DllStructSetData($setTime, 7, 32)
DllStructSetData($setTime, 8, 254)


Local $aRet=DllCall("Kernel32.dll", "bool", "SetLocalTime", "ptr", DllStructGetPtr($setTime))

if @error Then
    MsgBox(16,"Error","Error DllCall")
    Exit
EndIf

If $aRet[0]=0 Then
Local $iError=_WinAPI_GetLastError()
MsgBox(16,"Error SetLocalTime",_WinAPI_GetErrorMessage($iError))
Else
MsgBox(64,"Info","Everything should be OK")
EndIf
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...