如何解决通过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 举报,一经查实,本站将立刻删除。