⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdllview.cpp

📁 这是书上的代码
💻 CPP
字号:
// TestDLLView.cpp : implementation of the CTestDLLView class
//

#include "stdafx.h"
#include "TestDLL.h"

#include "MyClass.h"         //added by user
#include "DLLDlg2.h"         //added by user
#include "MyDLLDlg.h"        //added by user
#include "Foo.h"             //added by user

#include "TestDLLDoc.h"
#include "TestDLLView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView

IMPLEMENT_DYNCREATE(CTestDLLView, CView)

BEGIN_MESSAGE_MAP(CTestDLLView, CView)
	//{{AFX_MSG_MAP(CTestDLLView)
	ON_COMMAND(ID_EXPROT_OBJECT, OnExprotObject)
	ON_COMMAND(ID_EXPORT_BYUI, OnExportByUI)
	ON_COMMAND(ID_EXPORT_DIALOG, OnExportDialog)
	ON_COMMAND(ID_EXPORT_OBJECT_EXPLICT, OnExportObjectExplict)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView construction/destruction

CTestDLLView::CTestDLLView()
{
	// TODO: add construction code here

}

CTestDLLView::~CTestDLLView()
{
}

BOOL CTestDLLView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView drawing

void CTestDLLView::OnDraw(CDC* pDC)
{
	CTestDLLDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView printing

BOOL CTestDLLView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestDLLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestDLLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView diagnostics

#ifdef _DEBUG
void CTestDLLView::AssertValid() const
{
	CView::AssertValid();
}

void CTestDLLView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDLLDoc* CTestDLLView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDLLDoc)));
	return (CTestDLLDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestDLLView message handlers
//-----------------------导出类对象--------------------------------------
void CTestDLLView::OnExprotObject() 
{
	_declspec(dllimport) CMyClass myObject; //导入类对象
	CString str = myObject.str1;            //得到CMyClass类中等成员
	MessageBox(str,"导出类对象");           //显示在消息框中
}
//---------------------通过接口类导出函数和变量--------------------------
void CTestDLLView::OnExportByUI() 
{
	CDLLDlg2 dlg;           //显示对话框
	dlg.DoModal();
}
//---------------------导出MFC扩展DLL中的对话框类------------------------
void CTestDLLView::OnExportDialog() 
{
	CMyDLLDlg Dlg ;          //显示对话框
	Dlg.DoModal();	
}
//----------------------显示连接MFC扩展DLL导出类--------------------------
void CTestDLLView::OnExportObjectExplict() 
{
	typedef CFoo* (MYFUNC)(void);          //定义导出函数类型
	HINSTANCE hDLL = AfxLoadLibrary("DLL1.dll");//加载DLL
	if(hDLL != NULL)
	{
		MYFUNC* pnProc;
		pnProc = (MYFUNC*)GetProcAddress(hDLL,"CreateFoo");//得到导出函数在进程中的地址
		CFoo* pFoo = (*pnProc)();
		CString str = pFoo->Initialize("显式链接接调用CFooImp类成员函数");
		MessageBox(str,"导出类");          //显示结果
		delete pFoo;                       //释放动态内存
		FreeLibrary(hDLL);                 //接触DLL在进程的映射
	}
}
//-----------------------------The End------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -