Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

InternetQueryOption c#

285 views
Skip to first unread message

John Smith

unread,
Apr 2, 2003, 4:26:41 PM4/2/03
to
Hi,
i have problem implemented InternetQueryOption in c# (INTERNET_OPTION_PROXY
data)
have you functional example in c# for this ?

Thanks
Filip.


Mattias Sjögren

unread,
Apr 3, 2003, 1:16:23 AM4/3/03
to

> i have problem implemented InternetQueryOption in c# (INTERNET_OPTION_PROXY
>data)
>have you functional example in c# for this ?

Try this

enum InternetOpenType
{
Preconfig = 0,
Direct = 1,
Proxy = 3,
PreconfigWithNoAutoProxy = 4
}

struct INTERNET_PROXY_INFO
{
public InternetOpenType dwAccessType;
public IntPtr lpszProxy,
lpszProxyBypass;
}

class InternetQueryOptionTest
{
// For INTERNET_OPTION_PROXY
[DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern bool InternetQueryOption(
IntPtr hInternet,
uint dwOption,
out INTERNET_PROXY_INFO lpBuffer,
ref int lpdwBufferLength);

const uint INTERNET_OPTION_PROXY = 38;

static void Main()
{
INTERNET_PROXY_INFO ipi;
int cb = Marshal.SizeOf(typeof(INTERNET_PROXY_INFO));
if ( InternetQueryOption( IntPtr.Zero, INTERNET_OPTION_PROXY,
out ipi, ref cb ) ) {
Console.WriteLine( ipi.dwAccessType );
if ( ipi.lpszProxy != IntPtr.Zero ) {
Console.WriteLine( Marshal.PtrToStringAuto( ipi.lpszProxy ) );
Marshal.FreeHGlobal( ipi.lpszProxy );
}
if ( ipi.lpszProxyBypass != IntPtr.Zero ) {
Console.WriteLine( Marshal.PtrToStringAuto(
ipi.lpszProxyBypass ) );
Marshal.FreeHGlobal( ipi.lpszProxyBypass );
}
}
else
throw new Win32Exception();
}
}

Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

John Smith

unread,
Apr 3, 2003, 3:12:31 AM4/3/03
to
Hi, thansk for example, i tested but.
InternetQueryOption return False with this exception;

"The data area passed to a system call is too small"
Error number from GetLastError is 122 (ERROR_INSUFFICIENT_BUFFER)

return 248 to cb value;

solutions?

Thanks
John

"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:eBrZMia#CHA....@TK2MSFTNGP11.phx.gbl...

John Smith

unread,
Apr 3, 2003, 3:18:19 AM4/3/03
to
Hi, its functional, but some corrections....

First you get size of data (Calling InternetQueryOption for cb .value)
You must call InternetQueryOption twice with set cb.value (example 248);

Convert ProxyName to string you must use Marshal.PtrToStringAnsi(
ipi.lpszProxy) ;.....

VERY THANKS.!

John

"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:eBrZMia#CHA....@TK2MSFTNGP11.phx.gbl...
>

John Smith

unread,
Apr 3, 2003, 5:57:35 AM4/3/03
to
Next Problem....
in Console Application works all fine.
but if i transfer it to WinForms app, i get this exceptioin

System.NullReferenceException occured in system.windows.forms.dll
"Object reference not set to an instance of an object"


"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:eBrZMia#CHA....@TK2MSFTNGP11.phx.gbl...
>

Mattias Sjögren

unread,
Apr 4, 2003, 7:39:49 PM4/4/03
to
John,

>Hi, thansk for example, i tested but.
>InternetQueryOption return False with this exception;
>
>"The data area passed to a system call is too small"
>Error number from GetLastError is 122 (ERROR_INSUFFICIENT_BUFFER)

Shoot, must be a docs bug then. OK, try it like this instead

struct INTERNET_PROXY_INFO
{
public InternetOpenType dwAccessType;

public string lpszProxy,
lpszProxyBypass;
}

class InternetQueryOptionTest
{
// For INTERNET_OPTION_PROXY
[DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern bool InternetQueryOption(
IntPtr hInternet,
uint dwOption,

IntPtr lpBuffer,
ref int lpdwBufferLength);

const uint INTERNET_OPTION_PROXY = 38;

static void Main()
{
int cb = 0;
InternetQueryOption( IntPtr.Zero, INTERNET_OPTION_PROXY,
IntPtr.Zero, ref cb );
IntPtr buffer = IntPtr.Zero;
try {
buffer = Marshal.AllocHGlobal( cb );


if ( InternetQueryOption( IntPtr.Zero, INTERNET_OPTION_PROXY,

buffer, ref cb ) ) {
INTERNET_PROXY_INFO ipi = (INTERNET_PROXY_INFO)
Marshal.PtrToStructure(buffer, typeof(INTERNET_PROXY_INFO));
Console.WriteLine( ipi.dwAccessType );
Console.WriteLine( ipi.lpszProxy );
Console.WriteLine( ipi.lpszProxyBypass );
}
else
throw new Win32Exception();
}
finally {
if ( buffer != IntPtr.Zero ) Marshal.FreeHGlobal( buffer );
}
}

}


>Next Problem....
>in Console Application works all fine.
>but if i transfer it to WinForms app, i get this exceptioin
>
>System.NullReferenceException occured in system.windows.forms.dll
>"Object reference not set to an instance of an object"

Does that happen with this code as well? If so, on which line?

John Smith

unread,
Apr 5, 2003, 11:17:21 AM4/5/03
to
ROCKS!!!
Its fully functional in WinForms and Console.
VERY THANKS.

"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message

news:eA18Wvw#CHA....@TK2MSFTNGP12.phx.gbl...

0 new messages