Problem with InternetReadFile()

This is a discussion on Problem with InternetReadFile() within the Windows Programming forums, part of the Platform Specific Boards category; Here is the prototype... BOOL InternetReadFile( HINTERNET hFile, LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD lpdwNumberOfBytesRead ); Let me explain the problem ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    Problem with InternetReadFile()

    Here is the prototype...

    BOOL InternetReadFile(
    HINTERNET hFile,
    LPVOID lpBuffer,
    DWORD dwNumberOfBytesToRead,
    LPDWORD lpdwNumberOfBytesRead
    );

    Let me explain the problem I am having by showing you a little example:

    char *lpBuffer[2000];
    DWORD lpdwNumberOfBytesRead = 0;
    BOOL bRetval;
    HINTERNET hInternet, hFile;

    char url[] = "http://www.cnn.com"; // whatever -- not important

    hInternet = InternetOpen(" ",
    INTERNET_OPEN_TYPE_DIRECT,
    NULL, NULL, 0);
    if(!hInternet)
    MessageBox(hwnd, "Could not open connection", "InternetOpen", MB_OK | MB_ICONERROR);


    hFile = InternetOpenUrl(hInternet,
    url,
    NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NEED_FILE |
    INTERNET_FLAG_NO_COOKIES
    , 0);
    if(hFile != NULL)
    {
    for( ; ; )
    {
    bRetval = InternetReadFile( hFile, lpBuffer, 2000, &lpdwNumberOfBytesRead);
    if(bRetval && lpdwNumberOfBytesRead == 0)
    break;
    // I would include code here to null terminate lpBuffer
    }
    }
    Now, in this example, which works perfectly, the third parameter of InternetReadFile is set to equal the size of lpBuffer. But when I set the third parameter, or the number of bytes I want read per call to the InternetReadFile function, to less than the size of lpBuffer -- lets say to 1000 -- I get unwanted results. For instance, with the third parameter set to 1000 the first 1000 bytes of data is written to lpBuffer without a problem, but the remaining 1000 bytes of data, which is suppose to be written when InternetReadFile is called for the second time within the for loop, is not being written to lpBuffer at all. The end result: a file that is suppose to be 2000 bytes long but actually only contains 1000 bytes of data. This is the sort of problem I am having. Does anyone know why?

    thanks

    Echidna

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    You don't seem to be incrementing the pointer to the buffer, lpBuffer, so the new info is copied to the start each time?

    I would also use an array of chars, not an array of char pointers.
    Last edited by novacain; 05-21-2002 at 10:34 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Novacain wrote, "You don't seem to be incrementing the pointer to the buffer, lpBuffer, so the new info is copied to the start each time? "

    What you wrote gave me an idea...
    Instead of writing the data to a buffer, I started downloading the data directly to a file -- using the WriteFile function, of course. So far it works perfectly, thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21