// this function returns a list of Ethernet 802.3 adapters
// that have IP protocol enabled and IP address assigned.
// This covers most of network adapters that are in use these days.
// It may be used for local or remote host. When used for local host,
// all the parameters must be empty. For remote computer, parameters must be:
// TargetHost: name or IP address of the host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// Adapters: the out parameter that this function fills with captions
// of active adapters.
procedure ListActiveAdapters(TargetHost, UserName, Password: string; Adapters: TStrings);
var
vConfigs: TWmiQuery;
vAdapters: TWmiQuery;
vConnection: TWmiConnection;
begin
vAdapters := TWmiQuery.Create(nil);
vConfigs := TWmiQuery.Create(nil);
vConnection := TWmiConnection.Create(nil);
try
if TargetHost <> '' then
begin
vConnection.MachineName := TargetHost;
vConnection.Credentials.UserName := UserName;
vConnection.Credentials.Password := Password;
end;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
vConnection.Connected := true;
except
Exit;
end;
vAdapters.WQL.Text := 'select * from Win32_NetworkAdapter '+
'where AdapterType = ''Ethernet 802.3''';
vConfigs.WQL.Text := 'select * from Win32_NetworkAdapterConfiguration '+
'where IPEnabled = true';
vAdapters.Connection := vConnection;
vConfigs.Connection := vConnection;
vAdapters.Open;
vConfigs.Open;
while not vAdapters.EOF do
begin
if vConfigs.Locate('Caption', vAdapters.FieldByName('Caption').AsString, [])
and vConfigs.FieldByName('IPEnabled').AsBoolean
// for empty IP address, field IPAddress as string returns '{}'
and (Length(vConfigs.FieldByName('IPAddress').AsString) > 2) then
Adapters.Add(vConfigs.FieldByName('Caption').AsString);
vAdapters.Next;
end;
finally
vAdapters.Free;
vConfigs.Free;
vConnection.Free;
end;
end;
|