• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 6:53am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 6:53am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Creating Metatrader DLLs with Lazarus / Free Pascal 104 replies

Wininet.dll & user32.dll. Can we use it in Macintosh MT4/MT5? 0 replies

Multiple EAs using single DLL with DLL Windows - Using Delphi 9 replies

Pascal's Triangle as MM 45 replies

converting dll file to mq4 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Help: Problem getting File Mapping working with MT4/pascal dll
Exit Attachments
Tags: Help: Problem getting File Mapping working with MT4/pascal dll
Cancel

Help: Problem getting File Mapping working with MT4/pascal dll

  • Post #1
  • Quote
  • First Post: Edited 10:38pm Jun 26, 2013 6:42pm | Edited 10:38pm
  •  KingHigh
  • | Joined Aug 2009 | Status: Member | 98 Posts
Hi All,

Hopefully, I will get a quick answer on this. I have a prototype using Kernell32.dll and using the File Mapping functions of rsharng data between charts and MT4 directories.

I use a Pascal DLL so that I have an easy access to pointer functionality that MT4 lacks. I have every thing working but I get an exception when I try to move memory into and from Mapped Memory using RtlMoveMemory(). The exception is either a C00000FD or a C0000005. SoOOO I need permission to write into the map or my pointer usage isn't working.

Does anyone know if MT4 is blocking me some how with permissions into mapped page memory or something else out there is Mt4 craziness.

I have added my pascal prototype code in case anyone wants to look at it for obvious miss use of pointerw. I am new to pascal but have written a pretty solid file management dll --- but this has me stumped. The attached file is a formatted pascal dll file.

KingHigh

//==============================================================================
unit win32filemapping;

{$mode objfpc}
{$CALLING STDCALL}
{$I-}

interface
implementation
uses
Windows, SysUtils;

// HANDLE CreateFileMappingA( HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow, LPCTSTR lpName);
function CreateFileMappingA( hFile: DWORD; lpAttrib: Pointer; flProtect, MaxSizeHigh, MaxSizeLow: DWORD; FmemName: PChar ): DWORD;
stdcall; external 'kernel32.dll' name 'CreateFileMappingA';
//HANDLE WINAPI OpenFileMappingA( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName );
function OpenFileMappingA( DesiredAccess: DWORD; InheritHandle: Boolean; FmemName: PChar ): DWORD;
stdcall; external 'kernel32.dll' name 'OpenFileMappingA';
//LPVOID MapViewOfFile( HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap );
function MapViewOfFile( hFile, DesiredAccess, FileOffsetHigh, FileOffsetLow, NbrOfBytesToMap: DWORD ): Pointer;
stdcall; external 'kernel32.dll' name 'MapViewOfFile';
// BOOL WINAPI UnmapViewOfFile( LPCVOID lpBaseAddress );
function UnmapViewOfFile( BaseAddress: Pointer ): Boolean;
stdcall; external 'kernel32.dll' name 'UnmapViewOfFile';
//void RtlMoveMemory( PVOID Destination, const VOID *Source, SIZE_T Length );
procedure RtlMoveMemory( Destination, Source: Pointer; Length: DWORD )
stdcall; external 'kernel32.dll' name 'RtlMoveMemory';
//BOOL FlushViewOfFile( LPCVOID lpBaseAddress, SIZE_T dwNumberOfBytesToFlush );
function FlushViewOfFile( MapBaseAdr: Pointer; BytesToFlush: DWORD ): Boolean;
stdcall; external 'kernel32.dll' name 'FlushViewOfFile';
//BOOL CloseHandle( HANDLE hObject );
function CloseHandle( hFile: DWORD ): Boolean;
stdcall; external 'kernel32.dll' name 'CloseHandle';
//BOOL DeleteFileA( LPCTSTR lpFileName );
function DeleteFileA( FmemName: PChar ): Boolean;
stdcall; external 'kernel32.dll' name 'DeleteFileA';

//==============================================================================
function FPC_CreateFileMapA( Hndle: DWORD; SecAtrib: Pointer; MapAccMode, MemHighAdr, MemLowAdr: DWORD; FmemName: PChar )
: DWORD; stdcall;
begin
OutputDebugString(PChar(Format('CFSM: Before CFM',[])));
FPC_CreateFileMapA := CreateFileMappingA( Hndle, SecAtrib, MapAccMode, MemHighAdr, MemLowAdr, FmemName );
OutputDebugString(PChar(Format('CFSM: After CFM, H= %d',[FPC_CreateFileMapA])));
end;
//==============================================================================
function FPC_OpenFileMapA( AccessMethod: DWORD; InherHandle: Boolean; FmemName: PChar ): DWORD; stdcall;
begin
FPC_OpenFileMapA := OpenFileMappingA( AccessMethod, InherHandle, FmemName );
end;
//==============================================================================
function FPC_MapViewOfFile( Hndle, MemAccess, MemHighAdr, MemLowAdr, BytesToMap: DWORD ): Pointer; stdcall;
begin
OutputDebugString(PChar(Format('CFSM: Before MVF',[])));
FPC_MapViewOfFile := MapViewOfFile( Hndle, MemAccess, MemHighAdr, MemLowAdr, BytesToMap );
OutputDebugString(PChar(Format('CFSM: After MVF, P= %d',[FPC_MapViewOfFile])));
end;
//==============================================================================
function FPC_UnmapViewOfFile( MapBaseAdr: Pointer ): Boolean; stdcall;
begin
FPC_UnmapViewOfFile := UnmapViewOfFile( MapBaseAdr );
end;
//==============================================================================
function FPC_FlushViewOfFile( FMapBaseAdr: Pointer; BytesToFlush: DWORD ): Boolean; stdcall;
begin
FPC_FlushViewOfFile := FlushViewOfFile( FMapBaseAdr, BytesToFlush );
end;
//==============================================================================
function FPC_CloseHandle( Hndle: DWord ): Boolean; stdcall;
begin
FPC_CloseHandle := CloseHandle( Hndle );
end;
//==============================================================================
function FPC_DeleteFileA( FmemName: PChar ): Boolean; stdcall;
begin
FPC_DeleteFileA := DeleteFileA ( FmemName );
end;
//==============================================================================
procedure FPC_RtlMoveMemory( Destination, Source: Pointer; Length: DWORD ); stdcall;
begin
RtlMoveMemory( Destination, Source, Length );
end;
//==============================================================================//==============================================================================//==============================================================================
CONST
READ = 1;
WRITE = 2;

type
TCntlBlock = array [0..3] of DWORD;
TXferBufr = array [0..256] of DWORD;
var
MapTmp: array [0..1024] of Byte;
function FPC_AccessFileMap( var CntlBlock: TCntlBlock; var XferBufr: TXferBufr ): Boolean stdcall;
// CntlBlock contains r or w, chr cnt, max buffervsize, and the pointer to the buffer.
// XferBufr contains integers to be written into the map or return data being read from the map.
var
ChrShift: Array [0..3] of DWORD = (0,8,16,24);
RdOrWrt, MaxDWords, CharCnt, DWdIdx, ChrIdx, DWdChr, DWd_MapAdr: DWORD;
DWdXfer, ShiftVal: Integer;
MapTmpPtr: PByte;
MapFilePtr: PByte;
Test: Byte;
begin
MapTmpPtr := @MapTmp[0];
RdOrWrt := CntlBlock[0];
MaxDWords := CntlBlock[1];
CharCnt := CntlBlock[2];
DWd_MapAdr := CntlBlock[3]; // Integer containing Ptr to MapBufr
//MapFilePtr := PByte(DWd_MapAdr);
OutputDebugString(PChar(Format('CFSM: After var init,RdOrWrt = %d, MaxDWords = %d, CharCnt = %d, MapTmpPtr= %d. MapFilePtr= %d. ', [RdOrWrt,MaxDWords,CharCnt,DWORD(MapTmpPtr),DWORD(MapTmpPtr)])));
FPC_AccessFileMap := FALSE;
if( ( ( MaxDWords * 4) >= CharCnt ) and ( CharCnt > 0 ) ) then
begin
if RdOrWrt = READ then // Build Int XferBufr from MapFile access.
begin
OutputDebugString(PChar(Format('CFSM: Lets Xfer data, in read loop, Xter[0,1,2,3] = %x, %x, %x,
%x.',[XferBufr[0],XferBufr[1],XferBufr[2],XferBufr[3]])));
//RtlMoveMemory( MapTmpPtr, MapFilePtr, CharCnt );
for ChrIdx := 0 to CharCnt-1 do MapTmp[ChrIdx] := MapTmp[ChrIdx];
OutputDebugString(PChar(Format('CFSM: AFM Read Move Complete!!',[])));
for ChrIdx := 0 to CharCnt-1 do
begin
DWdChr := DWORD(MapTmp[ChrIdx]);
OutputDebugString(PChar(Format('CFSM: After DWORD DWdIdx = %d, DWrdChr = %x.',[DWdIdx,DWdChr])));
XferBufr[DWdIdx] := XferBufr[DWdIdx] or ( (DWORD(MapTmp[ChrIdx])) shl ChrShift[ChrIdx and 3] );
OutputDebugString(PChar(Format('CFSM: Read From Map Buffer, XferBufr[DWdIdx] = %x, ChrIdx = %d.',[XferBufr[DWdIdx],ChrIdx])));
end;
FPC_AccessFileMap := TRUE;
OutputDebugString(PChar(Format('CFSM: AFM Read Complete!!',[])));
end
else //THEN RdOrWrt = Write // Write chars from XferBufr into MapFile access
begin
OutputDebugString(PChar(Format('CFSM: Lets Xfer data, in write loop, Xter[0,1,2,3] = %x, %x, %x,
%x.',[XferBufr[0],XferBufr[1],XferBufr[2],XferBufr[3]])));
for ChrIdx := 0 to CharCnt-1 do
begin
//OutputDebugString(PChar(Format('CFSM: Xfer[] = %x. ',[XferBufr[ChrIdx DIV 4]])));
OutputDebugString(PChar(Format('CFSM: After DWORD DWdXfer = %x, ShiftVal = %d, ChrIdx=%d, CharCnt=%d.',
[DWdXfer,ShiftVal,ChrIdx,CharCnt])));
MapTmp[ChrIdx] := ((XferBufr[ChrIdx DIV 4]) SHR (ChrShift[ChrIdx AND 3]));
OutputDebugString(PChar(Format('CFSM: After DWORD MapTmp[ChrIdx] = %x',[MapTmp[ChrIdx]])));
//OutputDebugString(PChar(Format('CFSM: Write to Map Buffer, DWdXfer = %x , ChrIdx =%d , ChrShift[ChrIdx and 3] =%x,
',[DWdXfer,ChrShift[ChrIdx and 3]])));
end;
//RtlMoveMemory( MapFilePtr, MapTmpPtr, CharCnt );
for ChrIdx := 0 to CharCnt-1 do MapTmp[ChrIdx] := MapTmp[ChrIdx];
FPC_AccessFileMap := TRUE;
OutputDebugString(PChar(Format('CFSM: AFM Write RtlMove Complete!!',[])));
end;
end;
end;

//==============================================================================
exports
FPC_CreateFileMapA,
FPC_OpenFileMapA,
FPC_MapViewOfFile,
FPC_UnmapViewOfFile,
FPC_FlushViewOfFile,
FPC_AccessFileMap,
FPC_CloseHandle,
FPC_DeleteFileA;
end.
Attached File(s)
File Type: txt File Map Access.txt   9 KB | 486 downloads
  • Post #2
  • Quote
  • Jun 27, 2013 7:04am Jun 27, 2013 7:04am
  •  Ruptor
  • | Joined Jul 2011 | Status: Member | 10 Posts
In reply to your PM sorry it is way too complicated for me and I haven't used R yet. As you know pointers are the usual suspects for problems in your own code. I steer clear of trying to integrate too much in to Mt4 because it is an unknown black box. Without 7 bit's help in finding the maths hardware interrupt handling problem where FPC enabled interrupts that Mt4 didn't handle I would never have made my DLL work in CTL. By the way I don't see this exception handling in your code.
 
 
  • Post #3
  • Quote
  • Jul 18, 2013 4:34am Jul 18, 2013 4:34am
  •  rangebound
  • | Joined Aug 2006 | Status: Member | 237 Posts
I fear you may be trying to acheive something that the file mapping interface will not provide. I suggest you read up on the MSDN descriptions of these fuinctions which can be found by googling the function name and or browsing the MSDN tree here :

http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

I'm not at all sure what file(s) you are trying to map or how you are trying to use them at the process level but this description from the above link may dissuade you from continuing to try to use this interface, at least if your goal is to provide mapping to 2 separate computers.

////
Multiple processes can share a view of the same file by either using a single shared file mapping object or creating separate file mapping objects backed by the same file.
A single file mapping object can be shared by multiple processes through inheriting the handle at process creation, duplicating the handle, or opening the file mapping object by name. For more information, see the CreateProcess, DuplicateHandle and OpenFileMapping functions.
Creating a file mapping object does not actually map the view into a process address space.
The MapViewOfFile and MapViewOfFileEx functions map a view of a file into a process address space.
With one important exception, file views derived from any file mapping object that is backed by the same file are coherent or identical at a specific time. Coherency is guaranteed for views within a process and for views that are mapped by different processes.
The exception is related to remote files.
Although CreateFileMapping works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer only sees its own writes to the page. When the data gets updated on the disk, it is not merged.
A mapped file and a file that is accessed by using the input and output (I/O) functions (ReadFile and WriteFile) are not necessarily coherent./////////////

However if you wish to continue the most likely problematic areas are :

1) memory allocation ie is your memory able to be shared between processes.
2) security attributes are not setup correctly
3) you should refrain from using variables like this :

function FPC_AccessFileMap( var CntlBlock: TCntlBlock; var XferBufr: TXferBufr ): Boolean stdcall;
since these references will be passed on the stack and maybe passed as nearpointers rather than Far pointers



As a final note :

These interfaces are never very well documented and can be extremely difficult to implement even when you have a fully working
example that just needs to be tweaked slightly. I dont know what your end goal is IE what you are going to do with the mapped file but for a lot of implemetation scenarios I would suggest you try a client/ server | request/ reply approach to pass data between the two threads / applications.

HTH
 
 
  • Post #4
  • Quote
  • Last Post: Jul 19, 2013 1:20pm Jul 19, 2013 1:20pm
  •  FiatFap
  • Joined Sep 2010 | Status: Member | 578 Posts
This is another way using your registry.

hth
Politics is the womb in which war develops.
 
 
  • Platform Tech
  • /
  • Help: Problem getting File Mapping working with MT4/pascal dll
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023