Get current connection name

Just starting out? Need help? Post your questions and find answers here.
Wolf
Enthusiast
Enthusiast
Posts: 234
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Get current connection name

Post by Wolf »

I looking for an way to get current connection name from Network Connections.

Example: "Connection 1"

I looking in API codes and find some API:

RasEnumConnections
RasEnumEntries
RasGetConnectStatus
RasGetEntryDialParams


Maybe another API do it but i can't find it.

How can we do it?
Wolf
Enthusiast
Enthusiast
Posts: 234
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post by Wolf »

Don't know anybody ? :?
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Here's a couple of things for you to try Wolf. If these don't work for you, we can try something else. :)

Code: Select all

#INTERNET_CONNECTION_MODEM = $1 
#INTERNET_CONNECTION_LAN = $2 
#INTERNET_CONNECTION_PROXY = $4 
#INTERNET_RAS_INSTALLED = $10 
#INTERNET_CONNECTION_OFFLINE = $20 
#INTERNET_CONNECTION_CONFIGURED = $40 

Procedure.s checkConnection() 
  Shared conn$ 
  connectionName$ = Space(256) 
  tf = InternetGetConnectedStateEx_(@lpdwFlags.l, @connectionName$, 256, 0) 
  
  If tf
    
    conn$ = "Connection name is: " + connectionName$ + Chr(13) + Chr(10) 
    
    If lpdwFlags & #INTERNET_CONNECTION_CONFIGURED 
      conn$ + "Local system has a valid connection to the Internet, but it might or might not be currently connected." 
    EndIf 
    
    If lpdwFlags & #INTERNET_CONNECTION_OFFLINE 
      conn$ + "Local system is in offline mode." + Chr(13) + Chr(10) 
    EndIf 
    
    If lpdwFlags & #INTERNET_RAS_INSTALLED 
      conn$ + "Local system has RAS installed." + Chr(13) + Chr(10) 
    EndIf 
    
    If lpdwFlags & #INTERNET_CONNECTION_PROXY 
      conn$ + "Local system uses a proxy server to connect to the Internet." + Chr(13) + Chr(10) 
    EndIf 
    
    If lpdwFlags & #INTERNET_CONNECTION_LAN 
      conn$ + "Local system uses a local area network to connect to the Internet." + Chr(13) + Chr(10) 
    EndIf 
    
    If lpdwFlags & #INTERNET_CONNECTION_MODEM 
      conn$ + "Local system uses a modem To connect To the Internet." + Chr(13) + Chr(10) 
    EndIf 
    
  Else 
    conn$ = "No Internet connection detected." 
  EndIf 
  
  ProcedureReturn conn$ 
EndProcedure 

If OpenWindow(0, 10, 10, 400, 150, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "InternetGetConnectedStateEx") 
  If CreateGadgetList(WindowID(0)) 
    ButtonGadget(0, 10, 10, 200, 20, "Check connection") 
    StringGadget(1, 10, 40, 380, 100, "", #PB_String_MultiLine) 
  EndIf 
  
EndIf 

Repeat 
  
  Event = WaitWindowEvent() 
  
  Select Event 
    
    Case #PB_EventGadget 
      
      If EventGadgetID() = 0 
        checkConnection() 
        SetGadgetText(1, conn$) 
      EndIf 
      
    Case #PB_Event_CloseWindow 
      Quit = #True 
      
  EndSelect 
  
Until Quit = #True 

End 

Another option is to look at some code from ABBKlaus here. You can get the actual connection name just as it appears in the Network Connections folder but you'll have to read a registry key first by adding a few lines of code.

Code: Select all

keyRead.l = 0 
      connectionName$ = Space(255) 
      datasize.l = 255 
      myConnections$ = PeekS(@tempipinfo\AdapterName)
      myKey$ = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + myConnections$ + "\Connection"
      RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, myKey$, 0, #KEY_READ, @keyRead)
      RegQueryValueEx_(keyRead, "Name", 0, 0, @connectionName$, @datasize)
      RegCloseKey_(keyRead)
      Debug "Connection Name : "+connectionName$
Here's the complete code with my addition to ABBKlaus's great code. Run code with debugger for Debug output.

Code: Select all

;***************************************************************************
; original code from ABBKlaus with my addition from line 51 to 59 and line 85
; ***************************************************************************
; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getadaptersinfo.asp 
; with the help of IPHlpAPI.inc from the Powerbasic include-file for the structures 
; Microsoft isn´t quite a help here :-) 

Structure IP_ADDR_STRING 
  pNext.l 
  IpAddress.b[16] 
  IpMask.b[16] 
  Context.l 
EndStructure 

Structure IP_ADAPTER_INFO 
Next.l 
ComboIndex.l 
AdapterName.b[260] ; MAX_ADAPTER_NAME_LENGTH + 4 
Description.b[132] ; MAX_ADAPTER_DESCRIPTION_LENGTH + 4 
AdressLength.l 
Address.b[8] ; MAX_ADAPTER_ADDRESS_LENGTH 
Index.l 
Type.l 
DhcpEnabled.l 
CurrentIpAddressPTR.l 
IpAddressList.IP_ADDR_STRING 
GatewayList.IP_ADDR_STRING 
DhcpServer.IP_ADDR_STRING 
HaveWins.l 
PrimaryWinsServer.IP_ADDR_STRING 
SecondaryWinsServer.IP_ADDR_STRING 
LeaseObtained.l 
LeaseExpires.l 
EndStructure 

length.l=0 
result=GetAdaptersInfo_(0,@length) ; Get the length for Buffer 
If result=#ERROR_BUFFER_OVERFLOW 
  *Buffer=AllocateMemory(length) 
  result=GetAdaptersInfo_(*Buffer,@length) 
  If result=#ERROR_SUCCESS 
    adapters=length/SizeOf(IP_ADAPTER_INFO) 
    For x=0 To adapters-1 
      Debug "AdapterNr : "+Str(x+1) 
      tempipinfo.IP_ADAPTER_INFO 
      For i=0 To SizeOf(IP_ADAPTER_INFO)-1 
        byte.b=PeekB(*Buffer+(x*640)+i) 
        PokeB(tempipinfo+i,byte) 
      Next 
      ; ***** added by Sparkie*****************
      keyRead.l = 0 
      connectionName$ = Space(255) 
      datasize.l = 255 
      myConnections$ = PeekS(@tempipinfo\AdapterName)
      myKey$ = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + myConnections$ + "\Connection"
      RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, myKey$, 0, #KEY_READ, @keyRead)
      RegQueryValueEx_(keyRead, "Name", 0, 0, @connectionName$, @datasize)
      RegCloseKey_(keyRead)
      Debug "Connection Name : "+connectionName$
      ; *****************************************
      Debug "Adapter : "+PeekS(@tempipinfo\Description) 
      Debug "IP-Adress : "+PeekS(@tempipinfo\IpAddressList+4) 
      Debug "Gateway : "+PeekS(@tempipinfo\GatewayList+4) 
      mac$="" 
      For i=0 To 5 
        byte.b=PeekB(@tempipinfo\Address+i) 
        If byte>=0 
          mac$+RSet(Hex(byte),2,"0") 
        Else 
          mac$+RSet(Hex(byte+256),2,"0") 
        EndIf 
        If i<5 
          mac$+":" 
        EndIf 
      Next 
      Debug "MAC-Address "+mac$ 
    Next 
  Else 
    Debug "Error : "+Str(result) 
  EndIf 
EndIf 

;----- Sample dump------------------------------------------------------------------ 
;AdapterNr : 1 
;Connection Name : My Lan 
;Adapter : ELSA Airlancer MC11 High Rate Wireless LAN PC Card - Paketplaner-Miniport 
;IP-Adress : 192.168.1.115 
;Gateway : 192.168.1.1 
;MAC-Address 00:02:2D:3C:2E:73 
;AdapterNr : 2 
;Adapter : Realtek RTL8139-Familie-PCI-Fast Ethernet-NIC - Paketplaner-Miniport 
;IP-Adress : 0.0.0.0 
;Gateway : 
;MAC-Address 00:30:54:00:40:63 
** edited to fix error found by PB **
Last edited by Sparkie on Mon Aug 16, 2004 2:53 am, edited 1 time in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Nice, Sparkie -- thanks! :) But you have a bug in one line:

Code: Select all

conn$ = "Local system uses a modem To connect To the Internet." + Chr(13) + Chr(10) 
Should be:

Code: Select all

conn$ + "Local system uses a modem To connect To the Internet." + Chr(13) + Chr(10) 
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Thanks PB :) Nice catch.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Wolf
Enthusiast
Enthusiast
Posts: 234
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post by Wolf »

Sparkie very very thanks, first code work perfect :D

Next code can't get connection name for me but get for me other info about IP and network.

Nice codes.


Thanks again friend :wink:
Post Reply