C++でcurlライブラリを使用してファイルをアップロードするときにクラッシュする問題があります。私はこの場所から正確なデモコードを使用しています:https://curl.haxx.se/libcurl/c/fileupload.htmlCURLでCURLにファイルをアップロードするときにcurl_easy_perform()がクラッシュする
私がコードで変更する唯一の事はアップロード場所、ウィンドウ上のローカルワンプサーバーにアップロードすること、アップロードするファイルですその開封OKを確認した。
私はDLL
を通じてのVisual Studio 2014、および建物CURLを通してを実行しているプログラムからの出力は次のようになります。
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> PUT /replayupload.php HTTP/1.1
Host: 127.0.0.1
Accept: */*
Content-Length: 43
Expect: 100-continue
< HTTP/1.1 100 Continue
*その後、私はプログラムの行66でクラッシュを取得します。
res = curl_easy_perform(curl);
は無効なパラメータで問題を引き起こしています。私はcurl変数がnullではないことを確認しましたが、それ以上のデバッグ情報を得ることは非常に難しく、呼び出しスタックはDLL内のメモリアドレスを参照するだけです。
私はデモを実行してポスト変数をアップロードしてページを取得できますが、クラッシュすることなく正常に動作します。クラッシュはファイルをアップロードするときにのみ発生します。
私の正確なコードは次のとおりです。ライン
/* set where to read from (on Windows you need to use READFUNCTION too) */
をスポッティングためTkauslへ
int main(void)
{
CURL *curl;
CURLcode res;
struct stat file_info;
double speed_upload, total_time;
FILE *fd;
fd = fopen("E:\\testfiles\\test.txt", "rb"); /* open file to upload */
if (!fd)
return 1; /* can't continue */
/* to get the file size */
if (fstat(_fileno(fd), &file_info) != 0)
return 1; /* can't continue */
curl = curl_easy_init();
if (curl) {
/* upload to this place */
curl_easy_setopt(curl, CURLOPT_URL,
"http://127.0.0.1/testupload.php");
/* tell it to "upload" to the URL */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* set where to read from (on Windows you need to use READFUNCTION too) */
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
/* enable verbose for easier tracing */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/* now extract transfer info */
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
speed_upload, total_time);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(fd);
return 0;
}
'/ *どこから読み込むのか(WindowsではREADFUNCTIONも使う必要があります)* /' – tkausl
ああ、それを見逃しやすいです。私は行を追加しました \t curl_easy_setopt(curl、CURLOPT_READFUNCTION、&fread); すべてがうまくいくようです – user2783377