void fn(HINTERNET hfile,...){
char *url=new char[3000];
DWORD *len = new DWORD;
if(InternetQueryOption(hFile, INTERNET_OPTION_URL, url, len)==FALSE){
char* tmp=new char[3000];
sprintf(temp, "InternetQueryOption failed!\nError: %d\nRequired bytes: %d\n", GetLastError(),*len);
MessageBox(NULL, tmp, _T("myBHO !!!"), MB_OK | MB_ICONINFORMATION);
delete [] tmp;
}
//....
delete [] url;
delete len;
}
In this function I am using InternetQueryOption() with option INTERNET_OPTION_URL to find URL of handle hfile. Result would be returned in url[] and length in len.
But the function always fails and GetLastError() always returns 122 (ERROR_INSUFFICIENT_BUFFER).
According to this, maximum URL length possible in IE is 2083 while I am using buffer of length 3000. So how is this error possible? I even made url[] global, still the same error.
Now, according to IntenetQueryOption() documentation, if GetLastError() returns ERROR_INSUFFICIENT_BUFFER, then variable len points to the number of bytes required to hold the requested information (in this case the information is URL). And this value is always less than 100. So how can the URL not fit in ulr[]?
What could be the reason for this strange behaviour? Please help.
According to this, maximum URL length possible in IE is 2083 while I am using buffer of length 3000. So how is this error possible? I even made url[] global, still the same error.
Browse your code, you declare a buffer big enough but you do not communicate to InternetQueryOption
its size, that's why it always returns ERROR_INSUFFICIENT_BUFFER
.
And this value is always less than 100.
After you call that function, check its value BEFORE.
With this line:
DWORD *len = new DWORD;
You allocate a pointer to DWORD
but value in the pointed memory is uninitialized (may be 0, may be 338, may be 1234567, may crash your app). You may keep this code but you need to initialize memory to right value:
DWORD *len = new DWORD;
*len = sizeof(url);
IMO here you don't need any dynamic allocation and you may simply change it to:
DWORD len = sizeof(url);
Now you can pass its address to InternetQueryOption
, like this:
if (InternetQueryOption(hFile, INTERNET_OPTION_URL, url, &len) == FALSE) {
Don't forget to remove delete len;
.