M
M
MasterCopipaster2021-05-29 17:17:02
Boost
MasterCopipaster, 2021-05-29 17:17:02

Why does exe and dll code work differently?

When I build an exe with this code, everything works fine:

#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);
       // ну и т.д.

But when I try to assemble the same as a dll file, the program just hangs, it's going to be all right... but nothing happens.

#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);
       // ну и т.д.

At the same time, if for the dll version move the lines

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

into a function, then the dll works fine.

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);
       // ну и т.д.

What could be the problem? - I obviously do not know about some subtleties with global variables in dll.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2021-05-29
@MasterCopipaster

Because the program is executed completely, and from DDL only the function that you call.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question