Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Shared memory

Hi all,


I've an .exe and a DLL.


In the DLL i've a function that will make use of a

global variable (a handle) from the main application

(.exe).


As the DLL attaches to diferent processes, and in order

to use that variable, i've to save it in a shared memory.


Can any one show me how to implement this in my DLL

(in C or C++) and how to access the variable ?


Thanks in advance.





Comments

  • The implimentation depends on what you mean. If each application that attaches to the DLL has a different handle and the dll only needs to know that one handle for each application, then you can store that handle in a global variable in the DLL. When a DLL is mapped into a process, a copy of DLL global data is mapped into that process as well.


    So if this is the case there is no need to worry about what applications are using your DLL, each application will have its own DLL global data. Only the DLL code is shared between processes, not data.


    However if you need all the applications to have the same handle to data in the DLL that will be shared across all processes, the you can use shared memory, via GlobalAlloc, or more preferably a memory mapped file.


    I see the memory mapped file as the easier option, because that way the DLL can handle the memory mapped file internally with out the process 'worrying' about it. On the other hand with shared memory, you must find a mechanism of communicating the Shared Handle to all the processes. The only way I can't think of a way to do that...


    With a memory mapped file, all you need is the name of the file mapping, which can be stored as a constant string in the DLL's global data. Any process can open a memory mapped file, if they know the handle (OpenFileMapping).


    Hope this helps,


    Eric


    : Hi all,


    : I've an .exe and a DLL.


    : In the DLL i've a function that will make use of a

    : global variable (a handle) from the main application

    : (.exe).


    : As the DLL attaches to diferent processes, and in order

    : to use that variable, i've to save it in a shared memory.


    : Can any one show me how to implement this in my DLL

    : (in C or C++) and how to access the variable ?


    : Thanks in advance.





Sign In or Register to comment.