C++ 从DLL文件C+;调用类定义的函数+;

C++ 从DLL文件C+;调用类定义的函数+;,c++,visual-studio,dll,dllimport,dllexport,C++,Visual Studio,Dll,Dllimport,Dllexport,我已经从Amazon IoT设备SDK构建了一个示例项目,它是examples文件夹中的PubSub示例项目 我必须将该项目作为DLL文件输出,以便该示例可以在RAD studio中使用,我已经成功地构建了RAD studio。使用生成DLL的教程,我在类中的函数定义中添加了u declspec(dllexport)行 PubSub.hpp文件 #ifdef PUBSNUB_EXPORTS #define PUBSNUB_API __declspec(dllexport) #else #defi

我已经从Amazon IoT设备SDK构建了一个示例项目,它是examples文件夹中的PubSub示例项目

我必须将该项目作为DLL文件输出,以便该示例可以在RAD studio中使用,我已经成功地构建了RAD studio。使用生成DLL的教程,我在类中的函数定义中添加了u declspec(dllexport)行

PubSub.hpp文件

#ifdef PUBSNUB_EXPORTS
#define PUBSNUB_API __declspec(dllexport)
#else
#define PUBSNUB_API __declspec(dllimport)
#endif

#include "mqtt/Client.hpp"
#include "NetworkConnection.hpp"


namespace awsiotsdk {
    namespace samples {
        class PubSub {
        protected:
            std::shared_ptr<NetworkConnection> p_network_connection_;
            std::shared_ptr<mqtt::ConnectPacket> p_connect_packet_;
            std::atomic_int cur_pending_messages_;
            std::atomic_int total_published_messages_;
            std::shared_ptr<MqttClient> p_iot_client_;

            __declspec(dllexport) ResponseCode RunPublish(int msg_count);
            __declspec(dllexport) ResponseCode SubscribeCallback(util::String topic_name,
                                           util::String payload,
                                           std::shared_ptr<mqtt::SubscriptionHandlerContextData> p_app_handler_data);
            __declspec(dllexport) ResponseCode DisconnectCallback(util::String topic_name,
                                            std::shared_ptr<DisconnectCallbackContextData> p_app_handler_data);
            __declspec(dllexport) ResponseCode ReconnectCallback(util::String client_id,
                                           std::shared_ptr<ReconnectCallbackContextData> p_app_handler_data,
                                           ResponseCode reconnect_result);
            __declspec(dllexport) ResponseCode ResubscribeCallback(util::String client_id,
                                             std::shared_ptr<ResubscribeCallbackContextData> p_app_handler_data,
                                             ResponseCode resubscribe_result);
            __declspec(dllexport) ResponseCode Subscribe();
            __declspec(dllexport) ResponseCode Unsubscribe();
            __declspec(dllexport) ResponseCode InitializeTLS();

        public:
            __declspec(dllexport) ResponseCode RunSample();
        };
    }
}
#ifdef PUBSNUB#u导出
#定义PUBSNUB_API___declspec(dllexport)
#否则
#定义PUBSNUB_API___declspec(dllimport)
#恩迪夫
#包括“mqtt/Client.hpp”
#包括“NetworkConnection.hpp”
名称空间awsiotsdk{
名称空间示例{
类PubSub{
受保护的:
std::共享ptr p_网络连接u;
std::共享\u ptr p\u连接\u数据包\uu;
std::原子当前挂起消息;
std::原子\u int总数\u已发布消息\u;
std::共享ptr物联网客户机;
__declspec(dllexport)响应代码RunPublish(int msg_count);
__declspec(dllexport)响应代码SubscribeCallback(util::String topic_name,
util::字符串有效负载,
std::共享ptr p_应用程序处理程序(数据);
__declspec(dllexport)ResponseCode DisconnectCallback(util::String topic_name,
std::共享ptr p_应用程序处理程序(数据);
__declspec(dllexport)响应代码重新连接回调(util::String client_id,
std::共享的\u ptr p\u应用程序\u处理程序\u数据,
响应代码(U结果);
__declspec(dllexport)响应代码重新订阅回调(util::String client_id,
std::共享的\u ptr p\u应用程序\u处理程序\u数据,
响应代码重新订阅结果);
__declspec(dllexport)响应代码Subscribe();
__declspec(dllexport)响应代码Unsubscribe();
__declspec(dllexport)ResponseCode InitializeTLS();
公众:
__declspec(dllexport)响应代码RunSample();
};
}
}
我一直不知道如何在示例控制台项目中调用RunSample(),以确保我的DLL文件正常工作。我试着直接调用这个函数,但它说它是未定义的

#include <iostream>
#include "PubSub.hpp"

int main()
{
    RunSample();
}
#包括
#包括“PubSub.hpp”
int main()
{
RunSample();
}

不能调用没有成员的成员函数,但这是基本C++知识。此外,还有名称空间。此外,您可能需要导出该类,而不是该类的各个方法。因此,对于这个特定类,我会将PyBuffScript(DLExcel)放在PubSII类的前面,不记得(已经有一段时间),在中间还是在中间。不过,您应该能够很容易地找到示例。需要
pubsnuapi
而不是
\uuudeclspec(dllexport)
-因为对于dll,这必须是
\udeclspec(dllexport)
,对于调用方-
\udeclspec(dllimport)
。当然,首先您需要创建类的实例,然后再对其调用成员函数。@RbMn好的,我已经将u declspec(dllexport)替换为方法定义前面的pubsnu_API,但我是否将pubsnu_API放在“类PubSub”前面?