问题描述:

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

In this screenshot from Windows 10 shows one connected, NETGEAR21.

Would be grateful if someone could answer this.

网友答案:

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.
相关阅读:
Top