Link to home
Start Free TrialLog in
Avatar of divyaveda
divyaveda

asked on

Sharing a pointer in a DLL

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

Avatar of bkrahmer
bkrahmer

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
Would it be necessary to declare all your shared variables with a static scope?
since both DLL are mapped into the same process, use criticalsection to synchronize your read/write to your shared pointer.
Avatar of divyaveda

ASKER

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.
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
If you want to share memory between applications, use memory mapped files.
ASKER CERTIFIED SOLUTION
Avatar of divyaveda
divyaveda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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 (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer