Discussion:
InternetQueryOption crashes my application
(too old to reply)
John
2005-03-04 20:51:50 UTC
Permalink
Hey,

I am experiencing difficulty in using InternetQueryOption() in one of my
applications.
Here is the code snippet:

INTERNET_PROXY_INFO* po;
DWORD size=0;
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, NULL, &size))
{
po = (INTERNET_PROXY_INFO *) malloc(size);
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, po, &size))
{

}
free(po);
}

If I put this code in ANY threads, when application exits, it will crash.
But I can use the same funciton in other applications without any problems.

I am not the original author for this application. And I am wondering what
kind of things could prevent wininet queryOption usage?

Someone has the similar experience and could share your solutions?

Thanks very much!

John
Stephen Sulzer
2005-03-05 08:57:53 UTC
Permalink
What is the callstack of the crash?

There may be a memory corruption bug in the application. Try running the
application with a tool like Windows Application Verifier (with PageHeap
enabled) to look for memory corruption problems.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnappcom/html/AppVerifier.asp
http://www.microsoft.com/windows/appcompatibility/appverifier.mspx

The code you show that calls InternetQueryOption looks Ok. But make sure
that the code does *not* free the po->lpszProxy and po->lpszProxyBypass
strings inside the INTERNET_PROXY_INFO structure; it only needs to free(po).
Also check that the application does not use the po->lpszProxy and
po->lpszProxyBypass strings after the call to free(po).

Hope that helps.

- Stephen
Cristian Amarie
2005-03-07 06:48:59 UTC
Permalink
Let's try another way.

First determine - as you did - the size using the call with NULL.
Then allocate a buffer of TCHARs (cbSize in/out parameter returns the length
in TCHARs),
but only if GetLastError returns ERROR_INSUFFICIENT_BUFFER.

INTERNET_PROXY_INFO *po = NULL;
DWORD cbSize = 0;
if( ! InternetQueryOption( NULL, INTERNET_OPTION_PROXY, NULL, &cbSize ) )
{
DWORD dwLastError = GetLastError ( );
if ( dwLastError == ERROR_INSUFFICIENT_BUFFER )
{
po = ( TCHAR * ) malloc ( cbSize * sizeof ( TCHAR ) );
if ( po != NULL )
{
memset (po, 0, cbSize * sizeof(TCHAR) );
if( InternetQueryOption ( NULL, INTERNET_OPTION_PROXY, NULL,
&cbSize ) )
{
// do something with po - but if you alter it directly or as
a side effect, then free may complain again
// for example if you pass po to a function, pass it as
const
//
// BOOL DisplayProxyInfo (const DWORD cbSize, const
INTERNET_PROXY_INFO * po);
}

free ( po );
}
}
}

I didn't tested this exact code, but I've written similar code a lot of
times and I think is ok.
Please post a short reply if the solution works correctly (other users as
well may want to see this).

Cristian Amarie
Post by John
Hey,
I am experiencing difficulty in using InternetQueryOption() in one of my
applications.
INTERNET_PROXY_INFO* po;
DWORD size=0;
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, NULL, &size))
{
po = (INTERNET_PROXY_INFO *) malloc(size);
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, po, &size))
{
}
free(po);
}
If I put this code in ANY threads, when application exits, it will crash.
But I can use the same funciton in other applications without any problems.
I am not the original author for this application. And I am wondering what
kind of things could prevent wininet queryOption usage?
Someone has the similar experience and could share your solutions?
Thanks very much!
John
Cristian Amarie
2005-03-08 07:32:00 UTC
Permalink
Sorry about this copy/paste error:
The sequence

memset (po, 0, cbSize * sizeof(TCHAR) );
if( InternetQueryOption ( NULL, INTERNET_OPTION_PROXY, NULL,
&cbSize ) )

should be

memset (po, 0, cbSize * sizeof(TCHAR) );
if( InternetQueryOption ( NULL, INTERNET_OPTION_PROXY, po,
&cbSize ) )

(po instead of NULL in the 3rd parameter of the second call of
InternetQueryOption).

Cristian Amarie
Post by Cristian Amarie
Let's try another way.
First determine - as you did - the size using the call with NULL.
Then allocate a buffer of TCHARs (cbSize in/out parameter returns the
length in TCHARs),
but only if GetLastError returns ERROR_INSUFFICIENT_BUFFER.
INTERNET_PROXY_INFO *po = NULL;
DWORD cbSize = 0;
if( ! InternetQueryOption( NULL, INTERNET_OPTION_PROXY, NULL, &cbSize ) )
{
DWORD dwLastError = GetLastError ( );
if ( dwLastError == ERROR_INSUFFICIENT_BUFFER )
{
po = ( TCHAR * ) malloc ( cbSize * sizeof ( TCHAR ) );
if ( po != NULL )
{
memset (po, 0, cbSize * sizeof(TCHAR) );
if( InternetQueryOption ( NULL, INTERNET_OPTION_PROXY, NULL,
&cbSize ) )
{
// do something with po - but if you alter it directly or
as a side effect, then free may complain again
// for example if you pass po to a function, pass it as
const
//
// BOOL DisplayProxyInfo (const DWORD cbSize, const
INTERNET_PROXY_INFO * po);
}
free ( po );
}
}
}
I didn't tested this exact code, but I've written similar code a lot of
times and I think is ok.
Please post a short reply if the solution works correctly (other users as
well may want to see this).
Cristian Amarie
Post by John
Hey,
I am experiencing difficulty in using InternetQueryOption() in one of my
applications.
INTERNET_PROXY_INFO* po;
DWORD size=0;
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, NULL, &size))
{
po = (INTERNET_PROXY_INFO *) malloc(size);
if (!InternetQueryOption(NULL,INTERNET_OPTION_PROXY, po, &size))
{
}
free(po);
}
If I put this code in ANY threads, when application exits, it will crash.
But I can use the same funciton in other applications without any problems.
I am not the original author for this application. And I am wondering what
kind of things could prevent wininet queryOption usage?
Someone has the similar experience and could share your solutions?
Thanks very much!
John
Loading...