Solved

OBJECT Sharing

Posted on 1998-03-10
8
248 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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

12 Experts available now in Live!

Get 1:1 Help Now