DarkenRahl

SetLocalTime function (DLL)

6 posts in this topic

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")

 

Share this post


Link to post
Share on other sites



Not very experienced on DLLCalls, but changing system time needs admin privileges on most systems.

Did you include a #RequireAdmin or run the script as one?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I run my programme with the autoit require admin function. Still the same error. Danifirex, I believe the error is from the parameter but I just don't know what to do.. ANy help?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Good!!!!!!!!!!

 

 

Saludos

Share this post


Link to post
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