首先用Delphi来实现.delphi实现很简单.因为delphi的单元WinInet中就封装了该API调用的所有文档所以就简单多了.首先做一级面向对象的封装.一个对象TsetProxy,然后定义两个数据分别是代理主机和代理端口.然后一个负责调用的函数.代码如下:
unit Unit1;
interface
uses
Registry,WinInet
type
TSetProxy=class
private
{ Private declarations }
procedure SetReg(FMProxy:string);
public
{ Public declarations }
Proxy: string;
port:string;
constructor Create(Proxy,Port:string);
procedure SetProxy();
// procedure SetProxy(const fProxy,fPort: string);
end;
implementation
//实际设置代理
procedure TSetProxy.SetProxy;
var FProxy: string;
Pinfo: INTERNET_PROXY_INFO;
begin
FProxy:=Format('%s:%s',[self.Proxy,self.port]);
Pinfo.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
Pinfo.lpszProxy := pchar(proxy);
InternetSetOption(nil, INTERNET_OPTION_PROXY, @Pinfo, SizeOf(PInfo));
InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);
self.SetReg(FProxy);
end;
//SetReg负责读写注册表
procedure TSetProxy.SetReg(FMProxy:string);
var
reg: Tregistry;
begin
reg:=Tregistry.Create;
try
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet Settings', True) then
begin
reg.Writestring('ProxyServer', FMproxy);
reg.WriteInteger('ProxyEnable', integer(true));
end
finally
reg.CloseKey;
reg.free;
end;
end;
constructor TSetProxy.Create(Proxy,Port:string);
begin
self.Proxy:=Proxy;
self.port:=Port;
end;
end.