Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active January 3, 2021 20:34
Show Gist options
  • Save itn3000/b414da5337b7d229d812ec3ddcffb446 to your computer and use it in GitHub Desktop.
Save itn3000/b414da5337b7d229d812ec3ddcffb446 to your computer and use it in GitHub Desktop.
get winhttp proxy setting by powershell(using p/invoke)
# based on http://stackoverflow.com/questions/4265427/programmatically-set-proxy-address-port-username-password
$MethodDefinition = @'
using System.Runtime.InteropServices;
public enum AccessType
{
DefaultProxy = 0,
NamedProxy = 3,
NoProxy = 1
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WINHTTP_PROXY_INFO
{
public AccessType AccessType;
public string Proxy;
public string Bypass;
}
public class WinHttp
{
[DllImport("winhttp.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool WinHttpGetDefaultProxyConfiguration(ref WINHTTP_PROXY_INFO config);
}
'@
$asm = Add-Type -TypeDefinition $MethodDefinition -PassThru
$obj = New-Object WINHTTP_PROXY_INFO
$obj.AccessType = [AccessType]::DefaultProxy
$ret = [WinHttp]::WinHttpGetDefaultProxyConfiguration([ref]$obj)
$obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment