Problems With Kernel32 DLL import

 

Hi Friends, thanks for your good works.

I have a code that worked perfectly for me on mt4. I tried converting my code to mt5 but the kernel32.dll aspect of the code is not giving the same result.

Please help me check what I am doing wrong.

Thanks



//+------------------------------------------------------------------+

//|                                                    K32dllmt5.mq5 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict



#import   "kernel32.dll"

int CreateFileW(string Filename,int AccessMode,int ShareMode,int PassAsZero,int CreationMode,int FlagsAndAttributes,int AlsoPassAsZero);

int GetFileSize(int FileHandle,int PassAsZero);

int SetFilePointer(int FileHandle,int Distance,int &PassAsZero[],int FromPosition);

int ReadFile(int FileHandle,uchar &BufferPtr[],int BufferLength,int  &BytesRead[],int PassAsZero);

int CloseHandle(int FileHandle);

#import





double data[][2];



int BytesToRead;

string    datapath;

string    result;



union Price

  {

   uchar             buffer[8];

   double            close;

  };

Price     m_price;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

    string account_server=AccountInfoString(3);

    datapath=TerminalInfoString(3)+"\\history\\"

               +account_server+"\\"+Symbol()+"240"+".hst";

    ReadFileHst(datapath);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {



Comment("Bytes To Read: ", BytesToRead);





   

  }

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

void ReadFileHst(string FileName)

  {

   int       j=0;;

   string    strFileContents;

   int       Handle;

   int       LogFileSize;

   int       movehigh[1]= {0};

   uchar     buffer[];

   int       nNumberOfBytesToRead;

   int       read[1]= {0};

   int       i;

   double    mm;

//----- -----

   strFileContents="";

   Handle=CreateFileW(FileName,(int)0x80000000,3,0,3,0,0);

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

   if(Handle==-1)

     {

      Comment("");

      return;

     }

   LogFileSize=GetFileSize(Handle,0);

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

   if(LogFileSize<=0)

     {

      return;

     }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

   if((LogFileSize-148)/60==BytesToRead)

     {

      return;

     }

   SetFilePointer(Handle,148,movehigh,0);

   BytesToRead=(LogFileSize-148)/60;

   ArrayResize(data,BytesToRead,0);

   nNumberOfBytesToRead=60;

   ArrayResize(buffer,60,0);

   for(i=0; i<BytesToRead; i=i+1)

      //+------------------------------------------------------------------+

      //|                                                                  |

      //+------------------------------------------------------------------+

     {

      ReadFile(Handle,buffer,nNumberOfBytesToRead,read,NULL);

      if(read[0]==nNumberOfBytesToRead)

        {

         result=StringFormat("0x%02x%02x%02x%02x%02x%02x%02x%02x",buffer[7],buffer[6],buffer[5],buffer[4],buffer[3],buffer[2],buffer[1],buffer[0]);



         m_price.buffer[0] = buffer[32];

         m_price.buffer[1] = buffer[33];

         m_price.buffer[2] = buffer[34];

         m_price.buffer[3] = buffer[35];

         m_price.buffer[4] = buffer[36];

         m_price.buffer[5] = buffer[37];

         m_price.buffer[6] = buffer[38];

         m_price.buffer[7] = buffer[39];

         mm=m_price.close;

         data[j][0] = StringToDouble(result);

         data[j][1] = mm;

         j=j+1;

         strFileContents=TimeToString(StringToTime(result),3)+" "+DoubleToString(mm,8);

        }

      else

        {

         CloseHandle(Handle);

         return;

        }

     }

   CloseHandle(Handle);

   strFileContents=DoubleToString(data[j-1][0],3)+" "+DoubleToString(data[j-1][1],8)+" "+DoubleToString(data[j-2][1],3)+" "+DoubleToString(data[j-2][1],8);

   result=strFileContents;

  }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.08.22
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
  datapath=TerminalInfoString(3)+"\\history\\"
               +account_server+"\\"+Symbol()+"240"+".hst";

The first problem is that you are trying to read the H4 history. MT5 does not have timeframe separated history files. It generates a timeframe chart when accessed.