?
Solved

Sharing a pointer in a DLL

Posted on 2003-02-26
10
Medium Priority
?
358 Views
Last Modified: 2010-04-01
Hi,

This problem is a bit similar to the Shared memory problem under C Programming topic. But I could not use the solution suggested there in this problem. The problem is as follows:

I have a DLL(DLL1) that is used by an application and another DLL (DLL2). There are certain variables in
the DLL1 that are shared by the other 2. This shared segment is placed under pragma directive as

#pragma data_seg("APP_Shared")
BOOL gbLinkUp = FALSE;
APP_Handle ghAppHandle = NULL;
DWORD gdwEventId = 0;  and so on
#pragma data_seg()

One of these shared variables happens to be a pointer(AppHandle) that is allocated by a proprietary library accessed by the DLL. This obviously is
causing the problem when one application updates this variable and the other tries to use it as the contents of the pointer are messed up. My query is "How do we share a pointer between 2 applications using a common
DLL?"

Is there some other alternative that I should be opting given that I cannot change the Proprietary library allocating the pointer. For eg: store the pointer separately in App and DLL2 and pass it to DLL1 wherever it's used instead of storing in DLL1 as shared global. Please Suggest

Thanks
- Divya

0
Comment
Question by:divyaveda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8031455
The idea of using data segments this way is completely new to me.  I have no idea if it's valid.  Saying that, I do know how to protect resources across processes.  Assuming this type of operation is allowed, what you want is to use named mutexes to lock and block access to the pointers.  Search the MSDN for CreateMutex and OpenMutex.  You can get handles to the same mutex from multiple threads or processes that way.  It seems as if the interface is fixed, otherwise I would scrap it.  It doesn't seem like a very robust design.  Good luck.
brian
0
 

Expert Comment

by:phildsp
ID: 8031737
Would it be necessary to declare all your shared variables with a static scope?
0
 
LVL 5

Expert Comment

by:Kimpan
ID: 8032034
since both DLL are mapped into the same process, use criticalsection to synchronize your read/write to your shared pointer.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:divyaveda
ID: 8032146
The pointer allocated by the DLL1 in the DLL2 context has a different address space than in the application context. The value of the pointer does not change but the content it points to changes in different address space. At any point of time DLL2 and the APP do not corrupt the pointer. So, mutex/critical section will not solve the problem.

My problem is to retain the contents of the pointer in different contexts. Hope my query is clear.
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8034804
So the question remains: Are you _forced_ to use the current design?  If so, get a sample implementation by the person who created the DLL.  It looks pretty shady overall.  Without using a shared memory segment or being able to translate the pointer into some readable memory, I don't see how it can be done.
brian
0
 
LVL 5

Expert Comment

by:Kimpan
ID: 8036478
If you want to share memory between applications, use memory mapped files.
0
 

Accepted Solution

by:
divyaveda earned 0 total points
ID: 8040506
Thanks for all your suggestions. I fixed the problem by opting slightly a different approach. Memory mapped file was one option that could have looked into. But, to avoid additional complexity(mmap calls), I took a simpler approach where I donot share the pointer:-). I route all the controls (from DLL2) through the application so that all the variables and pointers are in the application address space. This is done with the help of messages and named events. Thus, DLL2 does not directly call DLL1 where the shared pointer was used. The design as some of you said is not clean. However, for our end product requirement this is the only way out.
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8049697
divyaveda, I would just like to state for the record that I asked you whether the design shown was fixed, and you never answered my question. (!)  Had you answered the question, you could have saved us all time and confusion, because we could have clearly stated different options for designing your interface from the beginning.
brian
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9847267
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered by original poster: Archive and refund

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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