Correct signature in mt5 for kernel32 import

 

Hi.

Use for years my script in mt4 and mt5. Now I resolve to change my windows 7 64bit to windows 10 64bits.

Only in mt4 working fine. In mt5 I have this strange erros:

2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      Access violation at 0x00007FFB6006605C read to 0x0000020200000010
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)         crash -->  00007FFB6006605C 837A1000          cmp        dword [rdx+0x10], 0x0
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB60066060 448D4102          lea        r8d, [rcx+0x2]
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB60066064 488B4208          mov        rax, [rdx+0x8]
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB60066068 410F45C8          cmovnz     ecx, r8d
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB6006606C 488945B8          mov        [rbp-0x48], rax
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB60066070 894DB0            mov        [rbp-0x50], ecx
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)                    00007FFB60066073 4885C0            test       rax, rax
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      00: 0x00007FFB6006605C
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      01: 0x00000202223216C3
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      02: 0x00000070D15FE2F4
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      03: 0x00000070D15FE3FC
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      04: 0x00000070D15FE2E4
2020.03.27 22:59:36.604 teste3 (EURUSD,H1)      05: 0x00007FF7F45043D8


I am using kernel32 functions, so I think that here is the problem. I think that my signature to import the function in 64bit is wrong...

#import "kernel32.dll"

int CreateNamedPipeW(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeOut, int security);

int ConnectNamedPipe(int hPipe, int lpOverlapped);

int ReadFile(int hPipe, int& inBuffer[], int NumberOfBytesToRead, int& bytesRead[], int lpOverlapped);

int WriteFile(int hPipe, string sBuffer, int NumberOfBytesToWrite, int& bytesWritten[], int lpOverlapped);

int FlushFileBuffers(int hPipe);

int DisconnectNamedPipe(int hPipe);

int CloseHandle(int hPipe);

//int MulDiv(string X, int N1, int N2);

int CreateFileW(string name, int desiredAccess, int SharedMode, int security, int creation, int flags, int templateFile);

#import


In this thread Alain help correcting the signature for 

CreateFileMappingW

https://www.mql5.com/en/forum/320327


Make sense? i dont know whats more I can test. I rebuild in parts my code, break in small parts and the problem persist. My hardware working well, I tested with diagnostic tester...


Thanks



EDIT1: I can use  CreateNamedPipeW, ConnectNamedPipe and  ReadFile without error. In the WriteFile crashing:

fWrite = CreateFileW(FullName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0);
WriteFile(fWrite, Buffer_Old, 2 * szData, BytesWritten, NULL);

EDIT2: Definely the problem is in WriteFile. There no erros in CreateFileW. But when the code come in WriteFile Access Vionalation happens!

MQL5 CreateFileMappingW (#import "kernel32.dll") Invalid Handle ??
MQL5 CreateFileMappingW (#import "kernel32.dll") Invalid Handle ??
  • 2019.08.18
  • www.mql5.com
Hi, can someone tell me why I get... Thanks...
 

I tried change to long:

int WriteFile(int hPipe, string sBuffer, long  NumberOfBytesToWrite, long& bytesWritten, int lpOverlapped);

the same problem...


EDIT1: Ops, my problem appear to be in CreateFileW, I am receiving  INVALID_HANDLE_VALUE...


I am receiving the error 87, ERROR_INVALID_PARAMETER

int fWrite= CreateFileW(FullName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0);


EDIT2: I am going...I junto dont know for where...

ERROR_ACCESS_DENIED
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
The CLR_NONE constant is used to outline the absence of color, it means that the graphical object or graphical series of an indicator will not be plotted. This constant was not included into the Web-color constants list, but it can be applied everywhere where the color arguments are required. The EMPTY_VALUE constant usually corresponds...
 

In your MT5 installation path, there is a folder MQL5\Include\WinAPI

It contains most of the needed signature.

If you need help please post your code to reproduce the issue.

 
//+------------------------------------------------------------------+
//|                                               TesteWriteFile.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#define INVALID_HANDLE_VALUE        -1
#define NMPWAIT_USE_DEFAULT_WAIT    0
#define PIPE_BUFFER_SIZE            255
#define PIPE_TYPE_MESSAGE           4
#define PIPE_READMODE_MESSAGE       2
#define PIPE_WAIT                   0
#define FILE_FLAG_OVERLAPPED        0x40000000
#define PIPE_ACCESS_DUPLEX          3
#define PIPE_UNLIMITED_INSTANCES    255
#define OPEN_ALWAYS                 4
#define CREATE_ALWAYS               2
#define GENERIC_EXECUTE                  0x20000000
#define GENERIC_ALL                      0x10000000

#define FILE_SHARE_READ             0x00000001
#define FILE_SHARE_WRITE            0x00000002
#define GENERIC_READ                0x80000000
#define GENERIC_WRITE               0x40000000
#define OPEN_EXISTING               0x00000003
#define FILE_ATTRIBUTE_NORMAL       128





#import "kernel32.dll"
int CreateNamedPipeW(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeOut, int security);
int ConnectNamedPipe(int hPipe, int lpOverlapped);
bool ReadFile (int fileHandle,char& buffer[],int bytes,int& numOfBytes[],long overlapped);
int WriteFile(int hPipe, string sBuffer, long  NumberOfBytesToWrite, int& bytesWritten, int lpOverlapped);
int FlushFileBuffers(int hPipe);
int GetFileSize(int, int);
int DisconnectNamedPipe(int hPipe);
int CloseHandle(int hPipe);
int CreateFileW(string name, int desiredAccess, int SharedMode, int security, int creation, int flags, int templateFile);
int GetLastError();
#import

int    hPipe;       
bool   success;
string inString = "";
int err1; 
string paridade;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
       string PipeName = "\\\\.\\pipe\\Perpendicular_Client_PEPPER5_";
       int    PipeMode = PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT;
       hPipe      = CreateNamedPipeW(PipeName, PIPE_ACCESS_DUPLEX, PipeMode, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL);
       if (hPipe == INVALID_HANDLE_VALUE)
       {
           err1 = GetLastError();
           Print("CreateNamedPipe failed");
       }
       else
       {
           Print("Named Pipe was created");
       }
       while (true)
       {
           Print("standy by");
           bool fConnected = ConnectNamedPipe(hPipe, NULL) != 0;
           Print("Conectou PipeServer");
           if (fConnected)
           {
               int  bytesRead[1];
               char inb[PIPE_BUFFER_SIZE];
               int  inBuffer[PIPE_BUFFER_SIZE];
               bool fSuccess = ReadFile(hPipe, inb, PIPE_BUFFER_SIZE-1, bytesRead, NULL) != 0;
              
               if (!fSuccess || (bytesRead[0] == 0))
               {
                   break;
               }
               inString = "";
               for (int i = 0; i < bytesRead[0]; i++)
               {
                   inString = inString + CharToString(inb[i]);
               }
               Print("instring= " + inString);
                      string path = "D:\\Forex\\PEPPER5\\Dados\\EURUSD\\Um.csv";
                      
                      int ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
                      int accessMode = GENERIC_READ | GENERIC_WRITE;
                     //int CreateFileW(string name, int desiredAccess, int SharedMode, int security, int creation, int flags, int templateFile);

                      int fWrite= CreateFileW(path, 0, ShareMode, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                      //int fWrite= CreateFileW(FullName, FILE_APPEND_DATA | FILE_READ_DATA, ShareMode, NULL, OPEN_ALWAYS, 0, NULL);
                      Print(kernel32::GetLastError());
                      if (fWrite == INVALID_HANDLE_VALUE)
                      {
                           Print("Unable to open " + path + " for writing");
                           
                      }
                      else
                      {
                           Print("5"); 
                           string DataBuffer = "This is some test data to write to the file.";
                           long dwBytesToWrite = (long)StringLen(DataBuffer);
                           int dwBytesWritten = 0;
                           Print("6"); 
                           WriteFile( 
                             fWrite,           // open file handle
                             DataBuffer,      // start of data to write
                             dwBytesToWrite,  // number of bytes to write
                             dwBytesWritten, // number of bytes that were written
                             NULL);            // no overlapped structure

                        //szData = StringLen(Buffer_Old[0]);
                        Print("7"); 
                         
                        //bool teste = WriteFile(fWrite, Buffer_Old, szData, BytesWritten, NULL);
                       //if(!teste){
                        //   Print(GetLastError());
                       //}
                        CloseHandle(fWrite);
                           Print("8");
                     } //Fecha o Else do if (!IsValidFileHandle(fWrite))
           } 
           FlushFileBuffers(hPipe);
           DisconnectNamedPipe(hPipe);
           GlobalVariablesFlush();
           ResetLastError();
           break;
           Sleep(250);
        }//while (true)
        Print("Saiu");
        CloseHandle(hPipe);
  }
//+------------------------------------------------------------------+
       

Ok, soory for this mess. My problem with CreateFileW was solved correcting the flag OPEN_EXISTING...

So the problem with the WriteFile persist

 

Is hard here...I guess that the problem is really in CreatefileW because for the code not return the error Access_Denied I changed my code but now I think that I cann't write so generating the error Access Violation.

 
void OnStart()
  {
//---
       while (true)
       {
                      string path = "D:\\Forex\\PEPPER5\\Dados\\EURUSD\\Um.csv";
                      //string path = "C:\\Users\\alexa\\AppData\\LocalLow\\Um.csv";
                    
                      int ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
                      int accessMode = GENERIC_READ | GENERIC_WRITE;

                      int fWrite= CreateFileW(path, GENERIC_READ | GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS , 0, NULL);
                      
                      if (fWrite == INVALID_HANDLE_VALUE)
                      {
                           Print("Unable to open " + path + " for writing");
                           Print("errorA " + kernel32::GetLastError());
                      }
                      else
                      {
                           Print("5a"); 
                           string DataBuffer = "This is some test data to write to the file.";
                           long dwBytesToWrite = (long)StringLen(DataBuffer);
                           int dwBytesWritten = 0;
                           Print("6a"); 
                           long numwritten; 
                           
                           bool test = WriteFile(fWrite, DataBuffer, 10, numwritten, 0);
                           if(test == false){
                                 Print("errorB " + kernel32::GetLastError());
                           }

                        CloseHandle(fWrite);
                           Print("8a");
                     } //Fecha o Else do if (!IsValidFileHandle(fWrite))
          // } 
           FlushFileBuffers(hPipe);
           DisconnectNamedPipe(hPipe);
           GlobalVariablesFlush();
           ResetLastError();
           break;
           Sleep(250);
        }//while (true)
        Print("Saiu");
        CloseHandle(hPipe);
  }

I created this new code for testing only the CreateFileW and the same error, 5...Access_Denied

 
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
    string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL5\\Files\\"+"Um.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Cуdigo de erro ",GetLastError());
     }
//--- maneira correta de trabalhar no "sandbox arquivo"
   ResetLastError();
   Print("ok");
  }

I tried creating this code to test other way and NO...no working....I receive the erros 5002....

I formated my pc just two days ago and install Windows 10...I am new use in W10. Maybe I need to do something? 

-I tried to change the files user rights...

-Folder user rights...

-Run mt5 as administrator

-Compatible mode as Windows7


My MT5 Build is 2361 from the Pepperstone Broker

 
void OnStart()
  {
   int filehandle=FileOpen("Um.csv",FILE_WRITE|FILE_CSV|FILE_COMMON);
   if(filehandle<0)
     {
      Print("Cуdigo de erro ",GetLastError());
     }
     FileWrite(filehandle,"my test");
     FileClose(filehandle); 
   ResetLastError();
   Print("ok");
  }

This code work in I wanted...But I need to save in D: and specific folder. I will read about this. Maybe the mt5 doesnt write out that this Common folder...

 
void OnStart()
  {
//---
       while (true)
       {
                      int ShareMode = FILE_SHARE_READ ;
                      int accessMode =  GENERIC_WRITE | GENERIC_READ;
                      //TerminalInfoString(TERMINAL_DATA_PATH)
                      //TerminalInfoString(TERMINAL_COMMONDATA_PATH)
                      string path=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\Um.csv";
                      int fWrite= CreateFileW(path , GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, NULL);
                      if (fWrite == INVALID_HANDLE_VALUE)
                      {
                           Print("Unable to open " + path + " for writing");
                           Print("errorA " + kernel32::GetLastError());
                      }
                      else
                      {
                           CloseHandle(fWrite);
                           Print("8a");
                     } //Fecha o Else do if (!IsValidFileHandle(fWrite))
          // } 
           FlushFileBuffers(hPipe);
           DisconnectNamedPipe(hPipe);
           GlobalVariablesFlush();
           ResetLastError();
           break;
           Sleep(250);
        }//while (true)
        Print("Saiu");
        CloseHandle(hPipe);
  }

Not working with this code too. Here I tried to change folder to the common folder or terminal path but I continue receiving error 5!

 
 string path = "\\\\.\\D:\\Forex\\Broker\\Dados\\Um.csv";                      
               
 int fWrite= CreateFileW(path  , accessMode, 0, NULL, CREATE_NEW , FILE_ATTRIBUTE_NORMAL, NULL);

With this code I can create a new file. But if it exist, error 80...

 

I give up...There are somthing more bigger than me...I tried unistall my MT5 Pepperstone and I received the error "This app cant run in your computer" 

The app is unistall.exe

So now I cant unistall too...

Reason: