curl-library
Newbie Question: libcurl with C++
Date: Fri, 12 Oct 2001 18:58:01 +0900
Hi folks,
I am trying to use libcurl with C++ and can't bring it to work.
This is my try, which leads into an Segmentation fault while using Method C.
Any hints? Maybe somebody can send me a simple C++ Example. All Examples I
found till now were C examples.
The Segfault happens while executing _ curl_easy_perform(curl_handle); _
and only, if there is an internet connection established.
I believe I make a general Mistake with passing WriteMemoryCallback the way
I do it.
Thx in advance and greetz, Andre
Here is my code (based on a C example from the curl homepage):
-----
gethtml.h
-----
#ifndef H_GETHTML
#define H_GETHTML
#include <stdlib.h>
#include <malloc.h>
#include <iostream.h>
extern "C" {
#include <curl/curl.h>
}
class CGetHTML
{
public:
CGetHTML();
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data);
void get();
private:
struct MemoryStruct {
char *memory;
size_t size;
};
struct MemoryStruct chunk;
};
#endif
------
------
gethtml.cpp
------
#include "../../include/gethtml.h"
CGetHTML::CGetHTML()
{
chunk.memory=NULL;
chunk.size=0;
}
size_t CGetHTML::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
void *data){
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)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;
};
void CGetHTML::get() {
CURL * curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://settlers4.net/");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
&CGetHTML::WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_FILE,(void *)&chunk);
curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
};
-----
Received on 2001-10-12