Scott Kraemer
2008-02-01 01:56:52 UTC
I am trying to load a dll from a secured directory across https. I can use this method and retrieve small text files etc. However it is not working on large binary files.
The part thats failing is the InternetReadFile, well I shouldnt say its failing. It returns as finished, however the pointer is just 3 characters while the returned size shows correct.
If I try a large 3k text file it returns ok! I can even browse the data via MSVC++ while debugging it.
I am using a third party DLL Inection method that needs a unsigned char *, and a size parameter. I have spent over 6 hours on this and my head is spinning.
If someone can help me out I will send $25 us paypal (paypal only)
I have tried most traditional ways to do this: (credits to unknown coder)
unsigned char* GetInternetFile(LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
unsigned char* buffer = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
std::wstring header = TEXT("Authorization: Basic MyTopSecretCode\r\n");
DWORD dwBuffSize = 0;
dwBuffSize = header.length();
if (!(hNet = InternetOpen(TEXT("IHG Downloader"), INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE)))
goto cleanup;
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE;
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, header.c_str(), dwBuffSize, options, 0)))
goto cleanup;
do
{
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
goto cleanup;
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
goto cleanup;
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
goto cleanup;
} while (cbBytesRead > 0);
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
goto cleanup;
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile)
InternetCloseHandle(hUrlFile);
if (hNet)
InternetCloseHandle(hNet);
if (!bResult)
free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return (bResult ? buffer : NULL);
}
void* ReallocOrFree(void* original_ptr, size_t new_size)
{
void* temp = realloc(original_ptr, new_size);
if (!temp) free(original_ptr);
return temp;
}
my email is kozmo _ 1970 @ hotmail DOT com
The part thats failing is the InternetReadFile, well I shouldnt say its failing. It returns as finished, however the pointer is just 3 characters while the returned size shows correct.
If I try a large 3k text file it returns ok! I can even browse the data via MSVC++ while debugging it.
I am using a third party DLL Inection method that needs a unsigned char *, and a size parameter. I have spent over 6 hours on this and my head is spinning.
If someone can help me out I will send $25 us paypal (paypal only)
I have tried most traditional ways to do this: (credits to unknown coder)
unsigned char* GetInternetFile(LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
unsigned char* buffer = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
std::wstring header = TEXT("Authorization: Basic MyTopSecretCode\r\n");
DWORD dwBuffSize = 0;
dwBuffSize = header.length();
if (!(hNet = InternetOpen(TEXT("IHG Downloader"), INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE)))
goto cleanup;
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE;
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, header.c_str(), dwBuffSize, options, 0)))
goto cleanup;
do
{
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
goto cleanup;
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
goto cleanup;
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
goto cleanup;
} while (cbBytesRead > 0);
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
goto cleanup;
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile)
InternetCloseHandle(hUrlFile);
if (hNet)
InternetCloseHandle(hNet);
if (!bResult)
free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return (bResult ? buffer : NULL);
}
void* ReallocOrFree(void* original_ptr, size_t new_size)
{
void* temp = realloc(original_ptr, new_size);
if (!temp) free(original_ptr);
return temp;
}
my email is kozmo _ 1970 @ hotmail DOT com