是否可以在进程之间使用函数指针? [英] Is it possible to use function pointers across processes?

查看:189
本文介绍了是否可以在进程之间使用函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道每个进程都会创建自己的内存地址空间,但是我想知道,

I'm aware that each process creates it's own memory address space, however I was wondering,

如果进程A具有以下功能:

If Process A was to have a function like :

int DoStuff() { return 1; }

和类似的指针typedef:

and a pointer typedef like :

typedef int(DoStuff_f*)();

和类似:的吸气功能:

DoStuff_f * getDoStuff() { return DoStuff; }

以及通过...说boost :: interprocess

and a magical way to communicate with Process B via... say boost::interprocess

是否可以将函数指针传递给进程B并调用

would it be possible to pass the function pointer to process B and call

直接来自过程B的过程A的DoStuff吗?

Process A's DoStuff from Process B directly?

推荐答案

否.所有函数指针都是进程地址空间中的一个地址.它没有不同过程所特有的固有标记.因此,即使将函数指针移至B之后,即使函数指针恰好仍然有效,它仍将代表进程B调用该函数.

No. All a function pointer is is an address in your process's address space. It has no intrinsic marker that is unique to different processes. So, even if your function pointer just happened to still be valid once you've moved it over to B, it would call that function on behalf of process B.

例如,如果您有

////PROCESS A////
int processA_myfun() { return 3; }
// get a pointer to pA_mf and pass it to process B

////PROCESS B////
int processB_myfun() { return 4; } // This happens to be at the same virtual address as pA_myfun
// get address from process A
int x = call_myfun(); // call via the pointer
x == 4;  // x is 4, because we called process B's version!

如果进程A和B运行相同的代码,则可能会在相同的地址处使用相同的功能-但您仍将使用B的数据结构和全局内存!所以简短的答案是,不,这不是您想要的方式!

If process A and B are running the same code, you might end up with identical functions at identical addresses - but you'll still be working with B's data structures and global memory! So the short answer is, no, this is not how you want to do this!

此外,诸如地址空间布局随机化之类的安全措施可以防止此类技巧"从没工作过.

Also, security measures such as address space layout randomization could prevent these sort of "tricks" from ever working.

您混淆了IPC和RPC. IPC用于传递数据,例如对象或文本块. RPC 用于使代码在远程进程中执行.

You're confusing IPC and RPC. IPC is for communicating data, such as your objects or a blob of text. RPC is for causing code to be executed in a remote process.

这篇关于是否可以在进程之间使用函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆