I want to move my caching library to a DLL and allow multiple applications to share a single pointer allocated within the DLL using GlobalAlloc(). How could I accomplish this, and would it result in a significant performance decrease?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could certainly do this and there won’t be any performance implication for a single pointer.
Rather than use
GlobalAlloc, a legacy API, you should opt for a different shared heap. For example the simplest to use is the COM allocator,CoTaskMemAlloc. Or you can useHeapAllocpassing the process heap obtained byGetProcessHeap.For example, and neglecting to show error checking:
Note that you only need to worry about heap sharing if you expect the memory to be deallocated in a different module from where it was created. If your DLL both creates and destroys the memory then you can use plain old
malloc. Because all modules live in the same process address space, memory allocated by any module in that process, can be used by any other module.Update
I failed on first reading of the question to pick up on the possibility that you may be wanting multiple process to have access to the same memory. If that’s what you need then it is only possible with memory mapped files, or perhaps with some form of IPC.