注意 本文前部份 引自 朱金灿 的关于对话框DoModal()函数调用失败的原因分析 .
地址:http://blog.csdn.net/clever101/article/details/2576968
typedef int (__stdcall* StarViwe_)(const char * , const char * , int );
HINSTANCE hmodule_StarViwe = ::LoadLibrary ("PrintForGxNx.dll");
if( NULL != hmodule_StarViwe)
{
AfxSetResourceHandle(hmodule_StarViwe);
StarViwe_ StarViwe = (StarViwe_)::GetProcAddress(hmodule_StarViwe, "TxtToPrn");
if(NULL != StarViwe)
{
CString Printname = "";
GetDlgItem(IDC_EDIT5)->GetWindowText(Printname);
StarViwe("","",0);//调用
::FreeLibrary (hmodule_StarViwe);
AfxSetResourceHandle(AfxGetInstanceHandle( ));
}
else
{
AfxMessageBox("TxtToPrn函数地址错误!");
::FreeLibrary (hmodule_StarViwe);
}
}
else
{
AfxMessageBox("PrintForGxNx.dll加载失败");
}
(2) 主程序的版本 与 DLL 的编译版本对应,即Debug /release 的一一对应
这里再补充说明一点:
最好在DLL弹出对话框前切换模块状态
extern "C" int _declspec(dllexport) __stdcall TxtToPrn(const char * PrnName, const char * FileName, int Delete)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
PageSetDlg pdlg = new PageSetDlg();
pdlg.DoModal();
return 0;
}
做到以上的规则 基本是不会有问题的了。
但是会出现以下情况:
二、
引用别人的工程,即使做到了一中说明的规则 依然报错
那么可能的原因就是 别人生成DLL采用的是静态模式。
现在你只要去 Project-->Settings---->Genrval页面
改成动态链接 即可。