MFC 动态dll调用函数编码实现
一、
1.先打开Microsoft Visual Studio 2010软件。
2.点击新建项目->选择MFC DLL,在名称处填写“DllTest”,点击确定。
3.点击下一步->选择“使用共享MFC DLL的规则 DLL”,点击完成。
4.在“DllTest.h”文件中,添加extern “C” __declspec(dllexport)bool add(int a,int b,int *c);语句。
5.在“DllTest.def”文件中,在输入add。
6.在“DllTest.cpp”文件中,添加如下语句:
extern "C" __declspec(dllexport)bool add(int a,int b,int *c)
{
*c=a+b;
return true;
}
修改后的这三个文件为:
DllTest.h
// DllTest.h : main header file for the DllTest DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CDllTestApp
// See DllTest.cpp for the implementation of this class
//
class CDllTestApp : public CWinApp
{
public:
CDllTestApp();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
extern "C" __declspec(dllexport)bool add(int a,int b,int *c);
DllTest.def
; DllTest.def : Declares the module parameters for the DLL.
LIBRARY
EXPORTS
; Explicit exports can go here
add
DllTest.cpp
// DllTest.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "DllTest.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // //TODO: 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. // // CDllTestApp BEGIN_MESSAGE_MAP(CDllTestApp, CWinApp) END_MESSAGE_MAP() // CDllTestApp construction CDllTestApp::CDllTestApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } // The one and only CDllTestApp object CDllTestApp theApp; // CDllTestApp initialization BOOL CDllTestApp::InitInstance() { CWinApp::InitInstance(); return TRUE; } extern "C" __declspec(dllexport)bool add(int a,int b,int *c) { *c=a+b; return true; }
编译生成DllTest.dll
二、再新建一个基于对话框的工程,再ok按钮事件函数中添加如下代码:
void CCreateThreadDlg::SetEditText(CString strText) { GetDlgItem(IDC_EDIT1)->SetWindowText(strText); } void CCreateThreadDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); HINSTANCE hPro= ::LoadLibrary("DllTest.dll"); if(hPro == NULL) { return; } else { typedef bool (*DLL_ADD)( int a,int b,int *c); DLL_ADD dllAddFunc = NULL; dllAddFunc=(DLL_ADD)::GetProcAddress(hPro, "add"); if(dllAddFunc) { int s= 0; dllAddFunc(8,4,&s); CString strSum; strSum.Format("%d", s); SetEditText(strSum); } ::FreeLibrary(hPro); } }
再把DllTest.dll复制到测试工程的exe同级目录,然后编译、运行,对话框中的输入框值为12
浙公网安备 33010602011771号