@MasterCopipaster

Почему код из exe и dll работает по-разному?

Когда я собираю exeник с таким кодом то, все работает хорошо:

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>

net::io_context ioc;
websocket::stream<tcp::socket> ws{ ioc };

// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
{
    try
    {
        // Check command line arguments.
        std::string host = "192.168.1.104";
        auto const port = "3001";
        auto const text = "Hello, world!";

        // The io_context is required for all I/O
        //net::io_context ioc;

        // These objects perform our I/O
        tcp::resolver resolver{ ioc };
        //websocket::stream<tcp::socket> ws{ ioc };

        // Look up the domain name
        auto const results = resolver.resolve(host, port);

        // Make the connection on the IP address we get from a lookup
        auto ep = net::connect(ws.next_layer(), results);
       // ну и т.д.

Но когда я пытаюсь то же самое собрать как dll файл, то программа просто виснет, она собирается все ок... но ничего не происходит.

#include "stdafx.h"
#include "Project2.h"
#include "Header1.h"
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>


namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
// Reads the storage
net::io_context ioc;
websocket::stream<tcp::socket> ws{ ioc };

PROJECT2_API void connect(void) {
        std::string host = "192.168.1.104";
        auto const port = "3001";
        auto const text = "Hello, world!";
        //net::io_context ioc;
        //websocket::stream<tcp::socket> ws{ ioc };
        // The io_context is required for all I/O
        //net::io_context ioc;
        // These objects perform our I/O
        tcp::resolver resolver{ ioc };
        //ws{ ioc };
        // Look up the domain name
        auto const results = resolver.resolve(host, port);
        // Make the connection on the IP address we get from a lookup
        auto ep = net::connect(ws.next_layer(), results);
       // ну и т.д.

При этом, если для dll версии перенести строки

net::io_context ioc;
websocket::stream<tcp::socket> ws{ ioc };

в функцию, то тогда dll'ка работает отлично.

namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
// Reads the storage

PROJECT2_API void connect(void) {
        std::string host = "192.168.1.104";
        auto const port = "3001";
        auto const text = "Hello, world!";
        net::io_context ioc;
        websocket::stream<tcp::socket> ws{ ioc };
        // The io_context is required for all I/O
        //net::io_context ioc;
        // These objects perform our I/O
        tcp::resolver resolver{ ioc };
        //ws{ ioc };
        // Look up the domain name
        auto const results = resolver.resolve(host, port);
        // Make the connection on the IP address we get from a lookup
        auto ep = net::connect(ws.next_layer(), results);
       // ну и т.д.

В чем может быть проблема? - я явно не знаю о каких-то тонкостях с глобальными переменными в dll.
  • Вопрос задан
  • 507 просмотров
Решения вопроса 1
tsklab
@tsklab
Здесь отвечаю на вопросы.
Потому что программа выполняется полностью, а из DDL только функция, которую вы вызываете.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы