简体   繁体   中英

Can I pass a pointer using memory mapped files?

I have read the article about Memory-Mapped Files and the example about CreateFileMapping .
My question is: Can I pass a pointer to a struct or a object between two processes using memory-mapped file?


Since there are some answers that it is possible, here is struct that I want to pass:

 // First Process struct OtherStruct{}; struct MyStruct { unsigned long handleObject; unsigned long *phandleObject; OtherStruct someData; OtherStruct *pData; } MyStruct dataSend = { ... }; WriteToMappedFile(data); // Second Process MyStruct dataReceived = ReadFromMappedFile() 

As the other answers already stated, you must either rely on the address of the memory-mapped areas to be equal, or you must move from absolute addresses in your pointers to relative addressing.

One possible implementation I stumbled across recently is the offset_ptr in the Boost library, which seems to fit your use case perfectly.

The answer depends on what you want to achieve. Passing a pointer in shared memory is easy, but the other process may not be able to use it in the way you expect.

Note that a pointer contains a virtual address of the data structure it points to. Such a virtual address is only valid within the process that holds the pointed-to data structure. If you pass the pointer to another process, the other process will have its own virtual address space, and the passed pointer loses its validity.

So the answer to your question is: Yes, you can pass the pointer, but without further actions, you won't be able to successfully use this pointer in the receiving process. Specifically, you will most probably not be able to use it for accessing the struct or object it points to.

If you want to access the struct or object within the other process, you need to do the following:

  • Put the object itself into shared memory.
  • Convert the pointer to the object into an offset relative to the beginning of the memory mapped file.
  • Pass this offset to the other process
  • In the other process, use the offset to convert back to a pointer.

boost::offset_ptr can help you with part of that.

Assuming the Pointer is to a struct that is part of the same memory mapped region, yes that can make sense. but then you will have to ensure that the memory mapped region is mapped to the same virtual address, this is not always guaranteed and is a bad way to design things.

You can pass the offset instead and deal with relative offsets everywhere fpor structures present in this memory region.

If the pointer you want to pass into the memory-mapped-file is not allocated by GlobalAlloc, and not locked by GlobalLock, it can't. However, you have already the memory allocated to pass the data. So you can re-write the memory on the memory-mapped-file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM