小弟新手学mfc,用多线程抓网页内容,在用curl的CURLOPT_WRITEFUNCTION 回调的时候发生问题了,
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
回调函数:
size_t opp::my_fwrite(void *buffer, size_t size, size_t nmemb, void *userp)因为是静态函数,我不知道怎么传一个flag进去,所以无法得知是哪个线程的,这时就会串数据,有什么办法能解决么?

解决方案 »

  1.   

    求void *userp此指针如何使用,因为没有能传入的地方比如AfxBeginThread(MyThread,p);这里还有一个p指针能传入,curl这个没地方传入啊
      

  2.   

    这个 CURLOPT_WRITEDATA
    文档里都有
      

  3.   

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &p);
    这个CURLOPT_WRITEDATA 的参数指针p就是回调里的void *userp指针咯??。。
      

  4.   

    CURLOPT_WRITEFUNCTIONFunction pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *stream); This function gets called by libcurl as soon as there is data received that needs to be saved. The size of the data pointed to by ptr is size multiplied with nmemb, it will not be zero terminated. Return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it'll signal an error to the library. This will abort the transfer and return CURLE_WRITE_ERROR.From 7.18.0, the function can return CURL_WRITEFUNC_PAUSE which then will cause writing to this connection to become paused. See curl_easy_pause(3) for further details.This function may be called with zero bytes data if the transferred file is empty.Set this option to NULL to get the internal default function. The internal default function will write the data to the FILE * given with CURLOPT_WRITEDATA.Set the stream argument with the CURLOPT_WRITEDATA option.

    The callback function will be passed as much data as possible in all invokes, but you cannot possibly make any assumptions. It may be one byte, it may be thousands. The maximum amount of data that can be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE.CURLOPT_WRITEDATAData pointer to pass to the file write function. If you use the CURLOPT_WRITEFUNCTION option, this is the pointer you'll get as input. If you don't use a callback, you must pass a 'FILE *' as libcurl will pass this to fwrite() when writing data.The internal CURLOPT_WRITEFUNCTION will write the data to the FILE * given with this option, or to stdout if this option hasn't been set.If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set this option or you will experience crashes.This option is also known with the older name CURLOPT_FILE, the name CURLOPT_WRITEDATA was introduced in 7.9.7.