File handling via kernel32.dll functions

 

Hi,

in https://www.mql5.com/en/articles/1540 a very nice approach is described how to work with files, which are not restricted to the directory locations as with MQL4 file functions.

   int _lopen(string path, int of);

   int _lcreat (string path, int attrib);

   int _llseek (int handle, int offset, int origin);

   int _lread  (int handle, string buffer, int bytes);

   int _lwrite (int handle, string buffer, int bytes);

   int _lclose (int handle);  

I used them a lot with old scripts. Unfortunatelly with builds> 600 of MT4 those functions do not work anymore.

Is anybody facing the same problem or does anybody know a workaround? 

 

Due to the fact the mt4 has now switched from ASCII to ANSI this article it not valid any more!

To use Windows' kernel3.dll function use CreateFileW(..) and the equiv. functions:

#import "kernel32.dll" //http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
   int  SetFileAttributesW(string file, int attributes);
   int  GetFileAttributesW(string file);
   int  CreateFileW(string lpFileName, uint dwDesiredAccess, int dwShareMode, 
                    int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
   int  GetFileSize(int hFile, int& lpFileSizeHigh[]);
   int  SetFilePointer(int hFile, int lDistanceToMove, int& lpDistanceToMoveHigh[], int dwMoveMethod);
   int  ReadFile( int hFile, uchar &lpBuffer[], int nNumberOfBytesToRead, int &lpNumberOfBytesRead[], int lpOverlapped);
   int  WriteFile(int hFile, uchar &Buffer[], int BufferLength, int & BytesWritten[], int PassAsZero);
        //int WriteFile(int hPipe, string sBuffer, int NumberOfBytesToWrite, int& bytesWritten[], int lpOverlapped);
   int  CloseHandle(int hFile);
   bool MoveFileExW(string lpExistingFileName, string lpNewFileName, int dwFlags); // CANNOT move to anther device: " ERROR_NOT_SAME_DEVICE"
   bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool failIfExists); //false=overwrite, true don't overwrite existuing file
        //bool CopyFileA(string lpExistingFileName, string lpNewFileName, bool failIfExists); //false=overwrite, true don't overwrite existuing file
        int  DeleteFileW(string file); //https://forum.mql4.com/11524  https://www.forex-tsd.com/f6/coding-help-2432-print/index50.html
        int  GetEnvironmentVariableW(string lpName, string& lpBuffer, int nSize);
        bool SetEnvironmentVariableW(string lpName, string text);
        bool CreateDirectoryW( string TdsFolder, int lpSecurityAttributes);
        bool RemoveDirectory( string dirPath );
   int  SetFilePointer(int FileHandle, int Distance, int PassAsZero, int FromPosition);
...
 
gooly:

Due to the fact the mt4 has now switched from ASCII to ANSI this article it not valid any more!

To use Windows' kernel3.dll function use CreateFileW(..) and the equiv. functions:

Perfect! This works!

Thanks! 

Reason: