Libraries: File Mapping without DLL - page 8

 
o_O:
I wrote that CMemMapFile::Open returns an error code or 0 when there is no error.

At the beginning of the conversation I posted a screenshot of what CMemMapFile::Open function returns.

it was 0 as if there was no error.


Then I corrected it to see if the handle is NULL.

Print(__FILE__,", handle=",MapFile.m_hmem);

and as a result I got

2016.03.04 19:00:00.897 BuyNow Client v 1.03 USDCHF.m,M5: BuyNow Client v 1.03.mq4, handle=8589934592

There is no error, there is a handle, what else do you need, you may say... but no one created the file display... And it's impossible to open a mapping that hasn't been created.

Or maybe this mapping was created by someone else and I don't know about it????

Some programme creates a display with this exact name :-)

#define HADER "BuyNow v10"
CMemMapFile MapFile;
long handl;
int OnInit()
  {

//--- create timer
//---
      EventSetMillisecondTimer(Timer);
      handl=MapFile.Open(StringConcatenate("Local\\","_",HADER,"_",GetSymbol(),"_",((ServerAccauntNumber==0) ?  AccountNumber():ServerAccauntNumber)),BUF_SIZE+HEAD_MEM,modeOpen);
      Print(__FILE__,", handle=",MapFile.m_hmem);
//---
   return(INIT_SUCCEEDED);
  }
 

Dmitry Luck'janenko:


Or maybe this mapping was created by someone else and I don't know about it ???.

Some programme creates a mapping with that exact name :-)


Well, you have created something yourself before that )

the software just works with memory like that. It doesn't clear anything at once. That's why old data can hang around and be opened.

But when it overwrites, it won't open. You probably didn't do anything between openings.
 

Excellent library, used it together with mutexes to write a local copier

As an important component for the receiver I got the file size and built a loop from it,

I found a few bugs - ArrayOutOfRange or lack of 4 bytes.

Fixed

//------------------------------------------------------------------ Write
int CMemMapApi::Write(HANDLE64 hmem, const uchar &buf[], DWORD pos, int sz, DWORD &err) // write the specified number of bytes to memory
{
        if (hmem==NULL) return(-1);
        PBYTE64 view=ViewFile(hmem, err); if (view==0 || err!=0) return(-1); // if not open
        DWORD size=GetSize(hmem, err); if (pos+sz>size) { UnViewFile(view); return(-2); }; // if the size is smaller, exit
        uchar src[]; 
        ArrayResize(src, size+HEAD_MEM); 
        memcpyX(src, view, size+HEAD_MEM); // took the bytebuffer
        for(int i=0; i<sz; i++)
        { 
        src[pos+i+HEAD_MEM]=buf[i]; // written to memory
        }
        memcpyX(view, src, size+HEAD_MEM); // copied it back
        UnViewFile(view); // closed the view
        return(0); // returned OK.
}
//------------------------------------------------------------------ Read
int CMemMapApi::Read(HANDLE64 hmem, uchar &buf[], DWORD pos, int sz, DWORD &err) // read from memory the specified number of bytes
{
        if (hmem==NULL) return(-1);
        PBYTE64 view=ViewFile(hmem, err); if (view==0 || err!=0) return(-1); // if not open
        DWORD size=GetSize(hmem, err); // got the size
        uchar src[]; 
        ArrayResize(src, size+HEAD_MEM);
        memcpyX(src, view, size+HEAD_MEM); // took the bytebuffer
        ArrayResize(buf, sz);
        int i=0; 
        for(i=0; i<sz && pos+i<size; i++) 
        {
        buf[i]=src[pos+i+HEAD_MEM]; // read bytes
        //i++;
        }
        UnViewFile(view); // closed the view 
        return(i); // number of bytes copied
}
 

If I create MMF through MQL5 and write to it and then read it.

But if I create MMF with another programme, when I try to write or read from it, a critical error occurs.

What is the problem?

 
Alex19791979:

If I create MMF through MQL5 and write to it and then read it.

But if I create MMF with another programme, when I try to write or read from it, a critical error occurs.

What is the problem?


The club of telepaths is trying hard to guess the number of the critical error.

 
Alexey Volchanskiy:

The club of telepaths is tensely trying to guess the number of the critical error.

topikstarter is blocked from telepathic influence
 

MT5 critical error message without error code. If there was a code, it would be indicated.

 
Alex19791979:

But if I created MMF with another programme, when I try to write or read from it, a critical error occurs.

What is the problem?

- check if there are no errors when opening the file
- check the available allocated volume for reading/writing in all functions of working with arrays/strings (take into account 2 bytes)
etc.

 

Problem solved.

It is necessary to write and read MMF files by a third-party application starting from 4 bytes, not from 0.

And for MT5 4 bytes are not counted, even if the offset is 0.

So when I had a third-party programme writing from 0 byte, an error occurred when I tried to read MT5.

 
Alex19791979:

Problem solved.

It is necessary to write and read MMF files by a third-party application starting from 4 bytes, not from 0.

And for MT5 4 bytes are not counted, even if the offset is 0.

That's why when I had a third-party programme writing from 0 byte, when I tried to read MT5, an error occurred.

Maybe you didn't take into account that the first bytes in the class are allocated for the header, where the file size is stored?