c++

c++怎么调用dll中的共享数据

小亿
113
2024-03-21 10:39:45
栏目: 编程语言

在C++中调用DLL中的共享数据需要使用外部变量来引用DLL中的数据。以下是一个简单的示例:

假设有一个名为 sharedData.dll 的 DLL,其中包含一个共享的整型变量 sharedInt

首先,在DLL中声明共享的数据:

// sharedData.dll
extern "C" {
    __declspec(dllexport) int sharedInt;
}

然后在C++程序中引用并使用共享的数据:

#include <iostream>

extern "C" {
    __declspec(dllimport) int sharedInt;
}

int main() {
    std::cout << "Shared data from DLL: " << sharedInt << std::endl;
    
    // 修改共享数据
    sharedInt = 10;
    
    std::cout << "Modified shared data: " << sharedInt << std::endl;
    
    return 0;
}

在这个示例中,我们在C++程序中使用 extern "C" 来引用DLL中的共享数据 sharedInt,并可以对其进行读取和修改操作。需要注意的是,在使用共享数据之前,需要确保DLL已经被加载并且共享数据已经被初始化。

0
看了该问题的人还看了