Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sharing a pointer in a DLL

Posted on 2003-02-26
10
Medium Priority
?
361 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
  • 3
  • 2
  • 2
  • +2
9 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

564 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