Shared memory issue

 

Hi fellows coders,
I tried to use Shared memory (through File Mapping), by first setting up the required space using CreateFileMapping.
But i kept getting error 6 <=> INVALID_HANDLE ( the error is collected through the following function kernel32::GetLastError() ).

Even tho i tried multiples variations.
Below i will attach the code of each variation i used.

May be i'm just brain dead, but if you spot the issues, let me know ! THANKS

//-----------------------------------------------------------------------------------------------------Variation 1

#define HANDLE                                  int
#define LPSECURITY_ATTRIBUTES           	int
#define DWORD                                   int 
#define LPCSTR                                  const char&
#define BOOL                           		int	

#define MaximumSizeHigh                         0
#define MaximumSizeLow                          256

#define INVALID_HANDLE_VALUE                    (HANDLE)(~0)
#define BUF_SIZE                                256
#define PAGE_READWRITE                          0x00004  


string namefilemapping 	= "Global\\SharedMemory";
int HandleFilMapping 	= NULL;

#import "kernel32.dll"
   HANDLE CreateFileMappingA
   (HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCSTR lpName[]);
   int GetLastError();
   BOOL CloseHandle(HANDLE hObject);
#import

void OnTick()
{   
   if(HandleFilMapping==NULL)
   HandleFilMapping = CreateShardMemorySpace();
}

int CreateShardMemorySpace()
{    
  int  hFileMapping = 0;
  char nameFileMapping[]; StringToCharArray(namefilemapping,nameFileMapping);
  nameFileMapping[ArraySize(nameFileMapping)-1]='\0';
  hFileMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,MaximumSizeHigh,MaximumSizeLow,nameFileMapping);
  if (hFileMapping==NULL) { Print("Creation of the file mapping failed, sleep 2s, err : "+ IntegerToString(kernel32::GetLastError())); Sleep(2000); return NULL; }
  Print("Creation succed");
  return hFileMapping;
}


//-----------------------------------------------------------------------------------------------------Variation 2
// In the variation 2, here is what i changed

#define LPCSTR                                          const string& //FROM const char& TO const string&
#import "kernel32.dll"
   HANDLE CreateFileMappingW //From the Ansi function (CreateFileMappingA) to the unicode one (CreateFileMappingW)
   (HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCSTR lpName); // FROM lpName[] TO lpName
#import
 hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,MaximumSizeHigh,MaximumSizeLow,namefilemapping); //I directly used the string value (namefilemapping), 
																instead of converting it to char array (nameFileMapping)
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...
[Deleted]  
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Yerim Rayann Gon Diarra:
CreateFileMappingA

I am not familiar with that particular Windows API, but you can read its documentation here: CreateFileMappingA function

CreateFileMappingA function (winbase.h) - Win32 apps
CreateFileMappingA function (winbase.h) - Win32 apps
  • 2022.07.27
  • GrantMeStrength
  • learn.microsoft.com
Creates or opens a named or unnamed file mapping object for a specified file. (CreateFileMappingA)
 
I found the issue, hope it will help others.
I changed all the integer type (in the define section) to long type. 
Since my mt5 run on a 64-bits architecture, the INVALID_HANDLE_VALUE should have been casted to long, because windows was expecting a long value. 
But it's only the case for mt5 that run on 64-bits architecture, if you plate-forme run on 32-bites architecture you will be fine using int. 
To know the architecture of your Plateform you can use the built in function : IsX64() 

Also the issue was extending to the use of the MapViewOfFile function. 
After calling CreateFileMapping (), Windows was sending back handle that were 64-bites. Thus those handle were meant to be stored on a long variable (because long type have more space than int type) (and 64-bites handle are meant to be stored in long values). 

But since I stored them in integer variable (wich is smaller) some data got lost making the handle I stored different from the handle that windows sent to me. 

So here is the link to the code that helped me find the issues https://www.mql5.com/en/code/818