在Delphi 7中,如何检索Internet连接名称列表?

时间:2011-03-02 13:08:53

标签: delphi proxy delphi-7

早些时候我问我怎样才能set Internet Proxy in Windows connection

但我的问题是,如果我想设置所有连接,那么必须是列表连接名称列表。我在网上搜索,但我找不到任何答案。

1 个答案:

答案 0 :(得分:2)

您正在寻找RasEnumEntries功能。 Delphi没有声明RAS API函数,但是如果你有JVCL则很容易。 TJvRas32组件具有PhoneBook TStrings属性,其中填充了可用连接。

以下是D2007测试代码(无错误检查):

const
  RAS_MaxEntryName = 256;
  RASBASE = 600;
  ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
  ERROR_INVALID_SIZE = RASBASE + 32;

type
  PRasEntryName = ^TRasEntryName;
  TRasEntryName = record
    dwSize: Longint;
    szEntryName: array [0..RAS_MaxEntryName] of Char;
  end;

function RasEnumEntriesA(reserved: PChar; lpszPhonebook: PChar;
          lpRasEntryName: PRasEntryName; var lpcb: DWORD;
          var lpcEntries: DWORD): DWORD; stdcall; external 'RASAPI32.DLL';

procedure GetRasEntries(List: TStrings);
var
  RasEntryNames: array of TRasEntryName;
  Err, Size, Entries: DWORD;
  i: Integer;
begin
  List.Clear;
  SetLength(RasEntryNames, 1);
  Size := SizeOf(TRasEntryName);
  RasEntryNames[0].dwSize := Size;
  Err := RasEnumEntriesA(nil, nil, @RasEntryNames[0], Size, Entries);
  if (Err = ERROR_BUFFER_TOO_SMALL) and (Entries > 0) then begin
    Assert(Size = SizeOf(TRasEntryName) * Entries);
    SetLength(RasEntryNames, Entries);
    Err := RasEnumEntriesA(nil, nil, @RasEntryNames[0], Size, Entries);
    if Err = 0 then
      for i := 0 to Length(RasEntryNames) do
        List.Add(RasEntryNames[i].szEntryName);
  end else
    List.Add(RasEntryNames[0].szEntryName);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  GetRasEntries(ListBox1.Items);
end;