Your Ad Here

Thursday, February 26, 2009

WinInet UNICODE

I�m trying to exchange unicode strings between a C++ application and a PHP script on my homepage, using WinInet.
MSDN tells me, WinInet should be capable of doing it all automatically, by aliasing all functions, but I can�t figure out how to get it to work.

Before I started with unicode, I had a fairly simple function working just fine


Code:
---------
std::string MyInetConnection(std::string MySendstring, std::string MyDomain, std::string MyUrl)
{

HINTERNET inet = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET connection;
HINTERNET request;
unsigned long flags;
unsigned long len;
connection = InternetConnect(inet, MyDomain.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
flags = INTERNET_FLAG_PRAGMA_NOCACHE;
request = HttpOpenRequest(connection, "POST", MyUrl.c_str(), NULL, NULL, NULL, flags, 1);
HttpSendRequest(request, "Content-Type: application/x-www-form-urlencoded", 47, (void*)MySendstring.c_str(), MySendstring.size());

std::stringstream CompleteBuffer;
do{
char answerbuffer[10000];
InternetReadFile(request, answerbuffer, sizeof answerbuffer, &len);
answerbuffer[len] = '\0';
CompleteBuffer << answerbuffer;
} while(len);

InternetCloseHandle(request);
InternetCloseHandle(connection);
InternetCloseHandle(inet);

return CompleteBuffer.str();

}
---------
Now, after I�ve done

#define UNICODE
#define _UNICODE

the function still works, I only have to put an L in front of some variables, like

request = HttpOpenRequest(connection, L"POST", MyUrl.c_str(), NULL, NULL, NULL, flags, 1);

but MySendstring as well as CompleteBuffer remains ANSI and I can�t figure out how to convert that to UNICODE.
Or better what do I have to do, so that HttpSendRequest() will take std::wstring for MySendstring and InternetReadFile() will accept wchar_t[10000] as buffer?

Read More...
Your Ad Here

No comments: