|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Shared Memory: Sharing pointers, classes, variant arrays
Hello people.
I'm trying to share some pointers, classes and/or variant arrays in a shared memory space through the use of: * OpenFileMapping * CreateFileMapping * MapViewOfFile It's a set of pretty simple applications to test: a server and a client residing in the same machine. server (done on initialization): Code:
MapHandle := CreateFileMapping($FFFFFFFF,
nil,
PAGE_READWRITE,
0,
SizeOf(TSharedMemory),
PChar(__MAPPING_NAME));
PSharedMemory := MapViewOfFile(MapHandle,
FILE_MAP_ALL_ACCESS,
0,
0,
SizeOf(TSharedMemory));
client (when attempting to access shared memory space) Code:
MapHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS,
False,
PChar(__MAPPING_NAME));
if MapHandle = 0 then
ShowMessage('Server not running.')
else
begin
PSharedMemory := MapViewOfFile(MapHandle,
FILE_MAP_ALL_ACCESS,
0,
0,
SizeOf(TSharedMemory));
How the shared memory looks like: Code:
TSharedMemory = packed record
ServerProcessID: DWORD;
test: TPWSBaseModule;
MyString: String;
end;
Primitive data types such as integers, strings, DWORD, etc. (even normal array of integers) are being shared without any problems ~ both client and server can read and write shared memory spaces. However, problem arise when I try to share more complex data types such as classes, pointers and/or variant arrays, it throws Violation errors. Here's what I have tried: * Checked type casting * Unchecked type casting None of them worked (giving me same error: violation errors) Any hints / suggestions / enlightments ? My theory is ~ I may need to "serialize" the more complex data types mentioned above (classes / pointers) before communicating, and "de-serialize" upon receiving the data. Thank you in advance. |
|
#2
|
|||
|
|||
|
Quote:
All of them are internally using pointers. And pointers are only valid in your own address space. Since server and client are independent processes, you can not use memory mapping this way. Memory mapping is a very low-level operation. You have to memmap the pointers' destinations (Objects and such) individually. Then go through all pointers manually and re-set them to the local address space. I don't think this is a passable way, but I didn't test this either. M.
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
- addendum -
Your idea about serializing probably solves the problem too. |
|
#4
|
|||
|
|||
|
Quote:
that's what i'd also like to know next ~ have you (or anybody) any experiences of converting a class / object / pointer into a stream of binaries that is transportable ? |
|
#5
|
|||
|
|||
|
Quote:
Some objects know a SaveToStream() method. You can use this to serialize at least the main data they are carrying to a memory stream which can be mapped with Memmap(). (Memmap "TMemoryStream.Memory^") But generally, I don't think Delphi supports real serialization. You could only use the SaveToStream() and LoadFromStream() function after you manually re-created all objects. My info about serialization may be outdated though since I am still working with Delphi 4 and 5 (didn't see the need to upgrade yet.) M. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Shared Memory: Sharing pointers, classes, variant arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|