0

I need to set an automatic configuration script that will set the proxy.

When I do this through the LAN settings, all is ok and Chrome is aware of the change immediately. I tried to replicate this behavior by changing the registry key Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings -> AutoConfigURL but Chrome is aware of the change only several minutes after the change (not sure why).

My next attempt is to use Winapi, but I cannot find how to set this script. I found the read function WinHttpDetectAutoProxyConfigUrl that works well, but I cannot find a write equivalent.

How do I setup the autoproxy script with Winapi?

Boiethios
  • 38,438
  • 19
  • 134
  • 183

2 Answers2

0

I have a partial answer (only for the setup of the autoproxy). After the registry modification, InternetInitializeAutoProxyDll must be called.

Here is a sample code:

[DllImport("wininet.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int InternetInitializeAutoProxyDll(uint dwReserved);

// ...

registryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);

// Setup the registry value:
registryKey.SetValue("AutoConfigURL", "http://the/config/path");

// Tell Windows to initialize the proxy:
if (InternetInitializeAutoProxyDll(0) == 0)
{
    throw new Win32Exception(Marshal.GetLastWin32Error());
}

However, it does not work when the proxy is unset:

registryKey.DeleteValue(AutomaticProxyRegistryName);

// Does nothing:
if (InternetInitializeAutoProxyDll(0) == 0)
{
    throw new Win32Exception(Marshal.GetLastWin32Error());
}
Boiethios
  • 38,438
  • 19
  • 134
  • 183
0

Instead of fiddling directly with the registry I recommend to do everything through the WinINet API. See Programmatically Set Browser Proxy Settings in C# for info on how to do this using C#. Code mentioned in that question can also be found here.

EDIT

The example mentioned fiddles with the registry, too. I have uploaded parts of a program that show how to do everything using WinINet calls. Unfortunately the program is written in Delphi, so you might have to translate it to C#. What is important is that one always resets the proxy settings before setting new values.

Olaf Hess
  • 1,453
  • 11
  • 18