Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing class pointers between processes
    primarykey
    data
    text
    <p>I have a server library which my client executable injects into a remote process. It is the server's responsibility to set up some sort of IPC/RPC implementation to allow the client to seamlessly communicate with the remote process.</p> <h2>Update</h2> <p>Take a look at the following server-side header:</p> <pre><code>#include &lt;boost/interprocess/managed_shared_memory.hpp&gt; #include &lt;boost/interprocess/mapped_region.hpp&gt; #include &lt;boost/interprocess/containers/vector.hpp&gt; #include &lt;boost/interprocess/containers/string.hpp&gt; #include &lt;boost/interprocess/allocators/allocator.hpp&gt; using namespace boost::interprocess; typedef allocator&lt;int, managed_shared_memory::segment_manager&gt; ShmIntAllocator; typedef vector&lt;int, ShmIntAllocator&gt; IntVector; class A { public: A(); A(string str, offset_ptr&lt;IntVector&gt; ints) : m_str(string(str)), m_ints(ints) {}; ~A(); string m_str; offset_ptr&lt;IntVector&gt; m_ints; }; class B { public: B(); B(offset_ptr&lt;A&gt; obj) : m_obj(obj); ~B(); VOID DoSomethingUseful() { MessageBoxA(NULL, m_obj-&gt;m_str.c_str(), "SomethingUseful", MB_ICONINFORMATION); } offset_ptr&lt;A&gt; m_obj; }; </code></pre> <p>And here's the server-side implementation:</p> <pre><code>managed_shared_memory g_shm; offset_ptr&lt;A&gt; g_objA = nullptr; PVOID g_hMem = nullptr; BOOL StartServer() // Create a shared memory pool try { g_shm = managed_shared_memory(create_only, "MySharedMem", OVR_MAPSIZE); } catch(interprocess_exception&amp; e) { std::string msg(e.what()); MessageBoxA(NULL, msg.c_str(), "Error", MB_ICONERROR); return FALSE; } // Construct a local class instance const ShmIntAllocator alloc_intVector (g_shm.get_segment_manager()); offset_ptr&lt;IntVector&gt; ints = g_shm.construct&lt;IntVector&gt;(unique_instance)(alloc_intVector); ints-&gt;push_back(10); ints-&gt;push_back(20); ints-&gt;push_back(30); g_objA = new A("Testing", ints); B objB(g_objA); // Copy data into shared memory size_t len = sizeof(objB); // &lt;-- Doesn't seem to make a difference if I set this to be something higher g_hMem = g_shm.allocate(len); std::memcpy(g_hMem, &amp;objB, len); return TRUE; } VOID StopServer() { // Free used resources if(g_objA) { delete g_objA; g_objA = nullptr; } try{ g_shm.destroy&lt;B&gt;(unique_instance); g_shm.deallocate(g_hMem); g_hMem = nullptr; shared_memory_object::remove("MySharedMem"); } catch(interprocess_exception&amp; e) { std::string msg(e.what()); MessageBoxA(NULL, msg.c_str(), "Error", MB_ICONERROR); } } </code></pre> <p>And the client implementation:</p> <pre><code>BOOL Connect() { // Grab the shared memory pool and extract the class managed_shared_memory shm(open_only, "MySharedMem"); std::pair&lt;B*, std::size_t&gt; ret = shm.find&lt;B&gt;(unique_instance); // &lt;-- Always ends up being 0x00000000! B *objB = static_cast&lt;B*&gt;(ret.first); if(!objB) return FALSE; objB-&gt;DoSomethingUseful(); return TRUE; } </code></pre> <p><br/> You'll notice that <code>managed_shared_memory::find()</code> always fails to return a valid pointer to the client. But as far as I can tell, the code is perfectly valid. There are no compiler warnings or errors, and everything appears to run smoothly up until this point.<br/></p> <p>So why is this failing? How can I get this to work as expected?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload