Reputation:
I already searched in multiples sites for a routine in Delphi that retrieves the current network connection type (wifi, Ethernet, Bluetooth) but I not have found.
I'm searching for something like this in C# language:
public string TipoConexao()
{
string conexao = String.Empty;
UdpClient u = new UdpClient(System.Net.Dns.GetHostName(), 1);
IPAddress localAddr = (u.Client.LocalEndPoint as IPEndPoint).Address;
NetworkInterface[] netIntrfc = NetworkInterface.GetAllNetworkInterfaces();
for (int i = 0; i < netIntrfc.Length - 1; i++)
{
if (netIntrfc[i].OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProps = netIntrfc[i].GetIPProperties();
foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
{
if (uni.Address.ToString() == localAddr.ToString())
{
conexao = netIntrfc[i].Name.ToString();
}
}
}
}
return conexao;
}
but in Delphi code.
So, someone here have a routine similar to routine above, in Delphi code?
Upvotes: 1
Views: 2495
Reputation: 136391
Check these two options.
To determine the type of the network adapter you can use the GetIfTable2
function which return a MIB_IF_TABLE2
structure. This record contains an array of MIB_IF_ROW2
structures from here check the value of the PhysicalMediumType
member.
Use the MSNdis_PhysicalMediumType
WMI class located in the root\WMI
namespace, the property NdisPhysicalMediumType
contains the type of the adapter (9 = Wifi, 10 = Bluetooth) , the meaning of these values are the same of the PhysicalMediumType
of the MIB_IF_ROW2
structure.
uses
SysUtils,
ActiveX,
ComObj,
Variants;
// NDIS Physical Medium Type
procedure GetMSNdis_PhysicalMediumTypeInfo;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\WMI', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM MSNdis_PhysicalMediumType','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('InstanceName %s',[String(FWbemObject.InstanceName)]));// String
Writeln(Format('NdisPhysicalMediumType %d',[Integer(FWbemObject.NdisPhysicalMediumType)]));// Uint32
Writeln('');
FWbemObject:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
GetMSNdis_PhysicalMediumTypeInfo;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
Note : for both solutions the minimum OS version supported is Windows Vista.
This is a full sample which return the connection type of the connected adapters. The NETWORKLIST_TLB unit is generated by the Delphi IDE when you import the Network List 1.0 Type Library
.
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils,
ActiveX,
ComObj,
Windows,
NETWORKLIST_TLB in 'NETWORKLIST_TLB.pas';
function GetAdapterDescription(const Guid : string) : string;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
Result:='';
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT Description FROM Win32_NetworkAdapter Where GUID=%s', [QuotedStr(Guid)]), 'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = 0 then
Result:= String(FWbemObject.Description);
end;
function GetMSNdis_PhysicalMediumType(const Adapter : string) : Uint32;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
Result:= 0;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\WMI', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT NdisPhysicalMediumType FROM MSNdis_PhysicalMediumType Where InstanceName = %s', [QuotedStr(Adapter)]), 'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = 0 then
Result := FWbemObject.NdisPhysicalMediumType;
end;
procedure GetNetworkInfo;
var
NetworkListManager: INetworkListManager;
EnumNetworksConnections: IEnumNetworkConnections;
Network: INetwork;
NetworkConnection : INetworkConnection;
fetched: ULONG;
AdapterGUID, AdapterDesc : string;
PhysicalMediumType : Uint32;
begin
NetworkListManager := CoNetworkListManager.Create;
EnumNetworksConnections := NetworkListManager.GetNetworkConnections;
while true do
begin
EnumNetworksConnections.Next(1, NetworkConnection, fetched);
if (fetched>0) and (NetworkConnection.IsConnectedToInternet) then
begin
Network := NetworkConnection.GetNetwork();
WriteLn('Connection Name ' + Network.GetName);
AdapterGUID := GUIDToString(NetworkConnection.GetAdapterId);
AdapterDesc := GetAdapterDescription(AdapterGUID);
Writeln('Adapter GUID ' + AdapterGUID);
Writeln('Adapter ' + AdapterDesc);
PhysicalMediumType := GetMSNdis_PhysicalMediumType(AdapterDesc);
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa814491(v=vs.85).aspx
case PhysicalMediumType of
9 : Writeln('Connection Type Wifi');
10 : Writeln('Connection Type Bluetooth');
else
Writeln('Connection Type '+IntToStr(PhysicalMediumType));
end;
end
else
Break;
end;
end;
begin
try
CoInitialize(nil);
try
GetNetworkInfo;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
Upvotes: 3