Does anyone know how to use the GetSystemTime() function from kernel32.dll ?

 

Hello guys.

 

I need to change the computer clock to the broker time automatically. 

I now i  has to import kernel32.dll and use SetSystemTime function, but don't know about the parameters of this function. 

Does anyone know how to use it? 

 
Ask Prof. Dr. Google he knows more..
 
 

Ok, thanks... I'm sorry for my mistake.

I would to write SetLocalTime() on the title and not GetSystemTime().

And Yes, i do a google search before ask here... but i just found about how to use GetSystemTime()/GetLocalTime() in mql4, but nothing about SetSystemTime()/SetSystemTime().

How can import the dll call i know, but i cannot know how to format the date value to send to the SetLocalTime() function. Below in this thread has a code that i trying to make...

For the record, here a code to get system and local time from DLL... I think this is irrelevant because we have TimeLocal() and TimeGMT() to get those values.... but it's a good exercise.

#import "Kernel32.dll"
   void GetSystemTime(int& TimeArray[]); // Working fine!
   void GetLocalTime(int& TimeArray[]); // Working fine!
#import

int TimeArray[4];

datetime GetWindowsSystemTime(){

   GetSystemTime(TimeArray);
   int year = TimeArray[0] & 65535;
   int month = TimeArray[0] >> 16;
   int day = TimeArray[1] >> 16;
   int hour = TimeArray[2] & 65535;
   int minute = TimeArray[2] >> 16;
   int second = TimeArray[3] & 65535;
   
   return StringToTime( FormatDateTime(year, month, day, hour, minute, second) );
}

datetime GetWindowsLocalTime(){

   GetLocalTime(TimeArray);
   int year = TimeArray[0] & 65535;
   int month = TimeArray[0] >> 16;
   int day = TimeArray[1] >> 16;
   int hour = TimeArray[2] & 65535;
   int minute = TimeArray[2] >> 16;
   int second = TimeArray[3] & 65535;
   
   return StringToTime( FormatDateTime(year, month, day, hour, minute, second) );
}


string FormatDateTime(int year, int iMonth, int iDay, int iHour, int iMinute, int iSecond) {
   string month = IntegerToString(iMonth + 100);
   month = StringSubstr(month, 1);
   string day = IntegerToString(iDay + 100);
   day = StringSubstr(day, 1);
   string hour = IntegerToString(iHour + 100);
   hour = StringSubstr(hour, 1);
   string minute = IntegerToString(iMinute + 100);
   minute = StringSubstr(minute, 1);
   string second = IntegerToString(iSecond + 100);
   second = StringSubstr(second, 1);
   return StringFormat("%d.%s.%s %s:%s:%s", year, month, day, hour, minute, second);

}


 

Here a code that i am trying to change computer local time, but not works:

struct _SYSTEMTIME {
      int wYear;
      int wMonth;
      int wDayOfWeek;
      int wDay;
      int wHour;
      int wMinute;
      int wSecond;
      int wMilliseconds;
   };
#import "Kernel32.dll"
   bool SetLocalTime(_SYSTEMTIME& st);
#import

bool SetWindowsLocalTime(datetime time){
      
   _SYSTEMTIME st;
   st.wYear      = TimeYear(time);
   st.wMonth     = TimeMonth(time);
   st.wDayOfWeek = TimeDayOfWeek(time);
   st.wDay       = TimeDay(time);
   st.wHour      = TimeHour(time);
   st.wMinute    = TimeMinute(time);
   st.wSecond    = TimeSeconds(time);
   
   return SetLocalTime(st);
}
 
struct _SYSTEMTIME {
      int wYear;
      int wMonth;
      int wDayOfWeek;
      int wDay;
      int wHour;
      int wMinute;
      int wSecond;
      int wMilliseconds;
   };
If you look at the documentation SYSTEMTIME structure (Windows) you would see that the structure are all WORD (Mq4 short) and not using DWORD (Mql4 int.)
 
WHRoeder:
If you look at the documentation SYSTEMTIME structure (Windows) you would see that the structure are all WORD (Mq4 short) and not using DWORD (Mql4 int.)

Hi WHRoeder.


Just updated to short, but not works... See:

 

struct _SYSTEMTIME {
      short wYear;
      short wMonth;
      short wDayOfWeek;
      short wDay;
      short wHour;
      short wMinute;
      short wSecond;
      short wMilliseconds;
   };


#import "Kernel32.dll"
   bool SetLocalTime(_SYSTEMTIME& st);
#import

bool SetWindowsLocalTime(datetime time){
      
   _SYSTEMTIME st;
   st.wYear      = (short) TimeYear(time);
   st.wMonth     = (short) TimeMonth(time);
   st.wDayOfWeek = (short) TimeDayOfWeek(time);
   st.wDay       = (short) TimeDay(time);
   st.wHour      = (short) TimeHour(time);
   st.wMinute    = (short) TimeMinute(time);
   st.wSecond    = (short) TimeSeconds(time);
   
   return SetLocalTime(st);
}
 
any error messages?
 
Check with the command line date command if you have enough privileges to change the system time/date.
 

googly, no messages. Function SetLocalTime Just return 0

Ovo, i test it and do not have privileges; only if i open terminal with Admin Privileges! But i run MetaTrader with admin privileges and not works too!

 

Error code is not returned normally - but set: int err = kernel32::GetLastError();

See here.

 
gooly:

Error code is not returned normally - but set: int err = kernel32::GetLastError();

See here.

Thanks my friend.

 

I've try this change on my code and returns error 87 ( ERROR_INVALID_PARAMETER ) - https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

 

Edited:

Found it...  Just set wDayOfWeek to 0...

But now, the errror code is 1314 ( ERROR_PRIVILEGE_NOT_HELD )https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx

Then there is nothing to do.. Right!? Because user that runs MetaTrader, has low privileges and need to be Admin!!

 

struct _SYSTEMTIME {
      short wYear;
      short wMonth;
      short wDayOfWeek;
      short wDay;
      short wHour;
      short wMinute;
      short wSecond;
      short wMilliseconds;
   };


#import "Kernel32.dll"
   bool SetLocalTime(_SYSTEMTIME& st);
   void SetLastError(int dwErrCode);
#import

#import "ntdll.dll"
   int  RtlGetLastWin32Error();
#import

bool SetWindowsLocalTime(datetime time){
      
   _SYSTEMTIME st;
   st.wYear      = (short) TimeYear(time);
   st.wMonth     = (short) TimeMonth(time);
   st.wDay       = (short) TimeDay(time);
   st.wHour      = (short) TimeHour(time);
   st.wMinute    = (short) TimeMinute(time);
   st.wSecond    = (short) TimeSeconds(time);

   st.wDayOfWeek=0; // <--- Here!!!!!
   st.wMilliseconds=0; // Optional
   
   SetLastError(0); // Reset error Flag before try to change computer local time
   bool result = SetLocalTime(st);
   int err = RtlGetLastWin32Error();

   Alert( "Error: ", err);
   return result;

}
Reason: