http://expert.csdn.net/Expert/topic/1175/1175644.xml?temp=.8361017
刚才的贴子结了才发现debug过了,release版执行出错,再来50分!错在:
_AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
{ ASSERT(afxCurrentResourceHandle != NULL);
return afxCurrentResourceHandle; }大家再帮忙看看,paul2002()再来.
刚才的贴子结了才发现debug过了,release版执行出错,再来50分!错在:
_AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
{ ASSERT(afxCurrentResourceHandle != NULL);
return afxCurrentResourceHandle; }大家再帮忙看看,paul2002()再来.
在DLL中要用
_stdcall 即CALLBACK,不能用_cdecl,我觉得。
应是callee完成清栈工作
{
if (dwReason == DLL_PROCESS_ATTACH)
{
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(extensionDLL, hInstance))
return false;
} return(true);
} 然后,每次使用 DLL 资源时,你必须改变资源的句柄,使其指向 DLL,并保存 exe 的资源,以便以后正确恢复。void get_DLL_resource(void)
{
/* this function changes the resource handle to that of the DLL */
//这个函数改变资源句柄使其指向DLL
if (resource_counter == 0)
{
save_hInstance = AfxGetResourceHandle();
AfxSetResourceHandle(extensionDLL.hModule);
} resource_counter++;
} 接着你需要其它函数来恢复资源句柄:void reset_DLL_resource(void)
{
/* this function restores the resource handle set by
'get_DLL_resource()' */ if (resource_counter > 0)
resource_counter--; if (resource_counter == 0)
AfxSetResourceHandle(save_hInstance);
} 接下来一点非常重要,只要有可能就必须恢复资源句柄,否则,你将会遇到许多问题。原因是可执行文件必须重画工具条等等,比如说,如果用户移动 DLL 的对话框,如果资源句柄仍然为 DLL 的资源,程序就崩溃了,我发现最好恢复句柄的时机在对话框的 OnInitDialog() 中,这时对话框的模板等已经读出了。
http://www.codeproject.com/debug/releasemode.asp
http://expert.csdn.net/Expert/TopicView1.asp?id=1176869
/////
extern "C" __declspec(dllexport) void TestDLG(char *c1,char *c2,long lData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
MYDLG dlg;
CString csTemp=c1;
dlg.m_cs1=csTemp;
csTemp=c2;
dlg.m_cs2=csTemp;
dlg.m_ldata=lData;
dlg.DoModal();
}//////
void CTestdllDlg::OnButton1()
{
typedef void(CDECL *DLLFUNC)(char *c1,char *c2,long lData);
HINSTANCE hOldInst = AfxGetResourceHandle();
HINSTANCE hDll = AfxLoadLibrary("mydll.dll");
AfxSetResourceHandle(hDll);
DLLFUNC FUNCtest;
if ( hDll!=NULL )
{
FUNCtest=(DLLFUNC)GetProcAddress(hDll,"TestDLG");
DWORD dw=GetLastError();
FUNCtest("测1","测2",5);
}
AfxSetResourceHandle( hOldInst );
AfxFreeLibrary( hDll );
}