2017-11-12 4 views
0

私は現在、MacOSでC++プログラムを作成しています.HWIDとIPアドレスの2つの変数を取って、そう;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

のcURLライブラリがCライブラリであり、そのすべての機能がC関数です。したがって、彼らはstd::stringのようなオブジェクトを処理することはできません。 "website.com/c.php?ip=" + ip + "&hwid=" + hwidの場合、結果はであり、であり、std::stringオブジェクトです。

それを解決する1つの方法は、変数に"website.com/c.php?ip=" + ip + "&hwid=" + hwidの結果を保存し、Cスタイルの文字列を取得するためにc_str機能でその変数を使用することです:

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*を準備する必要があります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()); 
関連する問題