Link to home
Start Free TrialLog in
Avatar of peri
peri

asked on

OBJECT Sharing

How to make  c++ OBJECT shared .
I have a c++ dll from where data to be shared between
two different process.

i am using microsoft Vc++5.0

Avatar of nietod
nietod

answer coming
You didn't give me much information, so this may only be a start at an answer.  If you need more post a comment with some more info.

to make a class exported from a DLL, use __declspec(dllexport)  like

class __declspec(dllexport) ExportedClass
{
 // stuff
};
This allows the class to be used by programs/dlls that use your dll.  You may want to use a macro to define __declspec(dllexport)  like

#define declspec(dllexport) DllExp

Is there more you need?
Avatar of peri

ASKER

Environment : 32-bit

I have done data sharing between different instances of a DLL using the following method.

I named the data segment as below
#pragma data_seg(".sdata")
int gnData = 0;      // Global integer to be shared - Line number 1
#pragma data_seg()

I gave the attributes of this "named data segment" in the .def file.
Partial contents of .def file
// other sections of the .def file are omitted for simplicity..
==== Start  of the def file ====
SEGMENTS
.sdata  READ WRITE SHARED
==== End of the def file ====


What I want to do is gnData should be a class object.
Example
Assume a class like this..
Class Myclass
{'
 private :
      int m_data;
public:
      Myclass();
      ~Myclass();
}
Then Line number 1 mentioned above should be
Myclass gnData;

It is clearly mentioned in the MSDN that for an object to be in shared data segment. It should be initialized. This means that I should use initializer list "{ }"  if it is a user defined type. This cannot be done for a class objects....

One more thing I observed was I was able to share the class objects if I did not have any constructors. This allows me to use the initializer  lists....
Anyway the main thing that is preventing me from doing this the initializtion  "Shared Objects Need to be initialized...And class objects cannot have initializer lists"

Questions :
1.      Is there a way to work around this ???
2.      Is it not possible for C++ object to go in the named shared data segment??

Thanks in advance,
peri
ASKER CERTIFIED SOLUTION
Avatar of jtwine100697
jtwine100697
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Peri, I didn't understand what you were looking for before.  I'm afraid jtwine doesn't either.

jtwine, the data in a window's dll is usually maintained seperately for each process.  That is, each proccess has a seperate copy of the data.  However, you can create segments that contain data that is shared among all the proccesses using the Dll.  Peri wants to put an object in such a segment.  Unfortunatley, I have no experience in this area.
  I understand that...  I have used a DLL to share data across to seperate applications before.

   I do not believe that sharing the same instance of an object is the best means to obtain communication here, and that is what it sounds like Peri is trying to do.

   Looks like Peri wants a struct-like object (class) to have the same data for both applications.  So if one application changes a member, the change is reflected in the other application as well.

   This can be done with "smart structures".  The strucures could create a Named Pipe, and preform sync. communication behind-the-scenes.

-=- James.
I see.  Without understanding where you were coming from, I thought you were totally of track.

I can agree that it may not be the best way, there are lots of pitfalls.  But without knowing more about what Peri is trying to do its is hard to say if there is a better way.