InternetOpenUrl - urgent help needed
-
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 advancedInterface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)
-
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 advancedInterface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)
You can try
URLDownloadToFile
function.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
You can try
URLDownloadToFile
function.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)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 :)
-
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 advancedInterface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)
-
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 :)
-
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 :)
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 omittedvoid 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 theINTERNET_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 callingInternetReadFile()
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); // ...
-
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 omittedvoid 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 theINTERNET_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 callingInternetReadFile()
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); // ...
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 :)
-
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 :)
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 thenew
call. Alternatively you could allocate your buffer at the beginning of the program, either vianew
or as a static data area. This would save repeatednew
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. -
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 thenew
call. Alternatively you could allocate your buffer at the beginning of the program, either vianew
or as a static data area. This would save repeatednew
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.once again - thank you!
Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)
-
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 :)
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
-
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
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 :)