Objectarx uses libcurl to request WebApi

Because developing CAD requires requesting data from the server, I set up a webapi user on the server to transfer data, so I installed libcurl to use the data in objectarx.
Open VS2012 x64 Native Tools Command PromptSupplementary address:

I will quote the relevant configuration pictures here, and the application in CAD is consistent with the conventional one.
Insert image description here

Insert image description here
Insert image description here
Insert image description here

All usage is consistent with the current case, but an error occurred here. I will record it here. In the following code, I ran the program and CAD reported an error memory leak. After locating the problem, I found that the problem was in this sentence.

CURLcode res = curl_easy_perform(curl);

I checked many articles but couldn't solve it. Later I found out that it was actually a data transfer error here, and my pointer transfer error caused the problem.
Here is the problem code snippet a>

inline BOOL webApi::DownloadFile(const char* Filepath)
{
    // ...

    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);

    fclose(fp);
    curl_easy_cleanup(curl);

    curl_global_cleanup();

    return TRUE;
}

You can see that in this code I passed in a pointer

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &webApi::curlWriteFunction);

The problem occurs here because what needs to be called here is the callback function pointer, but the instance function pointer is used in my code, resulting in a value transfer error, which leads to subsequent problems.
Modify here to:

webapi.h
static size_t downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer);

webapi.cpp
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;
}

Then modify the original code snippet:

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;

problem solved.

Guess you like

Origin blog.csdn.net/qq_41059339/article/details/134467458