Recent

Author Topic: [Solved] How to find Network Connection Name (not Interface name)?  (Read 988 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
Hi All,
I am getting the Network Connections information with the code below.

from: https://stackoverflow.com/questions/18378630/get-information-about-the-installed-network-adapters
Code: Pascal  [Select][+][-]
  1. uses
  2.   jwawindows;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   pAdapterInfo: PIP_ADAPTER_INFO;
  7.   AdapterInfo: IP_ADAPTER_INFO;
  8.   BufLen: DWORD;
  9.   Status: DWORD;
  10.   strMAC: String;
  11.   i: Integer;
  12. begin
  13.   BufLen:= sizeof(AdapterInfo);
  14.   pAdapterInfo:= @AdapterInfo;
  15.   Status:= GetAdaptersInfo(nil, BufLen);
  16.   pAdapterInfo:= AllocMem(BufLen);
  17.   try
  18.     Status:= GetAdaptersInfo(pAdapterInfo, BufLen);
  19.     if (Status <> ERROR_SUCCESS) then
  20.     begin
  21.       case Status of
  22.         ERROR_NOT_SUPPORTED: Memo1.Lines.Add('GetAdaptersInfo is not supported by the operating ' +
  23.           'system running on the local computer.');
  24.         ERROR_NO_DATA: Memo1.Lines.Add('No network adapter on the local computer.');
  25.       else
  26.         Memo1.Lines.Add('GetAdaptersInfo failed with error #' + IntToStr(Status));
  27.       end;
  28.       Dispose(pAdapterInfo);
  29.       exit;
  30.     end;
  31.  
  32.     while (pAdapterInfo <> nil) do
  33.     begin
  34.       memo1.Lines.Add('----------------------------------------------');
  35.       memo1.Lines.Add('Description: ' + pAdapterInfo^.Description);
  36.       memo1.Lines.Add('Name: ' + pAdapterInfo^.AdapterName);
  37.       //strMAC := '';
  38.       //for i := 0 to pAdapterInfo^.AddressLength - 1 do
  39.       //   strMAC := strMAC + '-' + IntToHex(pAdapterInfo^.Address[i], 2);
  40.       //memo1.Lines.Add('MAC address: ' + Copy(strMAC, 2, Length(strMAC) - 1));
  41.       //memo1.Lines.Add('IP address: ' + pAdapterInfo^.IpAddressList.IpAddress.S);
  42.       //memo1.Lines.Add('IP subnet mask: ' + pAdapterInfo^.IpAddressList.IpMask.S);
  43.       memo1.Lines.Add('Gateway: ' + pAdapterInfo^.GatewayList.IpAddress.S);
  44.       //memo1.Lines.Add('DHCP enabled: ' + IntTOStr(pAdapterInfo^.DhcpEnabled));
  45.       //memo1.Lines.Add('DHCP: ' + pAdapterInfo^.DhcpServer.IpAddress.S);
  46.       //memo1.Lines.Add('Have WINS: ' + BoolToStr(pAdapterInfo^.HaveWins,True));
  47.       //memo1.Lines.Add('Primary WINS: ' + pAdapterInfo^.PrimaryWinsServer.IpAddress.S);
  48.       //memo1.Lines.Add('Secondary WINS: ' + pAdapterInfo^.SecondaryWinsServer.IpAddress.S);
  49.       pAdapterInfo:= pAdapterInfo^.Next;
  50.     end;
  51.   finally
  52.     Dispose(pAdapterInfo);
  53.   end;
  54. end;

But the connection name looks like CLSID.

Code: Pascal  [Select][+][-]
  1. Description: Remote NDIS based Internet Sharing Device
  2. Name: {3B8EBC10-065D-46FE-A27C-D13322904BD9}
  3. Gateway: 192.168.42.129

Is there a way to get the name of the network connection in plain readable text?
Or will I have to use a different method?
« Last Edit: May 09, 2022, 08:02:55 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to find Network Connection Name (not Interface name)?
« Reply #1 on: May 09, 2022, 01:14:15 pm »
Is there a way to get the name of the network connection in plain readable text?
Or will I have to use a different method?

The Description is the plain readable name of the adapter.

If you mean something like “LAN Connection 1” or so then it seems that you'll need to retrieve that differently (no, I don't know how).

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to find Network Connection Name (not Interface name)?
« Reply #2 on: May 09, 2022, 01:21:02 pm »
If you mean something like “LAN Connection 1” or so then it seems that you'll need to retrieve that differently (no, I don't know how).
Thank you so much for the reply PascalDragon. Yes this is exactly what i want
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: How to find Network Connection Name (not Interface name)?
« Reply #3 on: May 09, 2022, 03:30:22 pm »
Why not Query the WMI?
This one's "deprecated"
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter?redirectedfrom=MSDN

Or this one
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapterconfiguration

No idea which Property it is

EDIT: Found it.
It's the class "Win32_NetworkAdapter" (Remember: "Deprecated")
The Property "NetConnectionID" --> returns "Ethernet 4" for my card

EDIT2: Also found this: https://forum.lazarus.freepascal.org/index.php?topic=24490.0

EDIT 3: Have you thought about looking for that Class-ID in the Registry?
Path: Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\
Digging there i found my "Ethernet 4", too
Quote
But the connection name looks like CLSID.
    Description: Remote NDIS based Internet Sharing Device
    Name: {3B8EBC10-065D-46FE-A27C-D13322904BD9}
    Gateway: 192.168.42.129
« Last Edit: May 09, 2022, 04:27:46 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

bobby100

  • Full Member
  • ***
  • Posts: 161
    • Malzilla
Re: How to find Network Connection Name (not Interface name)?
« Reply #4 on: May 09, 2022, 04:25:48 pm »
Or you can take a look at my code here and modify it to your needs:
https://forum.lazarus.freepascal.org/index.php/topic,57807.0.html
https://gitlab.com/bobby100 - my Lazarus components and units
https://sourceforge.net/u/boban_spasic/profile/ - my open source apps

https://malzilla.org/ - remainder at my previous life as a web security expert

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to find Network Connection Name (not Interface name)?
« Reply #5 on: May 09, 2022, 08:02:45 pm »
First of all, I can't thank you enough Zvoni and bobby100 for taking your precious time to reply.

Why not Query the WMI?

I didn't know about WMI Usage. Thank you very much, I learned from you.

EDIT2: Also found this: https://forum.lazarus.freepascal.org/index.php?topic=24490.0

This is the solution I'm looking for. I take this opportunity to thank you at Jurassic Pork.
Or you can take a look at my code here and modify it to your needs:
https://forum.lazarus.freepascal.org/index.php/topic,57807.0.html
I tried your solution as well, but the solution in EDIT2 above seemed a little easier to me.
Thank you very much. Regards.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018