同一个进程加载同一个动态库两次的现象

我猜,同一个进程加载同一个动态库两次,用的其实是同一份内存空间。下面直接根据代码看结果。

// 下面的测试动态库Test.dll的代码,就一个头文件dll_global.h和一个main.cpp.

#ifndef DLL_GLOBAL_H
#define DLL_GLOBAL_H

#ifdef __cplusplus
#define D_EXTERN_C extern "C"
#else
#define D_EXTERN_C
#endif

#ifdef __SHARE_EXPORT
#define D_SHARE_EXPORT D_DECL_EXPORT
#else
#define D_SHARE_EXPORT D_DECL_IMPORT
#endif

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
#define D_CALLTYPE __stdcall
#define D_DECL_EXPORT __declspec(dllexport)
#define D_DECL_IMPORT
#else
#define __stdcall
#define D_CALLTYPE
#define D_DECL_EXPORT __attribute__((visibility("default")))
#define D_DECL_IMPORT __attribute__((visibility("default")))
#endif

D_EXTERN_C D_SHARE_EXPORT void D_CALLTYPE Dll_SetValue(int value);
D_EXTERN_C D_SHARE_EXPORT int D_CALLTYPE Dll_GetValue();

#endif // DLL_GLOBAL_H

============================================================================ 

#include "dll_global.h"

int g_value = 0;

//---------------------------------------------------------------------------
void Dll_SetValue(int value)
{
    g_value = value;
}
//---------------------------------------------------------------------------
int Dll_GetValue()
{
    return g_value;
}
//---------------------------------------------------------------------------
// 下面是测试工具的代码 
#include <QCoreApplication>
#include <QString>
#include <QLibrary>
#include <QDebug>

typedef int (_stdcall *Dll_SetValue)(int value);
typedef int (_stdcall *Dll_GetValue)();

int loadDll_SameDllTwice()
{
    qDebug() << "===========loadDll_SameDllTwice=========";
    Dll_SetValue pDll_SetValue1{nullptr};
    Dll_GetValue pDll_GetValue1{nullptr};
    {
        QLibrary qLib(QString("./TestDll.dll"));
        if(false == qLib.load())
            return -1;

        pDll_SetValue1 = (Dll_SetValue)qLib.resolve("Dll_SetValue");
        if(nullptr == pDll_SetValue1)
            return -2;

        pDll_GetValue1 = (Dll_GetValue)qLib.resolve("Dll_GetValue");
        if(nullptr == pDll_GetValue1)
            return -3;
    }

    Dll_SetValue pDll_SetValue2{nullptr};
    Dll_GetValue pDll_GetValue2{nullptr};
    {
        QLibrary qLib(QString("./TestDll.dll"));
        if(false == qLib.load())
            return -1;

        pDll_SetValue2 = (Dll_SetValue)qLib.resolve("Dll_SetValue");
        if(nullptr == pDll_SetValue2)
            return -2;

        pDll_GetValue2 = (Dll_GetValue)qLib.resolve("Dll_GetValue");
        if(nullptr == pDll_GetValue2)
            return -3;
    }

    qDebug() << "pDll_SetValue1: " << &pDll_SetValue1;
    qDebug() << "pDll_SetValue2: " << &pDll_SetValue2;
    qDebug() << "pDll_GetValue1: " << &pDll_GetValue1;
    qDebug() << "pDll_GetValue2: " << &pDll_GetValue2;

    pDll_SetValue1(123);
    qDebug() << "pDll_SetValue1(123), pDll_GetValue2=" << pDll_GetValue2() << endl;

    return 0;
}

int loadDll_DiffDllOnce()
{
    qDebug() << "===========loadDll_DiffDllOnce=========";
    Dll_SetValue pDll_SetValue1{nullptr};
    Dll_GetValue pDll_GetValue1{nullptr};
    {
        QLibrary qLib(QString("./TestDll1.dll"));
        if(false == qLib.load())
            return -1;

        pDll_SetValue1 = (Dll_SetValue)qLib.resolve("Dll_SetValue");
        if(nullptr == pDll_SetValue1)
            return -2;

        pDll_GetValue1 = (Dll_GetValue)qLib.resolve("Dll_GetValue");
        if(nullptr == pDll_GetValue1)
            return -3;
    }

    Dll_SetValue pDll_SetValue2{nullptr};
    Dll_GetValue pDll_GetValue2{nullptr};
    {
        QLibrary qLib(QString("./TestDll2.dll"));
        if(false == qLib.load())
            return -1;

        pDll_SetValue2 = (Dll_SetValue)qLib.resolve("Dll_SetValue");
        if(nullptr == pDll_SetValue2)
            return -2;

        pDll_GetValue2 = (Dll_GetValue)qLib.resolve("Dll_GetValue");
        if(nullptr == pDll_GetValue2)
            return -3;
    }

    qDebug() << "pDll_SetValue1: " << &pDll_SetValue1;
    qDebug() << "pDll_SetValue2: " << &pDll_SetValue2;
    qDebug() << "pDll_GetValue1: " << &pDll_GetValue1;
    qDebug() << "pDll_GetValue2: " << &pDll_GetValue2;

    pDll_SetValue1(234);
    qDebug() << "pDll_SetValue1(234), pDll_GetValue2=" << pDll_GetValue2() << endl;

    return 0;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    loadDll_SameDllTwice();

    loadDll_DiffDllOnce();

    return a.exec();
}

===========loadDll_SameDllTwice=========
pDll_SetValue1:  0x60fe14
pDll_SetValue2:  0x60fe1c
pDll_GetValue1:  0x60fe18
pDll_GetValue2:  0x60fe20
pDll_SetValue1(123), pDll_GetValue2= 123

===========loadDll_DiffDllOnce=========
pDll_SetValue1:  0x60fe14
pDll_SetValue2:  0x60fe1c
pDll_GetValue1:  0x60fe18
pDll_GetValue2:  0x60fe20
pDll_SetValue1(234), pDll_GetValue2= 0

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值