Solved

OBJECT Sharing

Posted on 1998-03-10
8
251 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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