MQL5 Windows DLL readFile Function Problem

 

I have try to use a code which works in Metatrader 4 but in Metatrader 5 i get a error.

The function which i use is a windows dll function import, it looks like following:

#import "kernel32.dll"
int CreateFileW(string, uint, int, int, int, int, int);
int GetFileSize(int, int);
int ReadFile(int, uchar&[], int, int&[], int);
int CloseHandle(int);
#import

string ReadFile(string Filename)
  {
   string strFileContents = "";
   int h = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   if(h == -1)
     {
      // Open failed
     }
   else
     {
      int sz = GetFileSize(h, 0);
      if(sz > 0)
        {
         uchar buffer[];
         ArrayResize(buffer, sz);
         int read[1];
         ReadFile(h, buffer, sz, read, 0);
         if(read[0] == sz)
           {
            strFileContents = CharArrayToString(buffer, 0, read[0]);
           }
         else
           {
            // Read failed
           }
        }
      else
        {
         // Empty file
        }
      CloseHandle(h);
     }
   return strFileContents;
  }


When i put the EA on a chart i see the following error in the terminal experts tab printed:

Metatrader 5 error from windows dll function

How can this problem be slove?

 

"ReadFile" is already used.

int ReadFile(int, uchar&[], int, int&[], int);
string ReadFile(string Filename)

 
Calling WinAPI ReadFile() results in a crash
Calling WinAPI ReadFile() results in a crash
  • 2017.03.26
  • www.mql5.com
I tried to run the attached simple script in 64bit MT5, but for some reason ReadFile() results in an ugly crash...
 

Thank you for the help, i want to share my working code with everybody.

This is the code which is working now for me:

static const uint GENERIC_READ     = 0x80000000;
static const uint GENERIC_WRITE    = 0x40000000;

static const int SHARE_READ        = 1;
static const int SHARE_DELETE      = 4;

static const int CREATE_ALWAYS     = 2;
static const int OPEN_EXISTING     = 3;
static const int OPEN_CREATE       = 4;

#import "kernel32.dll"
int  CreateFileW(string name,int desiredAccess,int SharedMode,int security,int creation,int flags,int templateFile);
int GetFileSize(int, int);
bool ReadFile (int fileHandle,char& buffer[],int bytes,int& numOfBytes,long overlapped);
int CloseHandle(int);
#import


string ReadFileFunktion(string Filename)
  {
   string strFileContents = "";
   int h = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   if(h == -1)
     {
      // Open failed
     }
   else
     {
      int sz = GetFileSize(h, 0);
      if(sz > 0)
        {
         uchar buffer[];
         ArrayResize(buffer, sz);
         int read;
         ReadFile(h, buffer, sz, read, 0);
         if(read == sz)
           {
            strFileContents = CharArrayToString(buffer, 0, read);
           }
         else
           {
            // Read failed
           }
        }
      else
        {
         // Empty file
        }
      CloseHandle(h);
     }
   return strFileContents;
  }
Reason: