We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,521 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Get data from a Internet file into a char

I want to get the information downloaded from a file (a BMP image) into a char array. I have this function

#define _countof(x) (sizeof(x) / sizeof(x[0]))

char* DownloadBytes(char* szUrl) {
     HINTERNET hOpen = NULL;
     HINTERNET hFile = NULL;
     char* data = (char*)"";
     DWORD dataSize = 0;
     DWORD dwBytesRead = 0;

     hOpen = InternetOpen("MyAgent", NULL, NULL, NULL, NULL);
     if(!hOpen) return (char*)"";

          hFile = InternetOpenUrl(hOpen, szUrl, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, NULL);
          if(!hFile) {
          InternetCloseHandle(hOpen);
          return (char*)"";
     }

     do {
          char buffer[2000];
          InternetReadFile(hFile, (LPVOID)buffer, _countof(buffer), &dwBytesRead);
          char* tempData = new char[dataSize + dwBytesRead];
          memcpy(tempData, data, dataSize);
          memcpy(tempData + dataSize, buffer, dwBytesRead);
          delete[] data;
          data = tempData;
          dataSize += dwBytesRead;
     } while (dwBytesRead);

     InternetCloseHandle(hFile);
     InternetCloseHandle(hOpen);
     return data;
}

The problem is that I only get the first three bytes of the file and I really don't have any idea of what could be wrong. I am using CodeBlocs + MinGW, on Windows 7. This function is exported into a DLL, but I don't think it does matter. The only thing I can verify is that buffer gets the downloaded data correctly. Something goes wrong with memcpy()

1
Contributor
1
Reply
3 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
Question
Answered
alexbnc
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You have my project uploaded here. It is compiled and you have the Release executable if you want to test it. http://www.devimperium.info/DownloadFile.zip

Please, tell me if this works on your computers. The project type is CodeBlocks but I suppose that you can get those files into any IDE.

alexbnc
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Self-Answered as of 1 Year Ago

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0756 seconds using 2.72MB