unit e0_Proxy;
interface
procedure SaveBeginInetSetting;
function RestoreBeginInetSetting: Boolean;
function InetSessionToUserSetting(aEnProxy: Boolean;
aStrIP, aStrPort:
string): Boolean;
function InetSessionToRegistrySetting: Boolean;
implementation
uses
windows, WinInet, UrlMon, SysUtils;
var
spBeginProxyStruct: PInternetProxyInfo;
len_spBeginProxyStruct: DWORD;
procedure SaveBeginInetSetting;
begin
InternetQueryOption(
nil, INTERNET_OPTION_PROXY,
nil, len_spBeginProxyStruct);
spBeginProxyStruct := AllocMem(len_spBeginProxyStruct);
InternetQueryOption(
nil, INTERNET_OPTION_PROXY, spBeginProxyStruct, len_spBegin
ProxyStruct);
end;
function RestoreBeginInetSetting: Boolean;
var
dreserved: DWORD;
x: DWORD;
p: INTERNET_PROXY_INFO;
begin
Result := False;
p.dwAccessType := spBeginProxyStruct^.dwAccessType;
p.lpszProxyBypass := spBeginProxyStruct^.lpszProxyBypass;
p.lpszProxy := spBeginProxyStruct^.lpszProxy;
x := SizeOf(p);
dreserved := 0;
if UrlMkSetSessionOption(
{INTERNET_OPTION_REFRESH}
INTERNET_OPTION_PROXY, @p, x, dreserved) <> S_Ok
then
begin
Exit;
end;
Result := True;
end;
function InetSessionToUserSetting(aEnProxy: Boolean;
aStrIP, aStrPort:
string): Boolean;
var
p: INTERNET_PROXY_INFO;
x: Integer;
dreserved: DWORD;
begin
Result := False;
FillChar(p, SizeOf(p), 0);
if aEnProxy
then
begin
p.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
p.lpszProxy := PChar(aStrIP + '
:' + aStrPort);
end
else
begin
p.dwAccessType := INTERNET_OPEN_TYPE_DIRECT;
p.lpszProxy := '
';
end;
p.lpszProxyBypass := '
';
x := SizeOf(p);
dreserved := 0;
if UrlMkSetSessionOption(INTERNET_OPTION_PROXY, @p, x, dreserved) <> S_OK
then
begin
Exit;
end;
Result := True;
end;
function InetSessionToRegistrySetting: Boolean;
var
hk: HKEY;
s:
string;
BufSize: Integer;
posStr: Integer;
retType: DWORD;
isUsingProxy: DWORD;
begin
Result := False;
s := '
';
if RegOpenKeyEx(HKEY_CURRENT_USER,
'
SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings',
0,
KEY_ALL_ACCESS,
hk) = ERROR_SUCCESS
then
begin
BufSize := SizeOf(isUsingProxy);
isUsingProxy := 0;
retType := REG_DWORD;
RegQueryValueEx(hk,
'
ProxyEnable',
nil, @retType, @isUsingProxy, @bufSize);
if isUsingProxy > 0
then
begin
SetLength(s, MAX_PATH);
BufSize := Length(s);
FillChar(s[1], MAX_PATH, #0);
retType := REG_NONE;
RegQueryValueEx(hk,
'
ProxyServer',
nil, @retType, @s[1], @bufSize);
SetLength(s, bufsize);
if bufsize > 0
then
if s[Length(s)] = #0
then SetLength(s, Length(s) - 1);
if Length(s) > 0
then
begin
posStr := Pos('
http=', LowerCase(s));
if posStr > 0
then
s := Copy(s, posStr + 5, Length(s));
posStr := Pos('
;', s);
if posStr > 0
then
s := Copy(s, 1, posStr - 1);
Result := InetSessionToUserSetting(True, s, '
');
end;
end
else
Result := InetSessionToUserSetting(False, '
', '
');
RegCloseKey(hk);
end;
end;
end.