How to right call CreateFileMappingA for share memory on MT5?

 

i want to build communication between two MT5 via share memory,

i try to call windows API, but i faced GetLastError() =6 error, invalid handle


a part code as follow:

#define INVALID_HANDLE_VALUE            -1
#define BUF_SIZE                       256
#define FILE_MAP_READ                    4
#define PAGE_READWRITE              0x0004
#define FILE_MAP_ALL_ACCESS        0xf001F

#import "kernel32.dll"
   int   CreateFileMappingA(int,int,int,int,int,const string);
   int   MapViewOfFile(int,int,int,int,int);
   int   OpenFileMappingA(int,int,const string);
   int   UnmapViewOfFile(const int);
   int   CloseHandle(int);
   int   GetLastError();
#import

int   mt5_write_hMapFile,
      mt5_read_hMapFile,
      mt5_pBuf,
      mt5_data;

int OnInit()
{
    apply_memory_init();
}

void apply_memory_init()
{
    string szName="Global\\MT5_PriceMap";
    datetime init_time= TimeCurrent();

    mt5_write_hMapFile = kernel32::CreateFileMappingA (-1, NULL, 4, 0, 32, szName);
    Print("mt5_write_hMapFile1:",mt5_write_hMapFile);

    while (!IsStopped() && mt5_write_hMapFile==0)
    {
       mt5_write_hMapFile = kernel32::CreateFileMappingA(INVALID_HANDLE_VALUE,0,PAGE_READWRITE,0,BUF_SIZE,szName);
       if(mt5_write_hMapFile==0 && TimeCurrent() > init_time + 30)
       {
           Alert("CreateFileMapping failed!");
           Print("CreateFileMapping_error:",kernel32::GetLastError());
           return;
       }
       Sleep(100);
    }
    Print("mt5_write_hMapFile2: ",mt5_write_hMapFile);

    mt5_pBuf = kernel32::MapViewOfFile(mt5_write_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE); 
    Print("mt5_pBuf: ",mt5_pBuf);

    if(mt5_pBuf==0) 
    {
        Alert("Map View failed!");
        Print("MapViewOfFile_error:",kernel32::GetLastError());
        return;
    }
}

run result:

mt5_write_hMapFile=0,

GetLastError() =6, invalid handle.

who can help me?

How to right call CreateFileMappingA on MT5?

 

How to right call CreateFileMappingA on MT5?

Use CreateFileMappingW , MT5 is using Unicode.

 

xdop:
#import "kernel32.dll"
   int   CreateFileMappingA(int,int,int,int,int,const string);
   int   MapViewOfFile(int,int,int,int,int);
   int   OpenFileMappingA(int,int,const string);
   int   UnmapViewOfFile(const int);
   int   CloseHandle(int);
   int   GetLastError();
#import

  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Since February 3, 2014 (Build 600) Strings are Unicode

    What's New in MQL4 - MQL4 Documentation

    Why are you using A type calls?
 

first topic on the forum,

i have edited it,

thanks two guys.

Reason: