如何在 Objectarx 2016 中使用 Libcurl

How To use Libcurl With Objectarx 2016

提问人:Imkc 提问时间:11/17/2023 最后编辑:273KImkc 更新时间:11/17/2023 访问量:13

问:

在主类中。我可以使用 libcurl 获取 https info ,但是当我将代码转换为新类“webapi”并使用它时,cad set 错误。 我找到的点是:,当 curl 要提取这段代码时,cad 同时设置错误内存不足CURLcode res = curl_easy_perform(curl);webapi

这是我点击按钮使用此功能时的代码:(此功能是对的)

void CDockControlBarChildDlg::On_GetDateBtn()
{
    acutPrintf(TEXT("Thread ID : %d , %f\n", getpid(), 3));
    //std::string strUrl = "http://140.249.201.214:5353/api/Get";
    std::string strUrl = "https://localhost:7035/api/cad/GetFileDirectory";
    std::string strTmpStr;
    CURL *curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    std::string strRsp;
    if (res != CURLE_OK)
    {
        strRsp = "error";
    }
    else
    {
        strRsp = strTmpStr;
    }
    acutPrintf(TEXT("Thread ID : %d , %f\n", getpid(), 3));
    const std::string s = strRsp.c_str();
    const char* str;
    str = strdup(s.c_str());

    acutPrintf(TEXT("strRsp is |%s|\n"), CString(str));
    stringstream stream(str);
    char split = ';';
    vector<const string> result ;
    string fakeStr;
    while (getline(stream,fakeStr,split))
    {
        //MessageBox(TEXT("1"));
        result.push_back(fakeStr);
    }


    CArxDlg arxDlg;
    arxDlg.SetListValue(result);

    arxDlg.DoModal();



    acutPrintf(TEXT("strRsp is |%s|\n"), CString(str));

    
}

我使用 WebAPI 和 ClickButton 不正确

webapi.h
#pragma once

#include <sstream>
#include <curl/curl.h>
#include <vector>
#include "Until.h"
using namespace std;

#pragma comment(lib, "libcurl_a.lib")   //刚才编译的三个lib
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
 
#pragma comment(lib, "ws2_32.lib")      //系统lib,Curl与OpenSSL的lib中会用到,缺一不可
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "Crypt32.lib")

class webApi
{
public:
    webApi(void);
    ~webApi(void);
public:
    BOOL DownloadFile(const char* Filepath);
    int GetNewestVersion();
    BOOL GetMenuFileItems(const char* symbolName , vector<const string>& date);
    BOOL IsOlder(const char* fileName , const char* version);
    BOOL IsAdmin(const char* userName);

    BOOL SetDownloadUserInfo(const char* user , const char* time , const char* fileName);
    BOOL UploadFile(const char* fileName , const char* user , const char* time , const char* version);

    CURL* GetPoint();
private:
    size_t curlWriteFunction(void *ptr, size_t size, size_t nmemb, FILE *stream) ;
    size_t downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer);
    CURL* curl;
    //CURLM* curl_m;
    
};

inline size_t curlWriteFunction(void *ptr, size_t size, size_t nmemb, FILE *stream)  
{
    FILE* fp = (FILE * )stream;
    size_t return_size = fwrite(ptr, size, nmemb, fp);

    return return_size;  
}  


inline BOOL webApi::DownloadFile(const char* Filepath)
{
    string strFileName = Until::localSavePath;
    strFileName += Filepath;

    //char fileName[100] = strFileName.;
    
    //char url[200] = "https://localhost:7035/api/cad/DownLoadFile/downFile?fileName=test.docx";
    string combinePath ="https://localhost:7035/api/cad/DownLoadFile/downFile?fileName=";
    combinePath += Filepath;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    CURL *curl = curl_easy_init();
    if(curl)
    {
        FILE *fp = fopen(strFileName.c_str(), "wb");    // 打开文件,准备写入
        
        curl_easy_setopt(curl, CURLOPT_URL, combinePath.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &webApi::curlWriteFunction);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        CURLcode result = curl_easy_perform(curl);
        acutPrintf(L" %i",result);
        fclose(fp);                         // 关闭文件
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    
    return TRUE;
}

inline int webApi::GetNewestVersion()
{
    return 0;
}

inline size_t webApi::downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer)
{
    std::string* psResponse = (std::string*) writer;
    size_t size = sz * nmemb;
    psResponse->append((char*) buffer, size);
 
    return sz * nmemb;
}



inline BOOL webApi::IsOlder(const char* fileName, const char* version)
{
    return TRUE;
}

inline BOOL webApi::IsAdmin(const char* userName)
{
    return TRUE;
}

inline BOOL webApi::SetDownloadUserInfo(const char* user, const char* time, const char* fileName)
{
    return TRUE;
}

inline webApi::webApi(void)
{
    curl_global_init(CURL_GLOBAL_DEFAULT);
    //curl_m = curl_multi_init();
    //curl_multi_setopt(curl_m, CURLMOPT_MAXCONNECTS, 2);

    curl = curl_easy_init();
}


inline  BOOL webApi::UploadFile(const char* fileName, const char* user, const char* time, const char* version)
{
    return TRUE;
}

inline CURL* webApi::GetPoint()
{
    return curl;
}

inline webApi::~webApi(void)
{
    curl_easy_cleanup(curl);
    //curl_easy_cleanup(curl_m);
    curl_global_cleanup();

}
webapi.cpp
BOOL webApi::GetMenuFileItems(const char* symbolName, vector<const string>& date)
{
    int* running_handle = 0;

    
    std::string strUrl = "https://localhost:7035/api/cad/GetFileDirectory";
    std::string strTmpStr;
    acutPrintf(TEXT("Thread ID : %d , %i\n", getpid(), 3));
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &webApi::downloadCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);

    /*curl_multi_add_handle(curl_m, curl);
    curl_multi_perform(curl_m, running_handle);*/

    //MessageBox(NULL,L"1",L" ",0);
    CURLcode res = curl_easy_perform(curl);
    MessageBox(NULL, L"1", L" ", 0);
    //降curl导出
    acutPrintf(TEXT("Point ID : %p \n", curl));
    //curl_global_cleanup();
    std::string strRsp;
    /*if (res != CURLE_OK)
    {
        strRsp = "error";
        acutPrintf(TEXT("strRsp is |%s|\n"), strRsp);
        return FALSE;
    }
    else
    {
        strRsp = strTmpStr;
    }*/

    /*const std::string s = strRsp.c_str();
    const char* str;
    str = strdup(s.c_str());
    acutPrintf(TEXT("strRsp is |%s|\n"), CString(str));*/
    /*stringstream stream(str);
    char split = ';';
    string fakeStr;
    while (getline(stream,fakeStr,split))
    {
        MessageBox(NULL,L"1",L" ",0);
        date.push_back(fakeStr);
    }*/
    date.push_back("1");
    date.push_back("2");
    date.push_back("3");
    date.push_back("4");


    return TRUE;
}

按钮代码:

void CDockControlBarChildDlg::On_GetDateBtn()
{
    acutPrintf(TEXT("Thread ID : %d , %f\n", getpid(), 3));
    //std::string strUrl = "http://140.249.201.214:5353/api/Get";
    std::string strUrl = "https://localhost:7035/api/cad/GetFileDirectory";
    std::string strTmpStr;
    CURL *curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    std::string strRsp;
    if (res != CURLE_OK)
    {
        strRsp = "error";
    }
    else
    {
        strRsp = strTmpStr;
    }
    acutPrintf(TEXT("Thread ID : %d , %f\n", getpid(), 3));
    const std::string s = strRsp.c_str();
    const char* str;
    str = strdup(s.c_str());

    acutPrintf(TEXT("strRsp is |%s|\n"), CString(str));
    stringstream stream(str);
    char split = ';';
    vector<const string> result ;
    string fakeStr;
    while (getline(stream,fakeStr,split))
    {
        //MessageBox(TEXT("1"));
        result.push_back(fakeStr);
    }


    CArxDlg arxDlg;
    arxDlg.SetListValue(result);

    arxDlg.DoModal();



    acutPrintf(TEXT("strRsp is |%s|\n"), CString(str));

    
}
C++ libcurl cad objectarx

评论


答: 暂无答案