-1
#pragma once
#include <curl/curl.h>
#include <spdlog/spdlog.h>

#include <string>

#include "json.hpp"

inline std::string GetData() {
  CURL* curl = curl_easy_init();
  std::ofstream file;
  file.open("C:/google_logs/test10.txt");
  file << "t";
  file.close();
  if (curl) {
      std::string all_game_data_url =
          "https://google.com";
      curl_easy_setopt(curl, CURLOPT_URL, all_game_data_url);
      curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
      curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
      
      FILE* filep = fopen("c:\\google_logs\\dump", "wb");
      curl_easy_setopt(curl, CURLOPT_STDERR, filep);

      std::string response_body;
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body);

      long response_code;
      curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
      
      spdlog::error("Response code: {:d}", response_code);
      CURLcode response = curl_easy_perform(curl);
      
      std::ofstream file;
      file.open("C:/google_logs/test19.txt");
      file << response;
      file.close();
      

      if (response != CURLE_OK) {
        spdlog::error("curl_easy_perform() failed: {:s}",
                      curl_easy_strerror(response));
        
        std::ofstream file;
        file.open("C:/google_logs/test14.txt");
        file << curl_easy_strerror(response);
        file.close();

      }

      curl_easy_cleanup(curl);

      return response_body;
  }
  return std::string();
}

It keeps hanging when curl_easy_perform is called, then it times out, and nothing is returned.

273K
  • 29,503
  • 10
  • 41
  • 64
Ryan Glenn
  • 1,325
  • 4
  • 17
  • 30
  • 1
    Please post a [mcve]. Creating threads in DLLMain is extremely close to an epic fail, and you haven't shown it. – 273K Oct 18 '22 at 04:34
  • 1
    `CURLOPT_URL, all_game_data_url` this is the error. Please be careful with passing data to the function with variable parameters. – 273K Oct 18 '22 at 04:37
  • @273K is there a more efficient way to do this? – Ryan Glenn Oct 18 '22 at 04:54

1 Answers1

0

When using the function curl_easy_setopt with the argument CURLOPT_URL, it expects a pointer to a C-style string (array of characters terminated by a null character) as the third argument. However, you are instead passing it a std::string.

Since curl_easy_set_opt is a variadic function, the std::string argument will not automatically be converted to a C-style string.

Therefore, I recommend that you change the line

curl_easy_setopt(curl, CURLOPT_URL, all_game_data_url);

to:

curl_easy_setopt(curl, CURLOPT_URL, all_game_data_url.c_str() );

It is also worth noting that creating a thread in DLLMain is risky. See this link for further information.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Hi Andreas, I was able to get that to work. However, I'm trying to create a worker thread in my DllMain that will periodically (every second) make this curl http request for some data that is then passed to another thread (using std::queue, by just popping off an element from the queue). It all works if I only do the http request once, but if I try to do an infinite loop, and just `Sleep()` every second, it just hangs the entire application. – Ryan Glenn Oct 18 '22 at 16:43
  • @RyanGlenn: Are you doing all of this inside DllMain? Does that mean the function DllMain never returns, because it has an infinite loop? This is not the way to use the function `DllMain`. It should only be used for minimal initialization and should return immediately afterwards. See the link in my answer about DllMain for further information. – Andreas Wenzel Oct 18 '22 at 16:53