Buggish interaction between winfile_v600.mqh and PrintFormat

 

Hallo people,


I was testing in mql5 the mql5-slightly-modified winfile_v600.mqh library provided to the community by the Mighty JC (https://www.mql5.com/en/forum/150831).


Now look at this strange behavior: if I write something like

#include <winfile_v600.mqh>

void OnStart() {
  int hdl = OpenNewFileForWriting("C:/Users/sprea/Test/TestWinfile600.txt", true);
  if (hdl == INVALID_HANDLE_VALUE) {
    Print("Cannot create a valid handle");
    return;
  }
  WriteToFile(hdl, "This is a test string");
  CloseFile(hdl);

the script works wonderfully. The TestWinfile600.txt file is created in its directory and the test string inserted in it...

Now let's make just a VERY LITTLE MODIFICATION:

#include <winfile_v600.mqh>

void OnStart() {
  int hdl = OpenNewFileForWriting("C:/Users/sprea/Test/TestWinfile600.txt", true);
  if (hdl == INVALID_HANDLE_VALUE) {
    Print("Cannot create a valid handle");
    return;
  }
  PrintFormat("Received handle is %i", hdl);  //HERE IT IS!
  WriteToFile(hdl, "This is a test string");
  CloseFile(hdl);
}

Well: this is what you get in your Terminal Expert log:

QO      0       16:33:39.564    PrintFormatAndWinfile (EURUSD,H1)       Received handle is 780
KD      2       16:33:39.840    PrintFormatAndWinfile (EURUSD,H1)       Access violation at 0x00007FFA37ECE3D7 write to 0x0000021C00000000
GK      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE320 48895C2410        mov        [rsp+0x10], rbx
ID      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE325 4889742418        mov        [rsp+0x18], rsi
KI      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE32A 4C894C2420        mov        [rsp+0x20], r9
OG      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE32F 57                push       rdi
EK      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE330 4883EC60          sub        rsp, 0x60
PK      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE334 498BD9            mov        rbx, r9
DG      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE337 4C8BDA            mov        r11, rdx
CI      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE33A 488BF9            mov        rdi, rcx
OE      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE33D 33C0              xor        eax, eax
GD      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE33F 4889442450        mov        [rsp+0x50], rax
JK      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE344 4889442458        mov        [rsp+0x58], rax
MJ      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE349 4533D2            xor        r10d, r10d
NF      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE34C 4D85C9            test       r9, r9
KM      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE34F 7403              jz         0x7ffa37ece354
FO      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)       
FF      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE351 458911            mov        [r9], r10d
OE      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE354 83F9F4            cmp        ecx, 0xf4
FD      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE357 0F831F010000      jae        dword 0x7ffa37ece47c
KK      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE35D 488BB42490000000  mov        rsi, [rsp+0x90]
GJ      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE365 4C89542440        mov        [rsp+0x40], r10
PJ      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE36A 488BCF            mov        rcx, rdi
MD      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE36D 4885F6            test       rsi, rsi
QI      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE370 7565              jnz        0x7ffa37ece3d7
PE      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)       
KO      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE372 4C89542438        mov        [rsp+0x38], r10
GM      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE377 4489442430        mov        [rsp+0x30], r8d
FP      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE37C 4C895C2428        mov        [rsp+0x28], r11
LP      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE381 488D442450        lea        rax, [rsp+0x50]
LN      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE386 4889442420        mov        [rsp+0x20], rax
DP      2       16:33:39.842    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE38B 4533C9            xor        r9d, r9d
EO      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE38E 4533C0            xor        r8d, r8d
JQ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE391 33D2              xor        edx, edx
LS      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE393 48FF15AEA31700    call       qword near [rip+0x17a3ae]  ; UnhandledExceptionFilter (kernelbase.dll)
MD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE39A 0F1F440000        nop        [rax+rax+0x0]
MK      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE39F 8BC8              mov        ecx, eax
NE      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3A1 3D03010000        cmp        eax, 0x103
CL      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3A6 0F846E080700      jz         dword 0x7ffa37f3ec1a
GH      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       
HI      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3AC 85C9              test       ecx, ecx
EG      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3AE 0F88B5000000      js         dword 0x7ffa37ece469
DI      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3B4 4885DB            test       rbx, rbx
KD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3B7 7406              jz         0x7ffa37ece3bf
GF      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       
PN      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3B9 8B442458          mov        eax, [rsp+0x58]
IQ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3BD 8903              mov        [rbx], eax
EN      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3BF B801000000        mov        eax, 0x1
LQ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3C4 488B5C2478        mov        rbx, [rsp+0x78]
EQ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3C9 488BB42480000000  mov        rsi, [rsp+0x80]
LS      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3D1 4883C460          add        rsp, 0x60
CP      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3D5 5F                pop        rdi
LN      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3D6 C3                ret        
IP      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       
DS      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)          crash -->  00007FFA37ECE3D7 48C70603010000    mov        qword [rsi], 0x103
CR      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3DE 8B4610            mov        eax, [rsi+0x10]
PD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3E1 89442470          mov        [rsp+0x70], eax
PJ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3E5 8B4614            mov        eax, [rsi+0x14]
CD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3E8 89442474          mov        [rsp+0x74], eax
CD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3EC 488B5618          mov        rdx, [rsi+0x18]
NI      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)                     00007FFA37ECE3F0 F6C201            test       dl, 0x1
IO      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       
LD      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       00: 0x00007FFA37ECE3D7
CM      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       01: 0x0000021C6D6D0010
NS      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       02: 0x00007FFA00000016
EJ      2       16:33:39.843    PrintFormatAndWinfile (EURUSD,H1)       

Is there any Guru explaining what is going on here? If I remove the PrintFormat statement everything turns back to normal. Is this a bug in PrintFormat? Is this a strange interaction with the Win32 API? How to avoid it?

The thing seems to be quite nasty, because I wanted to include these WIN32 API functions into my library. And it looks like that ANY PrintFormat statement around the library will cause the winfile functions getting scrambled and crashing memory! (Of course I don't want to re-write the whole library without them, and... yes it would be a huge problem for encapsulation...).

[Without telling about how long it took tracing the problem down to this... bug?]

I am using MT5 5 Build 2650 on a Windows10 mounted on 64-bit (x64) Intel.


Thank you so much.

How to Read and write data from out side the metatrader folder
How to Read and write data from out side the metatrader folder
  • 2014.04.01
  • www.mql5.com
Hi everyone,Can you please help me hw to read and write file from external folders can you show me some small code so that i understand...
 

I have now posted this question on the Russian forum, both in English and in Russian.

Let's see if they answer.

Best regards to all!

 

It was the reply from one user in Russian forum (this user reproduced your issue, and he is asking admins to fix it):

Forum on trading, automated trading systems and testing trading strategies

New Version of MetaTrader 5 Build 2650: Background Chart Loading and Improvements in MQL5 Profiler

fxsaber , 2020.10.22 07:31

This strange crash is reproducible on the b2560. Interestingly, after the crash, the created file cannot be deleted - it is used by another process. It would be nice to fix that too.

for information.

 
That's great, indeed! Congratulations, very fast reaction time. Then... it is a bug.
 

Forum on trading, automated trading systems and testing trading strategies

New Version of MetaTrader 5 Build 2650: Background Chart Loading and Improvements in MQL5 Profiler

Slava , 2020.10.22 12:44

The very first thing is that the FileHandle type must be long, 64-bit, since you are working on a 64-bit system.

 #import "kernel32.dll"
   long CreateFileW( string Filename, uint AccessMode, int ShareMode, int PassAsZero, int CreationMode, int FlagsAndAttributes, int AlsoPassAsZero);
   int   WriteFile( long FileHandle, uchar & Buffer[], int BufferLength, int & BytesWritten[], int PassAsZero);
   int   CloseHandle( long FileHandle);
#import

 
Sergey Golubev:

Thank you so much, but this does not resolve the problem, unfortunately.

 
Fab:

Thank you so much, but this does not resolve the problem, unfortunately.

Hope they will fix it in the next beta build (because it was the reply from MQ programmer).
If not so you can repeat your post with this issue.
 
Fab:

Thank you so much, but this does not resolve the problem, unfortunately.

More reply:

Forum on trading, automated trading systems and testing trading strategies

New Version of MetaTrader 5 Build 2650: Background Chart Loading and Improvements in MQL5 Profiler

Slava , 2020.10.23 08:47

 #import "kernel32.dll"
long CreateFileW( string Filename, uint AccessMode, int ShareMode, long PassAsZero, int CreationMode, int FlagsAndAttributes, long AlsoPassAsZero);
int   WriteFile( long FileHandle, uchar & Buffer[], int BufferLength, int & BytesWritten[], long PassAsZero);
int   CloseHandle( long FileHandle);
#import

 

Ah ah ah ah! THIS now resolves the problem!

So, finally, the problem resolves by understanding the correct datatypes to be transferred to the win32 API.

Perhaps then, for the people that could need it in the future, these are the discoveries of this exercise. And I add also the BOOL story, that is present in other posts, trying to collect information in one place:

DATATYPE CORRESPONDENCE:

Win32 API Mql5 run on 32-bit system Mql5 run on 64-bit system
HANDLE int (4 bytes) long (8 bytes)
LPSECURITY_ATTRIBUTES int (4 bytes) long (8 bytes)
LPOVERLAPPED
int (4 bytes) long (8 bytes)
 BOOL (4 bytes)  NOT bool (1 byte) BUT int 0 = false; int >0 = true

  NOT bool (1 byte) BUT int 0 = false; int >0 = true

If some expert wants to go on in filling this table it would be great. Alternatively, if anybody knows where to find such kind of information in a more professional and complete way, I think it could be useful for the whole community posting such a link in a very visible way on the forum!


Thank you a lot for your very nice support!

 
Fab #:

Ah ah ah ah! THIS now resolves the problem!

So, finally, the problem resolves by understanding the correct datatypes to be transferred to the win32 API.

Perhaps then, for the people that could need it in the future, these are the discoveries of this exercise. And I add also the BOOL story, that is present in other posts, trying to collect information in one place:

DATATYPE CORRESPONDENCE:

Win32 API Mql5 run on 32-bit system Mql5 run on 64-bit system
HANDLE int (4 bytes) long (8 bytes)
LPSECURITY_ATTRIBUTES int (4 bytes) long (8 bytes)
LPOVERLAPPED
int (4 bytes) long (8 bytes)
 BOOL (4 bytes)  NOT bool (1 byte) BUT int 0 = false; int >0 = true

  NOT bool (1 byte) BUT int 0 = false; int >0 = true

If some expert wants to go on in filling this table it would be great. Alternatively, if anybody knows where to find such kind of information in a more professional and complete way, I think it could be useful for the whole community posting such a link in a very visible way on the forum!


Thank you a lot for your very nice support!

Hello
I have applied this suggestion and it works for me, to read, (possibly), because when I print what I read, I receive some characters in Chinese

If I use the OpenFile version that comes by default in MQL5, something similar happens to me, but in OPENFILE, I can add the FILE_ANSI flag and it solves it

So I ask, how can I add FILE_ANSI or something similar in the FILE_CREATE_W or READ_FILE of Kernel32.dll??


I will be very attentive


Thank you

 
Use the "A" versions of the functions, e.g. CreateFileA, and MAKE sure your input file is encoded as ASCII, use Notepad to check.