Getting network interface name

Just starting out? Need help? Post your questions and find answers here.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Getting network interface name

Post by Dreamland Fantasy »

Hi there,

I am using the Windows netsh command (using PureBasic as a front-end) to reconfigure a PC's IP address under Windows and I am looking for a way to obtain what the network interface name is. At present I have the network name set as "Local Area Connection", but this will not work with all systems as some have different names (e.g. "Ethernet" or "Local Area Connection 2").

Any suggestions how I can achieve this?

Thanks in advance!

Kind regards,

Francis
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Getting network interface name

Post by netmaestro »

You could look at this code and see if there's anything of use to you, you'd want to focus on connections that are active as it will show all possible connections, most of which have a status of 'disconnected'. They kept improving the code from the first post, the code my link points to is a later one with most of the bugs out:

http://purebasic.fr/english/viewtopic.p ... 2&start=45
BERESHEIT
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

Thanks netmaestro. I did have a look at that before, and it does have a lot of really useful stuff that might come in handy later on, but I couldn't see how to obtain the interface name I need from it.

Kind regards,

Francis
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

If I run the command netsh interface show interface at the command prompt it provides me with the following information (which will be different depending on the computer):

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VirtualBox Host-Only Network
Enabled        Connected      Dedicated        Ethernet
If I can't get a more elegant solution I might just do this and extract the interface name information that way.

Kind regards,

Francis
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

A very crude implementation, but it gives me the results I'm looking for:

Code: Select all

TempFile$ = GetTemporaryDirectory() + "interface_names.tmp"

ImportC "msvcrt.lib"
  system(cmd.p-ascii)
EndImport

system("netsh interface show interface > " + TempFile$)

NewList InterfaceName$()
hndFile = ReadFile(#PB_Any, TempFile$)

For i = 1 To 3
  dummy$ = ReadString(hndFile)  ; We are not interested in this part
Next

Repeat
  FileSeek(hndFile, 47, #PB_Relative)
  Name$ = ReadString(hndFile)
  If Name$
    AddElement(InterfaceName$())
    InterfaceName$() = Name$
  EndIf
Until Eof(hndFile)

ForEach InterfaceName$()
  Debug InterfaceName$()
Next

CloseFile(hndFile)
DeleteFile(TempFile$)
Any better way of getting this information would be gratefully appreciated. :)

Kind regards,

Francis
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Getting network interface name

Post by netmaestro »

I played around with this a bit, here's what I came up with. It returns the first connected interface name it finds:

Code: Select all

Procedure.s GetNetworkInterfaceName()
  *buffer = AllocateMemory(2048)
  *loc = *buffer
  prog = RunProgram("Cmd","","",#PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Hide)
  If prog
    WriteProgramStringN(prog, "netsh interface show interface")
    While ProgramRunning(prog)
      s = AvailableProgramOutput(prog)
      If s
        ReadProgramData(prog, *loc, s)
        result.s = PeekS(*loc, s, #PB_Ascii)
        If FindString(result, "Connected")
          FreeMemory(*buffer)
          result=Right(result, Len(result)-47)
          Break
        Else
          *loc + s
        EndIf
      EndIf
    Wend
  EndIf
  CloseProgram(prog)
  ProcedureReturn result
EndProcedure

Debug GetNetworkInterfaceName()
BERESHEIT
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

Thanks netmaestro, that is a better implementation than my effort. I never even thought to look at the process library stuff, saves me creating temporary files! :D

Getting the connected interface won't always work with what I am trying to do. Basically I need to be able to directly connect a computer to another device using a network cable and configuring the IP address to a static address compatible with the device. Until the static address is correctly set up, it won't show as connected. Also, in the case of my home computer, I have two connected interfaces ("Ethernet" and a VirtualBox one), but I might want to connect to the device via the "Ethernet 2" connection.

Kind regards,

Francis
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

After some more digging, it appears that the correct way of getting the names of the network interfaces is by reading the Windows registry, specifically the key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}.

I've had a quick look, but I'm not clear on how to go about doing this.

Kind regards,

Francis
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Getting network interface name

Post by electrochrisso »

The names of the adapters are in the Descriptions folder of that key, on this particular netbook they are Atheros for both wireless and Ethernet. If you search the forum under registry you can find some codes that might point you in the right direction to extract the names.
PureBasic! Purely one of the best 8)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Getting network interface name

Post by netmaestro »

Here's what you can do:

You've identified the registry key containing all the guids for the interfaces but only some of them are relevant. Here is the code you need to identify the relevant ones:

Code: Select all

; Get Network Interface names step 1
; Lloyd Gallant (netmaestro) Dec. 2016

Structure IP_ADAPTER_INDEX_MAP 
  Index.l
  Name.c[#MAX_ADAPTER_NAME]
EndStructure

Structure IP_INTERFACE_INFO
  NumAdapters.l
  Adapter.IP_ADAPTER_INDEX_MAP[0]
  padding.IP_ADAPTER_INDEX_MAP
EndStructure

*ipii.IP_INTERFACE_INFO = AllocateMemory(SizeOf(IP_INTERFACE_INFO) + 10*SizeOf(IP_ADAPTER_INDEX_MAP))
size.l = MemorySize(*ipii)
GetInterfaceInfo_(*ipii, @size)
Debug "Number of adapters: "+Str(*ipii\NumAdapters)
For i=0 To *ipii\NumAdapters-1
  Debug Right(PeekS(@*ipii\Adapter[i]\name[0]), 38)
Next
Then open the registry folder "Connection" under the folder for each guid listed and the name is the second-last member in that folder. Grab some registry functions off the forum and use them to read these keys and you're good to go. Lots of registry code on these boards, put it together with what I've posted here and you have a complete solution.
BERESHEIT
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

Thanks guys.

I eventually figured out how to do it using ts-soft's registry module here: http://www.purebasic.fr/english/viewtop ... 12&t=56204

I used the following code:

Code: Select all

EnableExplicit

UseModule Registry

Define count, i
Define BaseKey$ = "SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
Define Key$

count = CountSubKeys(#HKEY_LOCAL_MACHINE, BaseKey$)

For i = 0 To count - 1
  Key$ = ListSubKey(#HKEY_LOCAL_MACHINE, BaseKey$, i)
  If Left(Key$, 1) = "{"
    If Left(ReadValue(#HKEY_LOCAL_MACHINE, BaseKey$ + "\" + Key$ + "\Connection", "PnPInstanceId"), 4) = "PCI\"
      Debug ReadValue(#HKEY_LOCAL_MACHINE, BaseKey$ + "\" + Key$ + "\Connection", "Name")
    EndIf
  EndIf
Next
Kind regards,

Francis
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

My code edited with netmaestro's code:

Code: Select all

EnableExplicit

UseModule Registry

Structure IP_ADAPTER_INDEX_MAP 
  Index.l
  Name.c[#MAX_ADAPTER_NAME]
EndStructure

Structure IP_INTERFACE_INFO
  NumAdapters.l
  Adapter.IP_ADAPTER_INDEX_MAP[0]
  padding.IP_ADAPTER_INDEX_MAP
EndStructure

Define BaseKey$ = "SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
Define *ipii.IP_INTERFACE_INFO = AllocateMemory(SizeOf(IP_INTERFACE_INFO) + 10*SizeOf(IP_ADAPTER_INDEX_MAP))
Define i, size.l = MemorySize(*ipii)

GetInterfaceInfo_(*ipii, @size)

For i=0 To *ipii\NumAdapters-1
  Debug ReadValue(#HKEY_LOCAL_MACHINE, BaseKey$ + "\" + Right(PeekS(@*ipii\Adapter[i]\name[0]), 38) + "\Connection", "Name")
Next
Thanks again! :D

Kind regards,

Francis
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Getting network interface name

Post by netmaestro »

Yep, I think you're there. Your last code works perfectly on my machine!
BERESHEIT
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Getting network interface name

Post by electrochrisso »

Works here too Win7/32. :)
PureBasic! Purely one of the best 8)
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

electrochrisso wrote:Works here too Win7/32. :)
Nice to know it works on Windows 7 32-bit, since that is the intended target platform (all my Windows machines at home are Windows 10 64-bit). :)

Kind regards,

Francis
Post Reply