dialogs in dll

来源:百度文库 编辑:神马文学网 时间:2024/05/23 19:14:23
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 resourcesafx_manage_state(afxgetstaticmodulestate());cmylocaldialog dlg;dlg.arg1 = mydata1; //specific local data for mylocaldialogdlg.arg2 = mydata2;dlg.domodal();mydata1 = dlg.arg1; //data after processingmydata2 = 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 mylocaldialogdlg.arg2 = mydata2;dlg.domodal();mydata1 = dlg.arg1; //data after processingmydata2 = 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);}