0 users online | 0 Guests and 0 Registered

»

ID #1001

Den Namen der Netzwerkverbindung ermitteln (W2K)

Hier wird gezeigt, wie über Windows API-Befehle und der Registry unter Windows 2000 der Name der Netzwerkverbindung ausgelesen wird.

uses
  Registry;

const
  MAX_HOSTNAME_LEN                = 128;
  MAX_DOMAIN_NAME_LEN             = 128;
  MAX_SCOPE_ID_LEN                = 256;
  MAX_ADAPTER_NAME                = 128;
  MAX_ADAPTER_DESCRIPTION_LENGTH  = 128;
  MAX_ADAPTER_NAME_LENGTH         = 256;
  MAX_ADAPTER_ADDRESS_LENGTH      = 8;

type
  PIP_ADAPTER_INFO = ^IP_ADAPTER_INFO;
  IP_ADAPTER_INFO = record
      Next                : PIP_ADAPTER_INFO;
      ComboIndex          : DWORD;
      AdapterName         : array [1..MAX_ADAPTER_NAME_LENGTH+4] of Char ;
  end;

function GetAdaptersInfo(const pAdapterInfo : PIP_ADAPTER_INFO;const pOutBufLen : PULONG) : DWORD; stdcall;
     external 'IPHLPAPI.DLL' name 'GetAdaptersInfo';

resourcestring
  w2knetcard = 'SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}';

function GetConnectionNameList : TStringList;
var
  pAdapterList : PIP_ADAPTER_INFO;
  dwLenAdapter : integer;
  reg          : TRegistry;
  I            : Integer;
  AdapterName  : string;
begin
  result := TStringList.Create;
  result.Clear;
  pAdapterList := nil;
  dwLenAdapter := 0;
  if GetAdaptersInfo(pAdapterList,@dwLenAdapter) <> ERROR_BUFFER_OVERFLOW then exit;
  pAdapterList := AllocMem(dwLenAdapter);
  if GetAdaptersInfo(pAdapterList,@dwLenAdapter) <> ERROR_SUCCESS then exit;
  repeat
    AdapterName := '';
    for I := 0 to Length(pAdapterList.AdapterName) - 1 do
      if pAdapterList.AdapterName[i] <> '' then
        AdapterName := AdapterName + pAdapterList.AdapterName[i];
    reg := TRegistry.Create();
    reg.RootKey := HKEY_LOCAL_MACHINE;
    try
      if reg.OpenKeyReadOnly(w2knetcard + '\' + AdapterName + '\Connection') then
        result.Add(reg.ReadString('Name'));
    finally
      reg.CloseKey;
      reg.Free;
    end;
    pAdapterList := pAdapterList.Next;
  until pAdapterList = nil;
end;

Aufgerufen wird die Funktion so :

Memo1.Lines := GetConnectionNameList;

Das ganze funktioniert von Windows 2000 bis Windows VISTA. Ab Windows XP kann GetAdaptersAddresses verwendet werden. Diese Funktion ist nicht in der IPHLPAPI.DLL von Windows 2000 vorhanden, deswegen der Umweg über die Registry.

 

 

Tags: -

Related entries: -

Last update: 2009-12-10 07:41
Author: Rolf Warnecke
Revision: 1.1

{writePrintMsgTag} {writeSend2FriendMsgTag} {writePDFTag}
Please rate this FAQ:

Average rating: 0 (0 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Stefan:
Memo1.Lines := GetConnectionNameList;
Und schon hat man ein Memoryleak produziert da die Zuweisung an Memo1.Lines Assign verwendet und eben nicht den Zeiger kopiert.
Added at: 2009-10-10 17:20

Most popular FAQs RSS

  1. Display Resolution change
    (30 views)
  2. Scanned a Picture
    (13 views)

Latest FAQs RSS

  1. Scanned a Picture
    (2010-11-19 06:01)
  2. Display Resolution change
    (2010-02-09 18:01)