通过winAPI kernel32进行文件操作

问题描述

您好大师,我正在尝试读取mql4中的外部文件操作 首先,我的EA涉及在两个mt4终端之间进行读取和写入,但是要做到这一点,我必须知道如何在mql4中从外部操作文件,我已经尝试过此文档,但是没有用。 我下载了kernel32文件,并将其放在mql4库文件中,并尝试在下面的代码中使用此文档,但仍然无法正常工作。 请帮助enter code here完成该过程

#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 & BytesWritten[],int PassAsZero);
   int SetFilePointer(int FileHandle,int distance,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*/,CREATE_ALWAYS,0);
  // return (CreateFileA(FileName,GENERIC_WRITE,ShareMode,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");  }
    }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)