cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Newbie Question: libcurl with C++

From: Evan Martin <martine_at_cs.washington.edu>
Date: Fri, 12 Oct 2001 08:17:04 -0700

On Fri, Oct 12, 2001 at 01:49:32PM +0200, Daniel Stenberg wrote:
> The callback function must be a plain C function, not a C++ one. C++
> functions all get an object pointer passed to them as an invisible (first)
> argument, but C functions don't. So when libcurl calls the specified
> function, there is no invisible argument, but your code falsely expects one.
>
> Put your callback function within the extern "C" block, and things have a
> much better chance to work!

It may also work to just make the function "static" from within your
class.
Remember, though, that you no longer have an object pointer from within
your function!

The standard way to do this is this sort of thing:

class CGetHtml {
  //...
public:

  MemoryStruct chunk;
  
  static size_t WriteMemoryCallbackWrapper(void *ptr, size_t size,
                                           size_t nmemb, void *data)
  {
          CGetHtml *myself = (CGetHtml*)data;
        return myself->WriteMemoryCallback(ptr, size, nmemb);
  }

  // note no "data" argument to this function
  size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb) {
          // we can now do something with this object's "chunk" member.
  }
  
  // ...

  void CGetHTML::get() {
         // ...
           curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
         &CGetHTML::WriteMemoryCallbackWrapper);
         // ...
  }

};

-- 
      Evan Martin
martine_at_cs.washington.edu
  http://neugierig.org
Received on 2001-10-13