【发布时间】:2017-05-13 07:30:13
【问题描述】:
假设有一个结构
struct MyDataStructure
{
int a;
int b;
string c;
};
让一个dll暴露的接口中有一个函数。
class IDllInterface
{
public:
void getData(MyDataStructure&) = 0;
};
从加载 dll 的客户端 exe 中,以下代码是否安全?
...
IDllInterface* dll = DllFactory::getInterface(); // Imagine this exists
MyDataStructure data;
dll->getData(data);
...
当然,假设客户端和 dll 都知道 MyDataStructure。同样根据我的理解,由于代码是针对 dll 和 exe 分别编译的,因此对于不同的编译器/编译器版本,MyDataStructure 可能会有所不同。我的理解是否正确。
如果是这样,当使用不同的编译器/编译器版本时,如何在 dll 边界之间安全地传递数据。
【问题讨论】:
-
“我的理解正确吗。”是的。
-
没关系,这基本上就是进程中COM的工作方式。
-
啊。在我将其作为stackoverflow.com/questions/5661738/… 的副本关闭之前,您只是 获得了那个赏金集。
-
您的
MyDataStructure包含一个std::string对象,因此它与建议的副本完全相同。 -
如果你想要跨不同编译器的可移植性,你必须抛弃任何 C++ 库类型,只依赖已知大小的类型(int16_t 和 int32_t,而不是 short 和 int)。如果你只针对这个平台,你可能会得到 Windows API 提供的一些好处。
标签: c++ c dll cross-compiling