2017-11-12 3 views
0

현재 MacOS에서 C++ 프로그램을 작성 중입니다. HWID와 IP 주소 인 두 개의 변수를 가져와 같은 get 요청과 같이 사용해야합니다. 그래서;C++에서 사전 정의 된 변수를 사용하여 cURL GET 요청하기

CURL* curl; 
string result; 

curl = curl_easy_init(); 
curl_easy_setopt(curl, CURLOPT_URL, "website.com/c.php?ip=" + ip + "&hwid=" + hwid); 

이 정의하는 방법 hwidip이다;

auto hwid = al.exec("ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3; }'"); 

auto ip = al.exec("dig +short myip.opendns.com @resolver1.opendns.com."); 

al.exec는 터미널 명령의 출력을 실행하고 반환하는 함수입니다.

그러나이 모든 일을하는 데있어 문제는 curl_easy_setopt에 params에 잘못된 유형을 지정하는 것입니다. 예와 같이 GET 요청을 할 때 이러한 오류가 발생합니다.

Cannot pass object of non-trivial type 'basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >' through variadic function; call will abort at runtime 

모든 도움을 주시면 감사하겠습니다.

답변

1

컬 라이브러리는 모든 기능 C 함수된다 C 라이브러리이다. 따라서 그들은 std::string과 같은 객체를 처리 할 수 ​​없습니다. 그리고 "website.com/c.php?ip=" + ip + "&hwid=" + hwid을 수행하면 결과는 이고std::string 개체입니다.

std::string url = "website.com/c.php?ip=" + ip + "&hwid=" + hwid; 
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
+0

감사합니다. 많은 도움을주었습니다. – siroot

1

당신은 const char*에 대한 준비를해야합니다 그것을 해결하기

한 가지 방법은 C 스타일의 문자열을 얻을 수있는 변수에 "website.com/c.php?ip=" + ip + "&hwid=" + hwid의 결과를 저장 한 다음 c_str 기능이 변수를 사용하는 것입니다 전화 curl_easy_setopt() :

std::ostringstream oss; 
oss << "website.com/c.php?ip=" << ip << "&hwid=" << hwid; 
std::string url = oss.str(); 
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());