我有一个服务器库,我的客户端可执行文件将其注入远程进程。服务器负责设置某种 IPC/RPC 实现以允许客户端与远程进程无缝通信。
更新
查看以下服务器端标头:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
using namespace boost::interprocess;
typedef allocator<int, managed_shared_memory::segment_manager> ShmIntAllocator;
typedef vector<int, ShmIntAllocator> IntVector;
class A
{
public:
A();
A(string str, offset_ptr<IntVector> ints)
: m_str(string(str)), m_ints(ints) {};
~A();
string m_str;
offset_ptr<IntVector> m_ints;
};
class B
{
public:
B();
B(offset_ptr<A> obj) : m_obj(obj);
~B();
VOID DoSomethingUseful()
{
MessageBoxA(NULL, m_obj->m_str.c_str(), "SomethingUseful", MB_ICONINFORMATION);
}
offset_ptr<A> m_obj;
};
这是服务器端的实现:
managed_shared_memory g_shm;
offset_ptr<A> 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& 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<IntVector> ints = g_shm.construct<IntVector>(unique_instance)(alloc_intVector);
ints->push_back(10);
ints->push_back(20);
ints->push_back(30);
g_objA = new A("Testing", ints);
B objB(g_objA);
// Copy data into shared memory
size_t len = sizeof(objB); // <-- 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, &objB, len);
return TRUE;
}
VOID StopServer()
{
// Free used resources
if(g_objA) {
delete g_objA;
g_objA = nullptr;
}
try{
g_shm.destroy<B>(unique_instance);
g_shm.deallocate(g_hMem);
g_hMem = nullptr;
shared_memory_object::remove("MySharedMem");
} catch(interprocess_exception& e) {
std::string msg(e.what());
MessageBoxA(NULL, msg.c_str(), "Error", MB_ICONERROR);
}
}
和客户端实现:
BOOL Connect()
{
// Grab the shared memory pool and extract the class
managed_shared_memory shm(open_only, "MySharedMem");
std::pair<B*, std::size_t> ret = shm.find<B>(unique_instance); // <-- Always ends up being 0x00000000!
B *objB = static_cast<B*>(ret.first);
if(!objB) return FALSE;
objB->DoSomethingUseful();
return TRUE;
}
您会注意到managed_shared_memory::find()
始终无法向客户端返回有效指针。但据我所知,代码是完全有效的。没有编译器警告或错误,到目前为止一切似乎都运行顺利。
那么为什么会失败呢?我怎样才能让它按预期工作?