Async InternetReadFileEx() problem

Posted on 2005-04-21
Last Modified: 2013-11-20
  I need to do async communications using HttpSendRequest() and InternetReadFile(). HttpSendRequest() returns ERROR_IO_PENDING and I wait for INTERNET_STATUS_REQUEST_COMPLETE event in OnStatusCallback(), then I call InternetReadFileEx() which returns incomplete data (usually a few hundreds bites short). I would apreciate any help.

DWORD CAsyncSession::SendRequestAsync( CString& strHeaders,
                                       LPVOID lpOptional,
                                       DWORD dwOptionalLen )
    HINTERNET hHttpFile = m_HttpFile->GetFileHandle();
    if ( ::HttpSendRequest( hHttpFile, strHeaders, strHeaders.GetLength(), lpOptional, dwOptionalLen ) )
        return 0;
    DWORD dwError = ::GetLastError();
    if ( ERROR_IO_PENDING != dwError )
          AfxThrowInternetException( m_HttpFile->GetContext(), dwError);

    return dwError;

DWORD CAsyncSession::ReadAsync( LPVOID lpBuf, UINT uiCount, DWORD* pdwError )
    HINTERNET hHttpFile = m_HttpFile->GetFileHandle();

    DWORD dwBytes = 0, dwError = 0;

    ::SetLastError( 0 );

    FillMemory( &InetBuff, sizeof(InetBuff), 0 );
    InetBuff.dwStructSize = sizeof(InetBuff);
    InetBuff.lpvBuffer = lpBuf;
    InetBuff.dwBufferLength = uiCount - 1;
    ::InternetReadFileEx( hHttpFile, &InetBuff, 0, m_HttpFile->GetContext() );
        dwError = ::GetLastError();
        dwBytes = InetBuff.dwBufferLength;
    if ( 0 != dwError )
        if ( pdwError )
            *pdwError = dwError;
        if ( ERROR_IO_PENDING != dwError )
            AfxThrowInternetException( m_HttpFile->GetContext(), dwError);
    return dwBytes;
Question by:gelbert
    LVL 2

    Expert Comment

    Is the InternetReadFileEx returning 0 for dwError when it is complete or is it erroring?  

    Is the buffersize, uiCount - 1, big enough to hold all the data?
    LVL 2

    Accepted Solution

    You can check the amount of data sent from the server by using InetBuff.dwBufferTotal.  This is the total amount of data the server sent.  
    LVL 8

    Author Comment

    I am sorry that I did not replied in time. InetBuff.dwBufferTotal returns exact number of data sent including garbage, buffer size is big enough and InternetReadFileEx() returned success. Issue is still unresolved, but I believe that server returns invalid XML.
    Thank you for your help and I will assign you points

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Get filename and folder into excel 7 62
    Folder Comparison 12 50
    deburging in oracle form 12 68
    fizzArray2 challenge 1 41
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now