我试图从DLL(连接到程序)获取请求,但无论我尝试什么,一切都会挂起。
我尝试使用CURL和WinHttpClient。两者都做了同样的事情,但略有不同。
CURL使程序挂起很长时间,然后什么都没有显示(该代码没有被注释,并且包含来自其他Stack Overflow问题的代码。)
WinHttpClient立即返回任何内容。
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
//#include "WinHttpClient/WinHttpClient.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
using namespace std; // fix later
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
extern "C" __declspec(dllexport) void Quack();
__declspec(dllexport) void Quack() {
/*WinHttpClient client(L"http://www.google.com");
client.SetUserAgent(L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;...)");
client.SendHttpRequest();
wstring httpResponseHeader = client.GetResponseHeader();
wstring httpResponseContent = client.GetResponseContent();
std::string thingstr((const char*)&httpResponseContent[0], sizeof(wchar_t) / sizeof(char)*httpResponseContent.size());
LPCSTR thing = (LPCSTR)httpResponseContent.c_str();
MessageBoxA(NULL, thing, "Quack", MB_OK);*/
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
MessageBoxA(NULL, (LPCSTR)readBuffer.c_str(), "Quack", MB_OK);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Quack();
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
有没有人有解决方案?任何答案或建议都表示赞赏。