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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.