problem while using ReadFile WinAPI function...

 

Hello folks, I have spent all the afternoon trying to get the following code works.

After tried it with no success, I built a completely different code with Windows APIs to use the correct names from Kernel32 library but also didn't work.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx

Then, I write this post with the initial code I got from:

https://www.mql5.com/en/articles/1540

I need this custom function to read the entire content of a TXT file on whatever location on the file system.

With the following erroneous script I get:

content: []
Error opening file D:\file.txt

when I should get:

content: [Hello world!]
The script code is ready to copy/paste/run:
#define OF_READ               0
#define OF_WRITE              1
#define OF_READWRITE          2
#define OF_SHARE_COMPAT       3
#define OF_SHARE_DENY_NONE    4
#define OF_SHARE_DENY_READ    5
#define OF_SHARE_DENY_WRITE   6
#define OF_SHARE_EXCLUSIVE    7

#import "kernel32.dll"
        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);
#import

string ReadFile(string path) {

        int handle = _lopen(path, OF_READ);
        int read_size = 50;
        string char50 = "x                                                 ";

        if (handle < 0) {
                Print("Error opening file ", path);
                return ("");
        }
        int result = _llseek(handle, 0, 0);
        if (result < 0) {
                Print("Error placing the pointer");
                return ("");
        }

        string buffer = "";
        int count = 0;
        int last = 0;

        result = _lread(handle, char50, read_size);
        while (result > 0 && result == read_size) {
                buffer = buffer + char50;
                count++;
                result = _lread(handle, char50, read_size);
                last = result;
        }
        printf("The last read block has the size of %d bytes:", last);
        char50 = StringSubstr(char50, 0, last);
        buffer = buffer + char50;
        result = _lclose(handle);
        if (result < 0)
                Print("Error closing file ", path);
        return (buffer);
}

void OnStart() {
        // the following is a TXT file with the content: "Hello world!"
        string content = ReadFile("D:\\file.txt");
        printf("content: [%s]", content);
}

Thank you!

File Operations via WinAPI
File Operations via WinAPI
  • 2008.08.15
  • MetaQuotes Software Corp.
  • www.mql5.com
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.
 

i can only think of two things

one is that import dll is disabled and 2 the file path is incorrect/does not exist at the specified location.

i would first try to reach a file in the local directory then crawl my way out to where i need to be sometimes specifying the correct paths can be advantageous.

 
Marco vd Heijden:

i can only think of two things

one is that import dll is disabled and 2 the file path is incorrect/does not exist at the specified location.

i would first try to reach a file in the local directory then crawl my way out to where i need to be sometimes specifying the correct paths can be advantageous.

- the option: "Options / Expert Advisors / Allow DLL imports" is checked

- the path is correct

¿could you check by your self that script with DLL allowed and a valid TXT file with content: "Hello world!"?

Thank you Marco!

 
so i get the same outcome but i did notice there were issues with access rights to the file so make sure u run as administrator.
 
Marco vd Heijden:
so i get the same outcome but i did notice there were issues with access rights to the file so make sure u run as administrator.

I accessed as administrator (runned MT5 as admin) :(

but if you get the same output as administrator then that's not te problem.


The "_lopen" function works correctly because I get a handle. Also "_llseek" rise no error,

but the problem is the function: "_lread".

 

http://winapi.freetechsecrets.com/win32/WIN32lread.htm

The _lread function reads data from the specified file. This function is provided for compatibility with 16-bit versions of Windows.

Win32-based applications should use the ReadFile function.

http://winapi.freetechsecrets.com/win32/WIN32ReadFile.htm

BOOL ReadFile(
HANDLE hFile,
// handle of file to read
LPVOID lpBuffer,
// address of buffer that receives data
DWORD nNumberOfBytesToRead,
// number of bytes to read
LPDWORD lpNumberOfBytesRead,
// address of number of bytes read
LPOVERLAPPED lpOverlapped
// address of structure for data
);
 


_lread • Win32 Programmer's Reference
_lread • Win32 Programmer's Reference
  • winapi.freetechsecrets.com
The _lread function reads data from the specified file. This function is provided for compatibility with 16-bit versions of Windows. Win32-based applications should use the ReadFile function. UINT _lread( Parameters hFile Identifies the specified file. lpBuffer Pointer to a buffer that contains the data read from the file. uBytes Specifies...
 
Marco vd Heijden:

http://winapi.freetechsecrets.com/win32/WIN32lread.htm

The _lread function reads data from the specified file. This function is provided for compatibility with 16-bit versions of Windows.

Win32-based applications should use the ReadFile function.

http://winapi.freetechsecrets.com/win32/WIN32ReadFile.htm

BOOL ReadFile(
HANDLE hFile,
// handle of file to read
LPVOID lpBuffer,
// address of buffer that receives data
DWORD nNumberOfBytesToRead,
// number of bytes to read
LPDWORD lpNumberOfBytesRead,
// address of number of bytes read
LPOVERLAPPED lpOverlapped
// address of structure for data
);
 

Thank you Marco!