Examples: File Operations via WinAPI

 

New article File Operations via WinAPI has been published:

Environment MQL4 is based on the conception of safe "sandbox": reading and saving files using the language are allowed in some predefined folders only. This protects the user of MetaTrader 4 from the potential danger of damaging important data on the HDD. However, it is sometimes necessary to leave that safe area. This article is devoted to the problem of how to do it easily and correctly.

MQL4 is designed in such a way that even incorrectly written programs are unable to mistakenly delete data from the hard disk. The functions used for file reading and writing operations can work in the following directories only (quote):

  • /HISTORY/<current broker> - especially for the FileOpenHistory function;
  • /EXPERTS/FILES - common case;
  • /TESTER/FILES - especially for testing.
Working with files from other directories is prohibited.
If you still need to work outside the directories (defined for safety reasons), you can call the functions of Windows OS. For this purpose, the functions of API represented in kernel32.dll library are widely used.

Author: MetaQuotes Software Corp.

 

Thank you Rashid for a well presented article.

Annotated text with colors and vids are a well received addition in the issue of learning imho.

My understanding is heightened by your implementation of these tools.

The final "Let's modify function start() to check the new features:" is very concise and elegant ;)

Warm Regards...

 

I always have a bad feeling concerning file interfaces - although I use them more or less without trouble.

But isn't there any way to pass data from one EA to another (or some other app) directly through the RAM? Like with some sort of RPC/RMI or whatever!?

 
schnappi:

I always have a bad feeling concerning file interfaces - although I use them more or less without trouble.

But isn't there any way to pass data from one EA to another (or some other app) directly through the RAM? Like with some sort of RPC/RMI or whatever!?

See 'Обмен данными между MQL Советником и Win приложением' and 'Нужна помощь. Ошибка при использовании DDE из DLL-ки на С'
 

Hello Rashid,

Your WinAPI file operations are very useful. Thank you so much. I incorporated your code into my Correlation indicator (which I will share when completed). However, your ReadFile() function seems to return a string from a text file dropping all the non-printable characters. In my application I NEED the non-printable characters "/r/n" to signal end of line. Because my indicator requires the user input (currency pair) be validated against the broker symbols. My set file does not use ";" delimiter; it is only one column of symbols in a plain text file. I actually tried to use the standard FileOpen() in FILE_CSV mode but it only allows the use of ';' as the delimiter (see attached).

Any help is much appreciated.

Scott

 

Hi Programmers!


How can I use the FileWrite function for update a whole file? So, I would like to delete the all characters of the previous content of the file.


There is one way - I think. I could delete the file before the write.

 

Hi,

int _lcreat (string path, int attrib);

This does not work on MT4 build 600, 604.

Please help! Thanks!

 

why using _lopen and friends that are still there for backward compatibility and msdn doesnt even document them anymore? you can use CreateFile from kernel32 or fopen from msvcrt..

also you should check if return value is -1 ( HFILE_ERROR ) which is the value if the function fails, checking if is less than 0 it's pretty bad since on error the value is -1 and not less than 0

 
As mentioned before, using _lopen is broken since build 600, because DDL calls are now using Unicode strings, so either you have to use alternative calls (such as CreateFileW), or convert the ANSI string into Unicode.

Please update the article page.
GrabWeb not working on MT4 Build 600 (thili55) - MQL4 forum
GrabWeb not working on MT4 Build 600 (thili55) - MQL4 forum
  • www.mql5.com
GrabWeb not working on MT4 Build 600 (thili55) - MQL4 forum
Reason: