Mega Code Archive

 
Categories / Delphi / Forms
 

To read the proxy information

Title: to read the proxy information Friends, most tasks that works with internet, requires to read the current proxy settings from computer before connect to remote server. Today I want to show two way to read this information. 1. to use registry this is easy and fast way but note that if in future the Microsoft will change a key where proxy stored, you must change your code too:-) var Handle: HKey; Buffer: array[0..256] of Char; BufSize: Integer; DataType: Integer; begin if RegOpenKeyEx(HKEY_CURRENT_USER, 'SOFTWAREMicrosoftWindowsCurrentVersionInternet Settings', 0, KEY_READ, Handle) = ERROR_SUCCESS then begin BufSize := SizeOf(Buffer); DataType := reg_sz; if RegQueryValueEx(Handle, 'ProxyServer', nil, @DataType, @Buffer, @BufSize) = ERROR_SUCCESS then ProxyServer := Buffer; BufSize := SizeOf(Integer); if RegQueryValueEx(Handle, 'ProxyEnable', nil, @DataType, @i, @BufSize) = ERROR_SUCCESS then UseProxyServer := (i 0); RegCloseKey(Handle); end; end; 2. to use WinInet library this is more correct way to get an information but require IE 3.x (or higher) var ProxyInfo: PInternetProxyInfo; Len: LongWord; begin Len := 4096; GetMem(ProxyInfo, Len); try if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len) then if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then begin UseProxyServer := True; ProxyServer := ProxyInfo^.lpszProxy end finally FreeMem(ProxyInfo); end; end; Now if proxy used, we'll receive in ProxyServer variable similar string: "ftp=proxy.domain.com:8082;gopher=proxy.domain.com:8083;http=proxy.domain.com:8080;https=proxy.domain.com:8081" where domain.com is our proxy. So as you see, now we must parse this string for correct protocol (http/ftp etc) and to get the proxy server and port number. For example: if UseProxyServer and (ProxyServer '') then begin i := Pos('http=', ProxyServer); if (i 0) then begin Delete(ProxyServer, 1, i+4); j := Pos(';', ProxyServer); if (j 0) then ProxyServer := Copy(ProxyServer, 1, j-1); end; i := Pos(':', ProxyServer); if (i 0) then begin j := Pos(' ', ProxyServer); if(j=0) then begin j:=length(proxyserver)+1; end; ProxyPort := StrToInt(Copy(ProxyServer, i+1, j-i-1)); ProxyServer := Copy(ProxyServer, 1, i-1) end end;