分享

dialogs in dll

 bluecrystal 2006-08-04

c


原著: randy more.

建立一个拥有自己的资源文件(例如对话框资源)的动态链接库工程 ,并在别的工程中调用.

in the dll function that pops up the dialog you must manage the state so that the dll code uses the dll‘s resources.


extern __declspec(dllexport) void showeditdialog(int &mydata1, int &mydata2)
{
//ensure we are using our own resources
afx_manage_state(afxgetstaticmodulestate());
cmylocaldialog dlg;
dlg.arg1 = mydata1; //specific local data for mylocaldialog
dlg.arg2 = mydata2;
dlg.domodal();
mydata1 = dlg.arg1; //data after processing
mydata2 = dlg.arg2;
}

外部调用这个动态链接库的程序中不能直接用 getlasterror()来判断错误的发生.这是因为 afx_manage_state macro建立一个临时的堆栈,当函数结束时会自动析构,其中保存的错误当然也不复存在。

解决方案 :


extern __declspec(dllexport) void showeditdialog(int &mydata1, int &mydata2)
{
dword dwlasterr = no_error;
//
// surround the code in brackets, which will cause the temporary
// object created by afx_manage_state to be destroyed before leaving
// the exported function.
//
// note : do not call mfc code outside of these brackets.
//
{
afx_manage_state(afxgetstaticmodulestate());
cmylocaldialog dlg;
dlg.arg1 = mydata1; //specific local data for mylocaldialog
dlg.arg2 = mydata2;
dlg.domodal();
mydata1 = dlg.arg1; //data after processing
mydata2 = dlg.arg2;
//
// save possible errors
//
dwlasterr = ::getlasterror();
}
//
// only set error if none is currently set.
// (last error will always be no_error _unless_
// tlsgetvalue failed earlier)
//
if (::getlasterror() == no_error)
::setlasterror(dwlasterr);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多