上个图先……
图中的ADll是一个DLL工程,属于MFC扩展DLL。在资源中添加了一个对话框IDD_SHOW,然后为该资源创建了一个CShow类(Show.h/Show.cpp),添加了AFX_EXT_CLASS,并在stdafx.h中添加了"#include resource.h"。
编译OK,该工程完成……再看下面一个调用该DLL的对话框MFC应用程序:Stub。在对话框上添加一个按钮,增加双击消息响应函数,如下所示:
void CStubDlg::OnBnClickedDirect()
{
// TODO: 在此添加控件通知处理程序代码// AFX_MANAGE_STATE(AfxGetStaticModuleState())
HINSTANCE curInstance = AfxGetInstanceHandle();
HINSTANCE dllInstance = GetModuleHandle("ADll.dll"); AfxSetResourceHandle(dllInstance); CShow show("IDD_SHOW");
show.DoModal(); AfxSetResourceHandle(curInstance);
}
继续在该cpp文件中添加如下内容:
#include "../ADll/Show.h"
#pragma comment(lib, "../Debug/ADll.lib")
编译OK,改项目也完成了~----------------哥-------------个---------------------割-------------------嗝-----------------运行Stub,然后点击调用DLL的按钮,结果无反应……
再回到上图,看curInstance 和dllInstance 这两个变量,通过调试,发现两者的值是一样的。这是肿么回事?
在网上查过好多资料了,有一种解决方法是,在DLL中另建一个导出类,在导出类中调用目标类,在应用程序中和导出类来打交道。感觉这样中转一下有点麻烦,难道就不能直接使用这个目标扩展类么?求大牛帮忙解释上面的问题……小弟不甚感激涕零内牛满面另:征MFC高手一枚当师傅用,莫愁姐优先~

解决方案 »

  1.   


    大哥意思是说不需要资源句柄的转换直接使用CShow?我试了不行哦……
      

  2.   

    AfxGetInstanceHandle  
    HINSTANCE AfxGetInstanceHandle( );Return ValueAn HINSTANCE to the current instance of the application. If called from within a DLL linked with the USRDLL version of MFC, an HINSTANCE to the DLL is returned.ResThis function allows you to retrieve the instance handle of the current application. AfxGetInstanceHandle always returns the HINSTANCE of your executable file (.EXE) unless it is called from within a DLL linked with the USRDLL version of MFC. In this case, it returns an HINSTANCE to the DLL.
      

  3.   


    额……啥意思?
    我那个就是要获取调用者的HINSTANCE,而不是DLL的HINSTANCE。这段代码不是在DLL里面。