Dll

本文介绍了一个简单的DLL(动态链接库)创建与使用的示例。通过C++实现了一个导出函数MyFunction,并展示了如何从另一个应用程序加载并调用此DLL中的函数。涉及DLL的加载、函数获取及调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dll.cpp

 
extern "C" _declspec(dllexport) int MyFunction(int x);

// CDLL7App 初始化

BOOL CDLL7App::InitInstance()
{
    CWinApp::InitInstance();

    return TRUE;
}


extern "C" _declspec(dllexport) int MyFunction(int x)
{
    return x * 10;
}

 

dll.def


EXPORTS
    MyFunction @1

 

call


        typedef int (* MyFunction)(int);
        typedef int (CALLBACK* fndll3)(void);


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // 初始化 MFC 并在失败时显示错误
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: 更改错误代码以符合您的需要
        _tprintf(_T("错误: MFC 初始化失败/n"));
        nRetCode = 1;
    }
    else
    {

        HINSTANCE hDLL; // Handle to DLL
        MyFunction lpfnDllFunc1; // Function pointer


        double uParam2, uReturnVal;

        hDLL = LoadLibrary(_T("dll7.dll"));
        if (hDLL != NULL)
        {
            lpfnDllFunc1 = (MyFunction)GetProcAddress(hDLL,
                ("MyFunction"));
            if (!lpfnDllFunc1)
            {
                // handle the error
                FreeLibrary(hDLL);
                return -1;
            }
            else
            {
                // call the function
                uReturnVal = lpfnDllFunc1(113);
                printf("%f/r/n", uReturnVal);
            }
        }

    }

    return nRetCode;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值