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

 

posted on   wu.g.q  阅读(174)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示