코딩하는 나귀

unit ProxyUnit;
 
interface
 
uses
  Windows, Wininet;
 
const
  INTERNET_PER_CONN_FLAGS               = 1;
  INTERNET_PER_CONN_PROXY_SERVER        = 2;
  INTERNET_PER_CONN_PROXY_BYPASS        = 3;
  INTERNET_PER_CONN_AUTOCONFIG_URL      = 4;
  INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;
 
  PROXY_TYPE_DIRECT         = $00000001;
  PROXY_TYPE_PROXY          = $00000002;
 
  INTERNET_OPTION_PER_CONNECTION_OPTION   = 75;
 
type
   INTERNET_PER_CONN_OPTION = record
      dwOption: DWORD;
      Value: record
         case Integer of
            1: (dwValue: DWORD);
            2: (pszValue: PAnsiChar);
            3: (ftValue: TFileTime);
      end;
   end;
   LPINTERNET_PER_CONN_OPTION = ^INTERNET_PER_CONN_OPTION;
   INTERNET_PER_CONN_OPTION_LIST = record
      dwSize: DWORD;
      pszConnection: LPTSTR;
      dwOptionCount: DWORD;
      dwOptionError: DWORD;
      pOptions: LPINTERNET_PER_CONN_OPTION;
   end;
   LPINTERNET_PER_CONN_OPTION_LIST = ^INTERNET_PER_CONN_OPTION_LIST;

   procedure SetProxy(Con, ProxyServer: String);
   procedure DisableProxy(Con: String);

implementation 

procedure SetProxy(Con, ProxyServer: String);
var
   list: INTERNET_PER_CONN_OPTION_LIST;
   dwBufSize: DWORD;
   Options: array[1..2] of INTERNET_PER_CONN_OPTION;

begin
   dwBufSize := sizeof(list);
   list.dwSize := sizeof(list);
   list.pszConnection := pchar(Con);
   list.dwOptionCount := High(Options);
   Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
   Options[1].Value.dwValue := PROXY_TYPE_PROXY;
   Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
   Options[2].Value.pszValue := PansiChar(ProxyServer);
   list.pOptions := @Options;
   InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
   InternetSetOption(nil, INTERNET_OPTION_REFRESH, nil, 0);
end;

procedure DisableProxy(Con: String);
var
   list: INTERNET_PER_CONN_OPTION_LIST;
   dwBufSize: DWORD;
   Options: array[1..2] of INTERNET_PER_CONN_OPTION;

begin
   dwBufSize := sizeof(list);
   list.dwSize := sizeof(list);
   list.pszConnection := pchar(Con);
   list.dwOptionCount := High(Options); // the highest index of the array (in this case 3)
   Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
   Options[1].Value.dwValue := PROXY_TYPE_DIRECT;
   Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
   Options[2].Value.pszValue := PansiChar('');
   list.pOptions := @Options;
   InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
   InternetSetOption(nil, INTERNET_OPTION_REFRESH, nil, 0);
end;

end.
사용은 아래와 같이...

SetProxy('','localhost:8080');
DisableProxy('');