callback of curl

来源:互联网 发布:golang defer 函数 编辑:程序博客网 时间:2024/05/16 18:48

curl的回调分为2种, C方式的回调, C++方式的回调.


写了一个测试程序, 测试了curl当前版本 7.36.0 的c++方式回调, 验证了该静态库编译进PE的size, Release版只增加了25xKb, 还好吧.

curl-7.36.0-testprj.rar


今天同事在用的时候,发现wtl工程中使用Curl编译不过,最后他发现,主工程和Curl库的代码产生方式要保持一致. e.g. 都是 /MTD

src_curl-7.36.0-wtl-testprj.rar


C方式的回调

// cUrlTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"/// @attention/// project Property pages >> c/c++ >> Preprocessor >> Preprocessor Definitions/// add CURL_STATICLIB#include <windows.h>#include <atlbase.h>#include <sys/stat.h>#include "curl/curl.h"#pragma comment(lib, "libcurl.lib")#pragma comment(lib, "Ws2_32.lib")#pragma comment(lib, "Wldap32.lib")// file download example/// 没有任何权限限制的url.// #define URL_DL L"http://www.xx.com/dir1/file1.zip"size_t WriteFileFromDownLoad(char *str, size_t size, size_t nmemb, void *stream){    return fwrite(str, size, nmemb, (FILE*)stream);}int _tmain(int argc, _TCHAR* argv[]){    USES_CONVERSION;    CURL *curl;    CURLcode res;    struct stat file_info;    double speed_download, total_time;    FILE * fp = NULL;    fp = fopen("d:\\temp\\dlTemp.txt","wb");    if (NULL == fp)        return S_FALSE; /* can't continue */    res = curl_global_init(CURL_GLOBAL_WIN32);    if(res != CURLE_OK)        return S_FALSE;    curl = curl_easy_init();    if (curl)    {        /* upload to this place */        curl_easy_setopt(curl, CURLOPT_URL, W2A(URL_DL));        // default action is download        // if action is upload, set below        // curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);        /* enable verbose for easier tracing */        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);        /// C方式的回调        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileFromDownLoad);        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);        res = curl_easy_perform(curl);        fclose(fp);        /* Check for errors */        if(res != CURLE_OK)        {            _tprintf(L"download error\r\n");        }        else        {            /* now extract transfer info */            curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed_download);            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);            _tprintf(L"Speed: %.3f bytes/sec during %.3f seconds\n",                speed_download, total_time);        }        /* always cleanup */        curl_easy_cleanup(curl);    }return 0;}


C plus plus 方式的回调

// cUrlTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"/// @attention/// project Property pages >> c/c++ >> Preprocessor >> Preprocessor Definitions/// add CURL_STATICLIB#include <windows.h>#include <atlbase.h>#include <sys/stat.h>#include "curl/curl.h"#pragma comment(lib, "libcurl.lib")#pragma comment(lib, "Ws2_32.lib")#pragma comment(lib, "Wldap32.lib")// file download example/// 没有任何权限限制的url.#define URL_DL L"http://www.xx.com/dir1/file1.zip"  ///< 远程文件#define FILE_LOCAL_TO_SAVE "d:\\temp\\dlTemp.txt"   ///< 保存到本地的文件class CDlProcess{private:    FILE * m_fp;public:    CDlProcess()    {        m_fp = NULL;    }    virtual ~CDlProcess()    {        CloseFile();    }    BOOL OpenFileToWrite(char * pcFileLocalPathName)    {        if (NULL == pcFileLocalPathName)            return FALSE;        m_fp = ::fopen(pcFileLocalPathName, "wb");    }    void CloseFile()    {        if (NULL != m_fp)        {            fclose(m_fp);            m_fp = NULL;        }    }    size_t WriteFileFromDownLoad(char * data, size_t size, size_t nmemb, void * stream)    {        return fwrite(data, size, nmemb, (FILE*)stream);    }    static size_t CallBackProc_WriteFileFromDownLoad(char * data, size_t size, size_t nmemb, void * p)    {        CDlProcess * pDlProcess = NULL;        if (NULL == p)            return 0;        pDlProcess = (CDlProcess *)p;        return pDlProcess->WriteFileFromDownLoad(data, size, nmemb, pDlProcess->m_fp);    }};int _tmain(int argc, _TCHAR* argv[]){    USES_CONVERSION;    CDlProcess dlProcess;    CURL *curl = NULL;    CURLcode res;    struct stat file_info;    double speed_download, total_time;    res = curl_global_init(CURL_GLOBAL_WIN32);    if(res != CURLE_OK)        return S_FALSE;    curl = curl_easy_init();    if (curl)    {        /* upload to this place */        curl_easy_setopt(curl, CURLOPT_URL, W2A(URL_DL));        // default action is download        // if action is upload, set below        // curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);        /* enable verbose for easier tracing */        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);        /// C++方式的回调        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlProcess);        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CDlProcess::CallBackProc_WriteFileFromDownLoad);                if (dlProcess.OpenFileToWrite(FILE_LOCAL_TO_SAVE))        {            res = curl_easy_perform(curl);            dlProcess.CloseFile();            /* Check for errors */            if(res != CURLE_OK)            {                _tprintf(L"download error\r\n");            }            else            {                /* now extract transfer info */                curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed_download);                curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);                _tprintf(L"Speed: %.3f bytes/sec during %.3f seconds\n",                    speed_download, total_time);            }        }        /* always cleanup */        curl_easy_cleanup(curl);    }return 0;}






0 0