Download MetaTrader 5

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

To add comments, please log in or register
Sign up for MQL5.community to post your Forum topic
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.08.04 11:17 

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? 

Carl Schreiber
6588
Carl Schreiber 2016.08.04 11:28  
Ask Prof. Dr. Google he knows more..
Drazen Penic
2540
Drazen Penic 2016.08.04 12:02  
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.08.22 18:35  

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);
}
whroeder1
14480
whroeder1 2016.08.22 20:16  
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.)
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.08.22 22:01  
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);
}
Carl Schreiber
6588
Carl Schreiber 2016.08.23 06:46  
any error messages?
Ex Ovo Omnia
3146
Ex Ovo Omnia 2016.08.23 10:25  
Check with the command line date command if you have enough privileges to change the system time/date.
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.08.23 12:06  

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!

Carl Schreiber
6588
Carl Schreiber 2016.08.23 13:24  

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

See here.

Wemerson Guimaraes
560
Wemerson Guimaraes 2016.08.23 13:42  
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;

}
12
To add comments, please log in or register