Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2005, 05:15 PM
delphinewb delphinewb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 3 delphinewb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 6 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old November 29th, 2005, 05:27 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 42 m 50 sec
Reputation Power: 185
Quote:
Originally Posted by delphinewb
However, problem arise when I try to share more complex data types such as classes, pointers and/or variant arrays, it throws Violation errors.

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.

Reply With Quote
  #3  
Old November 29th, 2005, 05:38 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 42 m 50 sec
Reputation Power: 185
- addendum -

Your idea about serializing probably solves the problem too.

Reply With Quote
  #4  
Old November 29th, 2005, 06:25 PM
delphinewb delphinewb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 3 delphinewb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 6 sec
Reputation Power: 0
Quote:
Originally Posted by M.Hirsch
- addendum -

Your idea about serializing probably solves the problem too.


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 ?

Reply With Quote
  #5  
Old November 29th, 2005, 06:35 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 42 m 50 sec
Reputation Power: 185
Quote:
Originally Posted by delphinewb
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 ?

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Shared Memory: Sharing pointers, classes, variant arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT