13

What would be the easiest way to convert the "res" variable (CURLcode) into a CString?

Here's the standard example which compiles fine on my machine but I want to use this in a MFC app and display the result as a MessageBox. Any help is appreciated!

#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}
kogh
  • 995
  • 4
  • 17
  • 30

2 Answers2

12

You can use curl_easy_strerror function.

CString str(curl_easy_strerror(res));

or

CString str;
str.Format("curl_easy_perform return %s [%d]",curl_easy_strerror(res),res);
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
vromanov
  • 881
  • 6
  • 11
  • 1
    It seems you need to use curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,myBuffer) first, based on http://curl.haxx.se/libcurl/c/curl_easy_setopt.html – Suma Jun 26 '12 at 14:22
  • For some reason I can't get CString to be recognized. I'm including `curl/curl.h`, do I need anything else? – intrigued_66 Feb 02 '23 at 18:42
3

A CURLcode is a number, so after 4 seconds on Google and having never used MFC, I found you can do this:

CString str;
str.Format("%d", res);
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249