我在使用cURL将图像转换为动态内存缓冲区时遇到了一些问题。
使用的代码如下:
struct memoryStruct {
char *memory;
size_t size;
};
static void* CURL_realloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct memoryStruct *mem = (struct memoryStruct *)data;
mem->memory = (char *)CURL_realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory)
{
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
int main()
{
std::string everything = "https://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg";
CURL *curl; // CURL objects
CURLcode res;
memoryStruct buffer; // memory buffer
curl = curl_easy_init(); // init CURL library object/structure
if(curl) {
// set up the write to memory buffer
// (buffer starts off empty)
buffer.memory = NULL;
buffer.size = 0;
// (N.B. check this URL still works in browser in case image has moved)
curl_easy_setopt(curl, CURLOPT_URL, everything);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // tell us what is happening
// tell libcurl where to write the image (to a dynamic memory buffer)
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *) &buffer);
// get the image from the specified URL
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
free(buffer.memory);
}
return 0;
}
但我不确定是否理解显示的错误。我的猜测是我应该为curl启用IDN支持?但我不确定如何继续。这是否意味着我必须在启用IDN的情况下重新编译库? (如果我发现该怎么做)
感谢您的帮助
答案 0 :(得分:1)
您将std::string
对象传递给curl_easy_setopt
,而您应传递C字符串。它实际上试图解决上帝知道什么。
curl_easy_setopt(curl, CURLOPT_URL, everything.c_str());