I’m having a bit of a problem with the following code. I’ve seen tons of examples for using InternetReadFile to save to a file. But I cant find one, or get it to work for a char[]. I want to add the szBuffer up to get holdBuff, and then set content equal to holdBuff.
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <windows.h>
#include <WinInet.h>
HINTERNET hSession;
void urlToChar(char* url, char** content);
int main()
{
hSession = InternetOpen("Mozilla/4.0 (compatible) Poison", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
char* content;
urlToChar("http://google.com/", &content);
printf("%s",content);
return 0;
}
void urlToChar(char* url, char** content)
{
HINTERNET hConnect = InternetConnect(hSession, _T(""),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = InternetOpenUrl(hSession, url, NULL, 0, 0, 0);
if (hRequest)
{
char holdBuff[] = "";
char szBuff[1025];
memset(szBuff, 0x00, sizeof(szBuff));
DWORD bytesRead;
while (InternetReadFile(hRequest, szBuff, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
// Cat szBuff to holdBuff
memset(szBuff, 0x00, sizeof(szBuff));
}
*content = holdBuff;
// memset(holdBuff, 0x00, sizeof(holdBuff)); <-- Need this?
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
}
The variable declaration
will tell the compiler to put the contents of the string on the stack. Of course, the stack goes away when your function returns.
So in your case:
This tells the compiler to create a string of length one (the NULL terminator) as a local variable. Just because you’ve set the value of
contenttoholdBuffdoesn’t mean that whatholdBuffwas pointing exists anymore.You have to correct two things. Firstly, you must use
strcpy()or similar function. Second, you must allocate sufficient space forholdBuff.Example:
You’ll then need to
free(content)inmain()once you’re finished with it.Now, for how to actually do the concatenation: Your performance will be much better if you forget about using
szBuffat all and just write directly toholdBuff.Now
holdBuffwill have the data you want with no need for intermediary concatenation.