file operations via winAPI kernel32

0

hello gurus i am i am trying to read an external file operation in mql4 firstly my EA involves reading and writing between two mt4 terminals but to do that i have to know how to operate files externally in mql4 i have tried this documentation but it didnt work. i downloaded the kernel32 file and placed it in the mql4 library file and tried working with this documentation in the code below but still it didnt work. please help what isenter code here the process of doing it

#define GENERIC_READ    0x80000000
#define GENERIC_WRITE   0x40000000
#define FILE_SHARE_READ         0x00000001
#define FILE_SHARE_WRITE        0x00000002

#define CREATE_NEW              1
#define CREATE_ALWAYS           2
#define OPEN_ALWAYS             4
#define OPEN_EXISTING           3
#define TRUNCATE_EXISTING       5

#define FILE_BEGIN              0
#define FILE_CURRENT            1
#define FILE_END                2

#define INVALID_HANDLE_VALUE    -1
#import "kernel32.dll"
 
   int CreateFileW(string Filename, int AccessMode, int ShareMode, int PassAsZero, int CreationMode, int FlagsAndAttributes, int AlsoPassAsZero);
   int ReadFile(int FileHandle, int BufferPtr, int BufferLength, int & BytesRead[], int PassAsZero);
   int WriteFile(int FileHandle, int BufferPtr, int BufferLength, int & BytesWritten[], int PassAsZero);
   int SetFilePointer(int FileHandle, int Distance, int PassAsZero, int FromPosition);
   int GetFileSize(int FileHandle, int PassAsZero);
   int CloseHandle(int FileHandle);

   bool DeleteFileA(string Filename);

  int MulDiv(string X, int N1,int N2);
   // Used for temporary conversion of an array into a block of memory, which
   // can then be passed as an integer to ReadFile
   int LocalAlloc(int Flags, int Bytes);
   int RtlMoveMemory(int DestPtr, double & Array[], int Length);
   int LocalFree(int lMem);

   // Used for converting the address of an array to an integer 
   int GlobalLock(double & Array[]);
   bool GlobalUnlock(int hMem);
#import

int OpenNewFileForWriting(string FileName, bool ShareForReading = false)
{
   int ShareMode = 0;
   if (ShareForReading) ShareMode = FILE_SHARE_READ;

return CreateFileW(FileName, GENERIC_WRITE /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, CREATE_ALWAYS, 0, 0);
  // return (CreateFileA(FileName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0));
}

bool IsValidFileHandle(int FileHandle)
{
   return (FileHandle != INVALID_HANDLE_VALUE);
}

void CloseFile(int FileHandle)
{
   CloseHandle(FileHandle);
}

bool WriteToFile(int FileHandle, string DataToWrite)
{
   // Receives the number of bytes written to the file. Note that MQL can only pass 
   // arrays as by-reference parameters to DLLs
  int BytesWritten[1] = {0};

   // Get the length of the string 
   int szData = StringLen(DataToWrite);
Print(szData);
Print(DataToWrite);
   // Do the write 
   WriteFile(FileHandle, MulDiv(DataToWrite,1,1), szData, BytesWritten, 0);
   
   Print("BytesWritten = " + BytesWritten[0]);
   // Return true if the number of bytes written matches the expected number 
   return (BytesWritten[0] == szData);   
}

int start()
{
   string strTestFilename = "C:\\test2.txt";


   Print("About to write some example data to " + strTestFilename + "...");

   // Write some CRLF-terminated lines to the test file 
   int fWrite = OpenNewFileForWriting(strTestFilename, true);
   if (!IsValidFileHandle(fWrite))
   {
      Print("Unable to open " + strTestFilename + " for writing");
   } else {
      if((WriteToFile(fWrite, "Test12345678"))==TRUE) {
      Print("write ok"); 
      } else { Print("write not ok");  }
    }
    }
mql4
asked on Stack Overflow Aug 17, 2020 by maxwell Owusu

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0