Roland Bengtsson
Roland Bengtsson

Reputation: 5166

Get name of current network

I want to find the names of the active and connected networks on Windows with code in Delphi. There can be more than one.

Example of network

In this screenshot from Windows 10 shows one connected, NETGEAR21. Would be grateful if someone could answer this.

Upvotes: 0

Views: 1202

Answers (1)

Roland Bengtsson
Roland Bengtsson

Reputation: 5166

I got it working by first create an interface to Network list manager as described on https://theroadtodelphi.com/2015/10/28/using-the-network-list-manager-nlm-api-from-delphi/. This create the needed file NETWORKLIST_TLB.pas. Here is the minimal implementation:

unit ListTypes;

interface

uses
  ActiveX,
  NETWORKLIST_TLB,
  ComObj;

function SetUpAndGetConnections: String;
function GetConnections: string;

implementation

uses
  SysUtils,
  Windows;

function SetUpAndGetConnections: String;
begin
  CoInitialize(nil);
  try
    Result := GetConnections;
  finally
    CoUninitialize;
  end;
end;

function GetConnections: String;
var
  NetworkListManager: INetworkListManager;
  EnumNetworkConnections: IEnumNetworkConnections;
  NetworkConnection : INetworkConnection;
  pceltFetched: ULONG;
begin
   NetworkListManager := CoNetworkListManager.Create;
   EnumNetworkConnections :=  NetworkListManager.GetNetworkConnections();
   Result := '';
   while true do
   begin
     EnumNetworkConnections.Next(1, NetworkConnection, pceltFetched);
     if (pceltFetched>0)  then
        Result := Result + NetworkConnection.GetNetwork.GetName + #13#10
     else
       Break;
   end;
end;

end.

Upvotes: 2

Related Questions