I got code from one of the vendor to read something from Memory mapped file in C#, but due to limitations I need to develop the code in Delphi-7 language. The code I got is written below.
The tool is reading analog input from Hardware module.
Can any one help me to find equivalent code of C# in Delphi? My C# code is as below-
MemoryMappedFile file = MemoryMappedFile.OpenExisting("MY_SHARED_LOCATION");
MemoryMappedViewAccessor reader = file.CreateViewAccessor();
for (int i = 0; i < 16; i++)
{
inputByte[i + 1] = reader.ReadByte(i);
}
I found equivalent Class of MemoryMappedFile but still unable to code rest of the part in Delphi-7.
Memory Mapped Files are feature provided by Windows for different processes to share memory.
I don't currently have access to a Delphi compiler to test this, but it should set you on the right path. I'm also making some assumptions based on the code sample you provided: You only intend reading data, you'll read exactly 16 bytes. If these are invalid, you'll have to changed the code accordingly.
hFile := OpenFileMapping(FILE_MAP_READ, FALSE, "MY_SHARED_LOCATION");
Win32Check(hFile);
try
//Buffer must be a byte pointer
buf := MapViewOfFile(hFile, FILE_MAP_READ, 0, 0, 16);
Win32Check(buf);
try
//Use buf^ as you please
finally
UnmapViewOfFile(buf);
end;
finally
CloseHandle(hFile);
end;
For more info on the various memory map API routines, see the following:
https://msdn.microsoft.com/en-au/library/ms810613.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366556(v=vs.85).aspx