【发布时间】:2009-03-07 09:15:50
【问题描述】:
有一个类被编译成dll
//HeaderFile.h
//version 1.0
class __declspec(dllexport) A {
int variable;
//member functions omitted for clarity
};
//implementation file omitted for clarity
您从编译成的 dll 构建一个使用上述类的 exe
#include "HeaderFile.h"
int main() {
A *obj = new A();
obj->CallSomeFuncOnObj();
//
//whatever
//
}
到目前为止,您的程序运行良好。但是现在您重新编译您的 dll 使用以下代码
//HeaderFile.h
//version 2.0
class __declspec(dllexport) A {
int variable;
int anotherVariable;
//member functions omitted for clarity
};
//implementation file omitted for clarity
并且您不要重新编译您的 exe,而是开始使用旧 exe 中重新编译的 dll。现在会发生的是,您的 exe 具有将分配内存 = sizeof(A 类 1.0 版)的代码,但您的新 dll 中的构造函数的代码假定它正在传递一个内存块 = sizeof(A 类 2.0 版)。两者之间有一个整数的价值大小差异 - 一个不可预测的秘诀。
一个类似的例子出现在一本好书的第一章 - Don Box 的 Essential COM。现在来回答这个问题。在 c#(或任何其他 .Net 语言)的类似情况下会发生什么?
【问题讨论】: