dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] Get data from a Internet file into a char

  1. #1
    Join Date
    Dec 2011
    Posts
    13

    [RESOLVED] 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()

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Your char buffer[2000] is a local array within a function. It goes out of scope when this function returns, so the returned pointer points to a garbage.
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    No, that din't fix it. I don't believe the problem lies in buffer[2000]. Something wrong is happening with memcpy(). After memcpy() I get the bad results, not before.

  4. #4
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Quote Originally Posted by alexbnc View Post
    No, that din't fix it. I don't believe the problem lies in buffer[2000].
    What din't fix it?
    We don't see any "fixing" code...

    And BTW, why do you still use plain old char arrays? What prevents you to use std::string instead?
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    Making buffer[2000] global didn't fix it.

    I am trying to download a BMP image. I'm not downloading text data. That's why I don't use std::string

    I uploaded my project here: http://www.devimperium.info/DownloadFile.zip

    If somebody can download and test it (it is a CBlocks project) I would be very graceful to get a response like "It works on my computer" or "It doesn't work".
    Don't ask me stuff that has nothing to do with my concrete problem.
    Thanks!
    Last edited by alexbnc; February 18th, 2012 at 11:54 AM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Quote Originally Posted by alexbnc View Post
    I am trying to download a BMP image. I'm not downloading text data.
    Please, if you can't help, don't ask stuff that has nothing to do with my problem.
    I don't care what you are "trying to download".
    You wrote about a problem with the char* returned by your function, and U tried to explain that this function was wrong implemented.
    If you don't agree - no problem, just ignore my answers to your questions.
    Victor Nijegorodov

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Quote Originally Posted by alexbnc View Post
    Making buffer[2000] global didn't fix it.

    I am trying to download a BMP image. I'm not downloading text data. That's why I don't use std::string

    I uploaded my project here: http://www.devimperium.info/DownloadFile.zip

    If somebody can download and test it (it is a CBlocks project) I would be very graceful to get a response like "It works on my computer" or "It doesn't work".
    Don't ask me stuff that has nothing to do with my concrete problem.
    Thanks!
    Well, you have completely changed your post!
    Using char* to download a BMP image is not better than using std::string.
    BMP image is just an array of BYTEs. So what is wrong to use std::vector in that case?
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2011
    Posts
    13

    Smile Re: Get data from a Internet file into a char

    O.K. Sorry. I didn't mean to offend. It's just that I am working on this for two days and I don't see the light at the end of the tunnel. And maybe the fact that I don't speak English well creates misunderstanding.

    Let me explain this the best I can

    I want to create a function that downloads a file (a BMP image but not only) from the Internet (using WININET) and return their bytes to another function, that will convert them into a HBITMAP, which will be used to display the image on a form.

    So, I have modified another function that was downloading the images into a local file (download and save them into a file on my computer)

    My function is declared as char* DownloadBytes(char* url) because it has to return the bytes that it downloads from the Internet (the image) as a char array (as I know, is the only type that can store bytes - string only stores texts, not bytes).
    Anyway, my problem is not using a wold way of storing data. My problem is found in another place, which is the function memcpy() that is supposed to store each buffer of downloaded information into a char* variable (as I said, only char* can store bytes, without treating them as text or number - just bytes) then return the char* variable (the bytes) to whatever calls the function (in my case, another function that will convert those bytes into a image)

    This is the best I can explain it. I hope I made it clear and sorry if I may have sound rude.
    Thanks for asking, anyway. You're the only person that ever asked to my posts on this forum.

  10. #10
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    Quote Originally Posted by VictorN View Post
    Well, you have completely changed your post!
    Using char* to download a BMP image is not better than using std::string.
    BMP image is just an array of BYTEs. So what is wrong to use std::vector in that case?
    You mean create a vector (of what) and store each byte? Yes, that could be.
    What I fear about "string" is that it cannot handle binary data but only strings of ASCII text. It is not intended for bytes, so it could change my information when the binary information is converted into text. Maybe I could use vector<char*>, even if, I suppose, it would need more space than a simple char arraay.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Quote Originally Posted by alexbnc View Post
    You mean create a vector (of what) and store each byte? Yes, that could be.
    The vector of BYTEs (aka unsigned char)
    Victor Nijegorodov

  12. #12
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    Quote Originally Posted by VictorN View Post
    The vector of BYTEs (aka unsigned char)
    So, my function should look like this

    Code:
    #include <vector>
    
    vector<char*> DownloadBytes(char* szUrl) {
    	HINTERNET hOpen = NULL;
    	HINTERNET hFile = NULL;
    	vector<char*> data;
    	DWORD dataSize = 0;
    	DWORD dwBytesRead = 0;
    
    	hOpen = InternetOpen("MyAgent", NULL, NULL, NULL, NULL);
    	if(!hOpen) return data;
    
    	hFile = InternetOpenUrl(hOpen, szUrl, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, NULL);
    	if(!hFile) {
    		InternetCloseHandle(hOpen);
    		return data;
    	}
    	do {
    		char buffer[2000];
    		InternetReadFile(hFile, (LPVOID)buffer, strlen(buffer), &dwBytesRead);
    		data.push_back(buffer);
    	} while (dwBytesRead);
    	InternetCloseHandle(hFile);
    	InternetCloseHandle(hOpen);
    	return data;
    }

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,593

    Re: Get data from a Internet file into a char

    Quote Originally Posted by alexbnc View Post
    So, my function should look like this

    Code:
    #include <vector>
    
    vector<char*> DownloadBytes(char* szUrl) 
    {
    ..
    }
    Well it looks like a little better...
    However:
    as I already pointed out there should be BYTE (or unsigned char), not a char
    it should be vector<unsigned char> rather than vector<char*>
    Victor Nijegorodov

  14. #14
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    Quote Originally Posted by VictorN View Post
    Well it looks like a little better...
    However:
    as I already pointed out there should be BYTE (or unsigned char), not a char
    it should be vector<unsigned char> rather than vector<char*>
    Yes. Now is vector<char>

    Now, the problem is that I have to display the returned result into a EDIT box. It must be a char array. Now I have to convert the returned vector into a char array.

  15. #15
    Join Date
    Dec 2011
    Posts
    13

    Re: Get data from a Internet file into a char

    O.K. My problem was solved. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)