IEのProxy設定を取得する

Delphiメーリングリストにアップされた東京電力情報のプログラム プロクシ環境下で使えるようにしようかと思ってIEのProxy設定を取得する方法を調べてみた所、レジストリから取得する方法が見つかりました。

ただ、私の環境だとポリシーを使ってる為なのか良くわからんのですがHKCU\Software\Microsoft\Windows\CurrentVersion\Internet SettingsじゃなくてHKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settingsに設定が保存されていたりします。

それで、WindowsAPIを使った方法にしてみました。
APIと構造体の定義は下の通り。

type
  PWINHTTP_CURRENT_USER_IE_PROXY_CONFIG = ^TWINHTTP_CURRENT_USER_IE_PROXY_CONFIG;
  TWINHTTP_CURRENT_USER_IE_PROXY_CONFIG = record
    fAutoDetect: Boolean;
    lpszAutoConfigUrl: LPWSTR;
    lpszProxy: LPWSTR;
    lpszProxyBypass: LPWSTR;
  end;

  function WinHttpGetIEProxyConfigForCurrentUser(var pProxyConfig: TWINHTTP_CURRENT_USER_IE_PROXY_CONFIG): Boolean; stdcall; external 'Winhttp.dll';

FormCreateを下の通りに変更。

procedure TMainForm.FormCreate(Sender: TObject);
var
  ProxyConfig: TWINHTTP_CURRENT_USER_IE_PROXY_CONFIG;

  ClnPos: Integer;
begin
  ZeroMemory(@ProxyConfig, 0);

  WinHttpGetIEProxyConfigForCurrentUser(ProxyConfig);

  if Assigned(ProxyConfig.lpszProxy) then
  begin
    ClnPos := Pos(':', ProxyConfig.lpszProxy);
    if ClnPos > 0 then
    begin
      http.ProxyParams.ProxyServer := LeftStr(ProxyConfig.lpszProxy, ClnPos - 1);
      http.ProxyParams.ProxyPort := StrToIntDef(Trim(RightStr(ProxyConfig.lpszProxy, Length(ProxyConfig.lpszProxy) - ClnPos)), 0);
    end;
  end;

  if Assigned(ProxyConfig.lpszAutoConfigUrl) then
    GlobalFree(Cardinal(ProxyConfig.lpszAutoConfigUrl));
  if Assigned(ProxyConfig.lpszProxy) then
    GlobalFree(Cardinal(ProxyConfig.lpszProxy));
  if Assigned(ProxyConfig.lpszProxyBypass) then
    GlobalFree(Cardinal(ProxyConfig.lpszProxyBypass));

  Button1Click(nil);
end;

一応、上記の方法でProxy設定取得出来てるようなんですがレジストリから読み出す方法が主流になっているのはAPIで取得する方法には何か問題あるんですかね?