Delphi 获取当前网络的名称
我想用Delphi中的代码查找Windows上活动和连接的网络的名称。可以有多个 在这个来自Windows 10的屏幕截图中,显示了一个已连接的NETGEAR 21。Delphi 获取当前网络的名称,delphi,network-programming,Delphi,Network Programming,我想用Delphi中的代码查找Windows上活动和连接的网络的名称。可以有多个 在这个来自Windows 10的屏幕截图中,显示了一个已连接的NETGEAR 21。 如果有人能回答这个问题,我将不胜感激。我首先创建了网络列表管理器的接口,如中所述。这将创建所需的文件NETWORKLIST_TLB.pas。 以下是最低限度的实现: unit ListTypes; interface uses ActiveX, NETWORKLIST_TLB, ComObj; function
如果有人能回答这个问题,我将不胜感激。我首先创建了网络列表管理器的接口,如中所述。这将创建所需的文件NETWORKLIST_TLB.pas。 以下是最低限度的实现:
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.
我首先创建了一个到网络列表管理器的接口,如中所述。这将创建所需的文件NETWORKLIST_TLB.pas。 以下是最低限度的实现:
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.
可能不止一个。你想要哪一个。好的,我在问题中澄清。我想要一份清单。通常只有一个网络。但是你是对的,可能有很多。你尝试了什么?@roland你做了什么研究?我让它工作了:)我稍后会在这里发布代码,当我有更多的时间时,可能会有不止一个。你想要哪一个。好的,我在问题中澄清。我想要一份清单。通常只有一个网络。但是你是对的,可能有很多。你尝试了什么?@roland你做了什么研究?我让它工作了:)等我有更多时间的时候,我会在这里发布代码