Discussion:
Wininet InternetSetOption and proxy auth
(too old to reply)
Federico Sasso
2004-08-13 07:30:30 UTC
Permalink
Hi all

Thist of all, sorry for X-posting, but the question involves both wininet
and C# interop ;-)



I wrote a simple FTP client in C# using p/Invoke over Wininet.dll API
functions. It works fine, as it transparently passes ISA proxy/firewall when
this doesn't require authentication.

Now I want to modify it to handle proxy authentication.

I read on msdn

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/setting_and_retrieving_internet_options.asp

("Setting individual options" section) a little snippet inviting to set the
proxy user and password with InternetSetOption() using values for
INTERNET_OPTION_PROXY_USERNAME and INTERNET_OPTION_PROXY_ PASSWORD

I tried accomplishing this with:



if (!InternetSetOption(

hInternet,

INTERNET_OPTION_PROXY_USERNAME,

Marshal.StringToHGlobalAuto(proxyUser),

proxyUser.Length))

throw new Win32Exception(Marshal.GetLastWin32Error());



where hInternet is the handle returned by InternetOpen(), and
InternetSetOption() is defined as:



[DllImport("wininet.dll", SetLastError = true)]

public static extern bool InternetSetOption

(

IntPtr hInternet,

int dwOption,

IntPtr lpBuffer,

int lpdwBufferLength

);



Unfortunately I always obtain the Win32Exception (Unknown error (0x2ef2),
which doesn't help a lot).



I got several doubts:



1) is the call Marshal.StringToHGlobalAuto(proxyUser) correct?

2) is using proxyUser.Length correct? or should I add a *2 factor to comply
with Unicode char size?

3) how can I specify a domain other than user and password?

4) in InternetOpen() I tried both INTERNET_OPEN_TYPE_DIRECT (which I used
previously) to INTERNET_OPEN_TYPE_PROXY, and specify a proxy name, but the
behavior didn't change. Which is right?

5) I tried writing the proxy name in several ways: "srv-isa:8080",
"http://srv-isa:8080", "ftp://srv-isa:8080", which is the right one?



Please enlighten me.

Fred


-

Federico Sasso &8v>
http://www.crt.unige.it/jsSQL
The jsSQL Project - JavaScript Database and SQL Engine
Mattias Sjögren
2004-08-13 23:27:02 UTC
Permalink
Federico,
Post by Federico Sasso
1) is the call Marshal.StringToHGlobalAuto(proxyUser) correct?
No, because you're currently calling the ANSI version of the API, but
StringToHGlobalAuto returns a Unicode string on NT platforms. Plus
you're leaking memory, since you're not keeping a pointer to the
memory and freeing it.

A better way would be to declare the function like this

[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int
dwOption, string lpBuffer, int dwBufferLength);

and just pass in the string directly.
Post by Federico Sasso
2) is using proxyUser.Length correct? or should I add a *2 factor to comply
with Unicode char size?
No, but in the sample code in the MSDN help topic you referred to,
they add one for the terminating null character, so perhaps you should
use proxyUser.Length + 1.



Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Stephen Sulzer
2004-08-15 02:14:47 UTC
Permalink
Post by Federico Sasso
Unfortunately I always obtain the Win32Exception (Unknown error (0x2ef2),
which doesn't help a lot).
In addition to getting the parameter marshalling right, note that the
PROXY_USERNAME and PROXY_PASSWORD options can only be set on the hConnect
and hRequest handles. You cannot set these options on the hInternet handle
created by InternetOpen. The 0x2efc (12018) Win32 error code is WinInet's
ERROR_INTERNET_INCORRECT_HANDLE_TYPE error, which means that the given
handle does not support the operation (or option). WinInet error codes are
documented on-line at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/wininet_errors.asp


Stephen
Federico Sasso
2004-08-16 07:55:12 UTC
Permalink
Thank you, Mattias and Stephen, for your kind replies, you helped me having
a better understanding of the problem.

Federico

Loading...