如何在主程序下调用DLL,显示一非模式对话框!!

njluhao 2006-07-23 12:07:34
如何在主程序下调用DLL,显示一非模式对话框!!?
我在dll中实现了显示模式对话框,可是改成显示非模式对话框就会出错,
下面是FaultCheck.dll中的外部函数:
extern "C" __declspec(dllexport) void Test()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
pDlg = new CDialog;
if (pDlg != NULL)
{
pDlg->Create(IDD_DIALOG1);
pDlg->ShowWindow(SW_SHOWNOACTIVATE);

}
}
exe主程序中如下调用:
void CTestDllDlg::OnButton2()
{
typedef void (FAR __cdecl *MYDLL)(void); //

HINSTANCE hinstDLL=NULL; //
hinstDLL=LoadLibrary(GetCurrentDir()+"FaultCheck.dll"); //
if (hinstDLL)
{
MYDLL proc;
proc=(MYDLL)GetProcAddress(hinstDLL,"Test");
if(proc==NULL) AfxMessageBox("can't find function");
else proc();
FreeLibrary(hinstDLL);
}
else
{
AfxMessageBox("FaultCheck.dll装载失败...");
}
}
编译运行时,DIALOG1可以显示,可是马上就windows就会报错!棘手中,请大侠赐教,分不够可再加!
...全文
207 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
syy64 2006-07-23
  • 打赏
  • 举报
回复
写错了,应该这样。
extern "C" __declspec(dllexport) void Test(HWND hMainWnd)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CXXXDlg pDlg(CWnd::FromHandle(hMainWnd));
pDlg = new CDialog();
if (pDlg != NULL)
{
pDlg->Create(IDD_DIALOG1);
pDlg->ShowWindow(SW_SHOWNOACTIVATE);

}
}

njluhao 2006-07-23
  • 打赏
  • 举报
回复
论坛和外面的引擎上,我都搜索过了,好象还没有发现完备的方法给出,希望大侠们能给予帮助,整理后加入FAQ,为后来的朋友们提供便利!
njluhao 2006-07-23
  • 打赏
  • 举报
回复
to syy64(太平洋) :
pDlg = new CDialog(CWnd::FromHandle(hMainWnd));
这句有问题的啊,
CDialog(const char *,class CWnd *)':cannot convert parameter 1 from 'class CWnd *' to 'const char *'
感谢支持!!!
njluhao 2006-07-23
  • 打赏
  • 举报
回复
to DentistryDoctor(牙医的目标是没有蛀牙):
是Regular Dlls……
syy64 2006-07-23
  • 打赏
  • 举报
回复
extern "C" __declspec(dllexport) void Test(HWND hMainWnd)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
pDlg = new CDialog(CWnd::FromHandle(hMainWnd));
if (pDlg != NULL)
{
pDlg->Create(IDD_DIALOG1);
pDlg->ShowWindow(SW_SHOWNOACTIVATE);

}
}
DentistryDoctor 2006-07-23
  • 打赏
  • 举报
回复
断言?什么断言?是不是ResourceInstance不对?你的DLL可是MFC的扩展DLL?
njluhao 2006-07-23
  • 打赏
  • 举报
回复
另外想问to xing_xing_xing(ζ未名ζ) :FreeLibrary 得太早了,用完在 Free,应该如何free!请到http://community.csdn.net/Expert/topic/4898/4898925.xml?temp=.8572351领分
njluhao 2006-07-23
  • 打赏
  • 举报
回复
to xing_xing_xing(ζ未名ζ) :
非常感谢,你是对的!大意啊!给分!
to syy64(太平洋):
同样感谢你的支持,不过好象你贴出的代码是模式对话框的!对你倾力相助的精神表示感谢!!!
syy64 2006-07-23
  • 打赏
  • 举报
回复
// TestDll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "TestDll.h"
#include "DlgDBConnect.h"
#include "DlgRecordset.h"
#include "DlgTableDesign.h"
#include "DlgTest.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HWND hMainWnd;
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CTestDllApp

BEGIN_MESSAGE_MAP(CTestDllApp, CWinApp)
//{{AFX_MSG_MAP(CTestDllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDllApp construction

CTestDllApp::CTestDllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestDllApp object

CTestDllApp theApp;
BOOL bIsOpenDB(FALSE);

BOOL CTestDllApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
/* if (!AfxOleInit())
{
AfxMessageBox("ole ³õʼ»¯´íÎó");
return FALSE;
}*/

AfxEnableControlContainer();//
return CWinApp::InitInstance();
}

extern "C" __declspec(dllexport) void showDBConndlg(HWND hWndParent)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgDBConnect dlg(CWnd::FromHandle(hWndParent));
hMainWnd = hWndParent;
dlg.DoModal();
bIsOpenDB=TRUE;
}
extern "C" __declspec(dllexport) void showRecorddlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgRecordset dlg(CWnd::FromHandle(hMainWnd));
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);
}
extern "C" __declspec(dllexport) void closeconn()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// CDlgTest dlg(CWnd::FromHandle(hMainWnd));
//dlg.DoModal();
if(theApp.m_pConnection)
{
theApp.m_pConnection->Close();
theApp.m_pConnection.Release();
bIsOpenDB=FALSE;
}

}
extern "C" __declspec(dllexport) void showTableDesigndlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlgTableDesign dlg(CWnd::FromHandle(hMainWnd));
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);

}

extern "C" __declspec(dllexport) void showTestdlg()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

CDlgTest dlg(AfxGetApp()->GetMainWnd());
//hMainWnd=hWndParent;
if(bIsOpenDB)
dlg.DoModal();
else
::MessageBox(NULL,"Êý¾Ý¿âδ´ò¿ª£¬ÇëÏÈÁ¬½ÓÊý¾Ý¿â£¡","Ìáʾ",MB_OK);

}
xing_xing_xing 2006-07-23
  • 打赏
  • 举报
回复
FreeLibrary 得太早了,用完在 Free
njluhao 2006-07-23
  • 打赏
  • 举报
回复
我顶
njluhao 2006-07-23
  • 打赏
  • 举报
回复
to syy64(太平洋) :
你的意思是要从主程序中将句柄传给dll,不过好象一般不常用这种方式,大多是在dll中自己去获取,但是这样还是出现问题,希望大侠能提供一个demo,我将开贴再送100分!

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧