memory mapp file write C++ read in mql4
memory mapp file write C++ read in mql4
Loading saved threads...
faust · External communityPost link
External question — Stack Overflow Stack Exchange
Author: faust
Original post: https://stackoverflow.com/questions/64213192
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
hi guys i want read a memory mapped file created by C++ program , i use the
MSDN example
for create in C++ the memory mapped file , i want read the string in my metatrader 4 i write this code
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 1
#property indicator_color1 Blue
#define FILE_MAP_READ 4
#define BUF_SIZE 256
extern string szName = "MyFileMappingObject";
//extern string szName = "Global\\dllmemfilemap";
int handle = 0;
string Data;
#import "kernel32.dll"
int OpenFileMappingA(int dwDesiredAccess, bool bInheritHandle, string lpName);
string MapViewOfFile(int hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, int dwNumberOfBytestoMap);
int CloseHandle( int handle);
int UnmapViewOfFile(string lpBaseAddress);
#import
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
handle = OpenFileMappingA(FILE_MAP_READ, FALSE, szName);
if(handle == 0)
{
Alert("Could not open file mapping object", GetLastError());
}
else
{
Data = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, BUF_SIZE);
Alert(Data);
UnmapViewOfFile(Data);
CloseHandle(handle);
}
return (0);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
but not open the OpenFileMappingA, return always 0 , anyone have some idea ? thankz at all
Quote
Report
MartinVeronneau · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: MartinVeronneau
Original post: https://stackoverflow.com/a/64214500
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You need to call
CreateFileMapping
before trying to
OpenFileMapping
.
See example
here
:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process.");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
Quote
Report
sbsohrabi · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: sbsohrabi
Original post: https://stackoverflow.com/a/75354426
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
strings are unicode in MQL, or similarly 2-bytes
wchat_t
in C++.
Here is a sample :
#include <cstdlib>
#include <cstring>
#include <string>
char* pBufMQL;
std::wstring ss = L"Sample string";
CopyMemory((PVOID)pBufMQL, ss.c_str(), ss.size() * sizeof(wchar_t));
in MQL:
handle = OpenFileMappingW( FILE_MAP_ALL_ACCESS, FALSE, szName);
if(handle == 0)
{
Alert("Could not open file mapping object", GetLastError());
}
string Data = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
Print("Done, size: ", StringLen(Data), "-", Data);
p.s. 1: I'm currently struggling with writing from MQL to the buffer and string in MQL are a copy of pBuf(from C++), converted to string.
And I need to modify buffer from MQL.
p.s. 2: p.s. 1 solved, Here is a working example :
https://www.mql5.com/en/articles/364#3
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: MartinVeronneau Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/64214500 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. You need to call CreateFileMapping before trying to OpenFileMapping . See example here : #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #define BUF_SIZE 256 TCHAR szName[]=TEXT("Global\\MyFileMappingObject"); TCHAR szMsg[]=TEXT("Message from first process."); int _tmain() { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) szName); // name of mapping object if (hMapFile == NULL) { _tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError()); return 1; } pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, BUF_SIZE); if (pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(hMapFile); return 1; } CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR))); _getch(); UnmapViewOfFile(pBuf); CloseHandle(hMapFile); return 0; }
Checking account access…