Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. InternetOpenUrl - urgent help needed

InternetOpenUrl - urgent help needed

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    YaronNir
    wrote on last edited by
    #1

    i use InternetOpenUrl (as async) to receive a file from a url. this is the code i use : BOOL CMyCode::RequestFile( LPCTSTR lpszUrl ) { m_hInternet = InternetOpen(_T("MyCode"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC); if (!m_hInternet) { return FALSE; } InternetSetStatusCallback(m_hInternet, CMyCode::InternetStatusCallback); InternetOpenUrl(m_hInternet, lpszUrl, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD) this); return TRUE; } void __stdcall CMyCode::InternetStatusCallback (HINTERNET hSession, DWORD Context, DWORD Status, LPVOID pInformation, DWORD InfoLength) { CMyCode* pThis = (CMyCode*) Context; if (!pThis) { return; } switch (Status) { case INTERNET_STATUS_RESPONSE_RECEIVED: m_dwNumytes2Read = (*(DWORD *)pInformation); break; case INTERNET_STATUS_HANDLE_CREATED: m_hHttpSession = * (HINTERNET *) pInformation; // Copy break; case INTERNET_STATUS_REQUEST_COMPLETE: CompleteRequest(); break; case INTERNET_STATUS_HANDLE_CLOSING: break; default: ATLTRACE(_T("Status is %ld\n"), Status); break; } } void CMyCode::CompleteRequest() { LPBYTE pBuff = new BYTE[m_dwNumytes2Read]; ZeroMemory(pBuff, m_dwNumytes2Read); DWORD dwBytesRead(0); BOOL bOK(TRUE); while (bOK) { bOK = InternetReadFile (m_hHttpSession, pBuff, m_dwNumytes2Read, &dwBytesRead); if (!dwBytesRead) bOK = FALSE; // .. progress handling } if (pBuff) delete []pBuff; Abort(); //... done } the code works ok only i have 2 major problems: 1. the file is received in a chunks of 267/268 Bytes each time i call InternetReadFile???? 2. the CPU reaches 80% until the file is download completely can any1 help me here? thanks in advanced

    Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

    _ L 2 Replies Last reply
    0
    • Y YaronNir

      i use InternetOpenUrl (as async) to receive a file from a url. this is the code i use : BOOL CMyCode::RequestFile( LPCTSTR lpszUrl ) { m_hInternet = InternetOpen(_T("MyCode"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC); if (!m_hInternet) { return FALSE; } InternetSetStatusCallback(m_hInternet, CMyCode::InternetStatusCallback); InternetOpenUrl(m_hInternet, lpszUrl, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD) this); return TRUE; } void __stdcall CMyCode::InternetStatusCallback (HINTERNET hSession, DWORD Context, DWORD Status, LPVOID pInformation, DWORD InfoLength) { CMyCode* pThis = (CMyCode*) Context; if (!pThis) { return; } switch (Status) { case INTERNET_STATUS_RESPONSE_RECEIVED: m_dwNumytes2Read = (*(DWORD *)pInformation); break; case INTERNET_STATUS_HANDLE_CREATED: m_hHttpSession = * (HINTERNET *) pInformation; // Copy break; case INTERNET_STATUS_REQUEST_COMPLETE: CompleteRequest(); break; case INTERNET_STATUS_HANDLE_CLOSING: break; default: ATLTRACE(_T("Status is %ld\n"), Status); break; } } void CMyCode::CompleteRequest() { LPBYTE pBuff = new BYTE[m_dwNumytes2Read]; ZeroMemory(pBuff, m_dwNumytes2Read); DWORD dwBytesRead(0); BOOL bOK(TRUE); while (bOK) { bOK = InternetReadFile (m_hHttpSession, pBuff, m_dwNumytes2Read, &dwBytesRead); if (!dwBytesRead) bOK = FALSE; // .. progress handling } if (pBuff) delete []pBuff; Abort(); //... done } the code works ok only i have 2 major problems: 1. the file is received in a chunks of 267/268 Bytes each time i call InternetReadFile???? 2. the CPU reaches 80% until the file is download completely can any1 help me here? thanks in advanced

      Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You can try URLDownloadToFile function.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      Y 1 Reply Last reply
      0
      • _ _Superman_

        You can try URLDownloadToFile function.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        Y Offline
        Y Offline
        YaronNir
        wrote on last edited by
        #3

        thanks for the reply... i can not use other methods other than what i've posted in my code (for stupid reasons go figure) so i would still like any help regarding my issues thanks again

        Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

        B 1 Reply Last reply
        0
        • Y YaronNir

          i use InternetOpenUrl (as async) to receive a file from a url. this is the code i use : BOOL CMyCode::RequestFile( LPCTSTR lpszUrl ) { m_hInternet = InternetOpen(_T("MyCode"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC); if (!m_hInternet) { return FALSE; } InternetSetStatusCallback(m_hInternet, CMyCode::InternetStatusCallback); InternetOpenUrl(m_hInternet, lpszUrl, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD) this); return TRUE; } void __stdcall CMyCode::InternetStatusCallback (HINTERNET hSession, DWORD Context, DWORD Status, LPVOID pInformation, DWORD InfoLength) { CMyCode* pThis = (CMyCode*) Context; if (!pThis) { return; } switch (Status) { case INTERNET_STATUS_RESPONSE_RECEIVED: m_dwNumytes2Read = (*(DWORD *)pInformation); break; case INTERNET_STATUS_HANDLE_CREATED: m_hHttpSession = * (HINTERNET *) pInformation; // Copy break; case INTERNET_STATUS_REQUEST_COMPLETE: CompleteRequest(); break; case INTERNET_STATUS_HANDLE_CLOSING: break; default: ATLTRACE(_T("Status is %ld\n"), Status); break; } } void CMyCode::CompleteRequest() { LPBYTE pBuff = new BYTE[m_dwNumytes2Read]; ZeroMemory(pBuff, m_dwNumytes2Read); DWORD dwBytesRead(0); BOOL bOK(TRUE); while (bOK) { bOK = InternetReadFile (m_hHttpSession, pBuff, m_dwNumytes2Read, &dwBytesRead); if (!dwBytesRead) bOK = FALSE; // .. progress handling } if (pBuff) delete []pBuff; Abort(); //... done } the code works ok only i have 2 major problems: 1. the file is received in a chunks of 267/268 Bytes each time i call InternetReadFile???? 2. the CPU reaches 80% until the file is download completely can any1 help me here? thanks in advanced

          Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Have you checked the value of m_dwNumytes2Read bfore your call to InternetReadFile()?

          Y 1 Reply Last reply
          0
          • L Lost User

            Have you checked the value of m_dwNumytes2Read bfore your call to InternetReadFile()?

            Y Offline
            Y Offline
            YaronNir
            wrote on last edited by
            #5

            what do you mean? can you give an example

            Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

            L 1 Reply Last reply
            0
            • Y YaronNir

              what do you mean? can you give an example

              Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You have the following code (btw could you edit your original message and put the code between <pre></pre> tags):

              case INTERNET_STATUS_RESPONSE_RECEIVED:
              m_dwNumytes2Read = (*(DWORD *)pInformation);
              break;
              // intermediate lines omitted

              void CMyCode::CompleteRequest()
              {
              LPBYTE pBuff = new BYTE[m_dwNumytes2Read];
              ZeroMemory(pBuff, m_dwNumytes2Read);

              DWORD dwBytesRead(0);
              BOOL bOK(TRUE);
              while (bOK)
              {
              bOK = InternetReadFile (m\_hHttpSession, pBuff, m\_dwNumytes2Read, &dwBytesRead);
              // intermediate lines omitted
              

              }

              The value of m_dwNumytes2Read is set when you process the INTERNET_STATUS_RESPONSE_RECEIVED message and is relevant to the status information, so it is probably less than the amount of data that may be available. Try resetting it to some bigger value before calling InternetReadFile() for example:

              dwNumytes2Read = 2048;
              LPBYTE pBuff = new BYTE\[m\_dwNumytes2Read\];
              DWORD dwBytesRead;
              BOOL bOK(TRUE);
              while (bOK)
              {
              bOK = InternetReadFile (m\_hHttpSession, pBuff, m\_dwNumytes2Read, &dwBytesRead);
              // ...
              
              Y 1 Reply Last reply
              0
              • L Lost User

                You have the following code (btw could you edit your original message and put the code between <pre></pre> tags):

                case INTERNET_STATUS_RESPONSE_RECEIVED:
                m_dwNumytes2Read = (*(DWORD *)pInformation);
                break;
                // intermediate lines omitted

                void CMyCode::CompleteRequest()
                {
                LPBYTE pBuff = new BYTE[m_dwNumytes2Read];
                ZeroMemory(pBuff, m_dwNumytes2Read);

                DWORD dwBytesRead(0);
                BOOL bOK(TRUE);
                while (bOK)
                {
                bOK = InternetReadFile (m\_hHttpSession, pBuff, m\_dwNumytes2Read, &dwBytesRead);
                // intermediate lines omitted
                

                }

                The value of m_dwNumytes2Read is set when you process the INTERNET_STATUS_RESPONSE_RECEIVED message and is relevant to the status information, so it is probably less than the amount of data that may be available. Try resetting it to some bigger value before calling InternetReadFile() for example:

                dwNumytes2Read = 2048;
                LPBYTE pBuff = new BYTE\[m\_dwNumytes2Read\];
                DWORD dwBytesRead;
                BOOL bOK(TRUE);
                while (bOK)
                {
                bOK = InternetReadFile (m\_hHttpSession, pBuff, m\_dwNumytes2Read, &dwBytesRead);
                // ...
                
                Y Offline
                Y Offline
                YaronNir
                wrote on last edited by
                #7

                thanks for the reply - it did solve the problem now the chunck are 2048 bytes each time - good! the second problem still remains - high CPU about 80% - how can i solve this? thanks again for your kind reply

                Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

                L 1 Reply Last reply
                0
                • Y YaronNir

                  thanks for the reply - it did solve the problem now the chunck are 2048 bytes each time - good! the second problem still remains - high CPU about 80% - how can i solve this? thanks again for your kind reply

                  Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  First thing is to run some tests with different buffer sizes to see how that affects performance, 2048 was just a number plucked from the air. Looking at your code I notice that you use ZeroMemory on the input buffer just before reading data into it; this is wasteful and serves no purpose, just allocate it with the new call. Alternatively you could allocate your buffer at the beginning of the program, either via new or as a static data area. This would save repeated new delete[] pairs, and the consequent garbage collection code. There may be other issues in the rest of your code, but I leave that for you to investigate.

                  Y 1 Reply Last reply
                  0
                  • L Lost User

                    First thing is to run some tests with different buffer sizes to see how that affects performance, 2048 was just a number plucked from the air. Looking at your code I notice that you use ZeroMemory on the input buffer just before reading data into it; this is wasteful and serves no purpose, just allocate it with the new call. Alternatively you could allocate your buffer at the beginning of the program, either via new or as a static data area. This would save repeated new delete[] pairs, and the consequent garbage collection code. There may be other issues in the rest of your code, but I leave that for you to investigate.

                    Y Offline
                    Y Offline
                    YaronNir
                    wrote on last edited by
                    #9

                    once again - thank you!

                    Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

                    1 Reply Last reply
                    0
                    • Y YaronNir

                      thanks for the reply... i can not use other methods other than what i've posted in my code (for stupid reasons go figure) so i would still like any help regarding my issues thanks again

                      Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

                      B Offline
                      B Offline
                      Bram van Kampen
                      wrote on last edited by
                      #10

                      YaronNir wrote:

                      i can not use other methods other than what i've posted in my code (for stupid reasons go figure) so i would still like any help regarding my issues

                      Then You're at a dead end! We cannot help you any further!(for stupid reasons go figure). :) :)

                      Bram van Kampen

                      Y 1 Reply Last reply
                      0
                      • B Bram van Kampen

                        YaronNir wrote:

                        i can not use other methods other than what i've posted in my code (for stupid reasons go figure) so i would still like any help regarding my issues

                        Then You're at a dead end! We cannot help you any further!(for stupid reasons go figure). :) :)

                        Bram van Kampen

                        Y Offline
                        Y Offline
                        YaronNir
                        wrote on last edited by
                        #11

                        this sort of reply is in appropriate, if you can see below i got a really good reply who took my question seriously shame on you....

                        Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups