關于DLL輸出問題

easypower 2003-12-25 08:49:36
用嚮導自動生成如下代碼
#ifdef BOLTDB_MODULE
#define DLLIMPEXP __declspec(dllexport)
#else
//----- Note: we don't use __declspec(dllimport) here, because of the
//----- "local vtable" problem with msvc. If you use __declspec(dllimport),
//----- then, when a client dll does a new on the class, the object's
//----- vtable pointer points to a vtable allocated in that client
//----- dll. If the client dll then passes the object to another dll,
//----- and the client dll is then unloaded, the vtable becomes invalid
//----- and any virtual calls on the object will access invalid memory.
//-----
//----- By not using __declspec(dllimport), we guarantee that the
//----- vtable is allocated in the server dll during the ctor and the
//----- client dll does not overwrite the vtable pointer after calling
//----- the ctor. And, since we expect the server dll to remain in
//----- memory indefinitely, there is no problem with vtables unexpectedly
//----- going away.
#define DLLIMPEXP
#endif

class DLLIMPEXP Bolt
{
......
}

我想請問#else和#define DLLIMPEXP中間的注釋說的甚麼意思,沒看懂.
...全文
54 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
easypower 2003-12-25
  • 打赏
  • 举报
回复
謝謝.
實際上按上述代碼,無法導出Bolt類給其他工程使用,而我將類改成
class __declspec(dllexport) Bolt
{
......
}

就成功了,我很困惑.
cadinfo 2003-12-25
  • 打赏
  • 举报
回复
这里包含了VC6的dll生成规则,你可以见到其实VC6关于dll向导生成项目存在
__declspec(dllexport)
__declspec(dllimport)
dllimport用于在Server程序中导入.h中声明的函数,
而这里的意思是说由于vtbl的问题,无需用dllimport来修饰。

原因是客户端dll可能被unload。

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧