Solved

OBJECT Sharing

Posted on 1998-03-10
8
249 Views
Last Modified: 2011-09-20
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

0
Comment
Question by:peri
  • 5
  • 2
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1183314
answer coming
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183315
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
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183316
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?
0
 

Author Comment

by:peri
ID: 1183317
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
jtwine100697 earned 90 total points
ID: 1183318
[Answer coming?  What does that mean?]

   It sounds like you want two instances of an application to share the same INSTANCE of a class.

   You can have both applications create seperate instances of the same class, and pass them both, say, the same numeric identifier, and the class instances might be able to communicate via pipes, or some other shared memory arch.  Under MFC, you could use CMemFile.

-=- James.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183319
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.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183320
  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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183321
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now