locked
Detect Wireless Adapter to see if it has IP address and Wireless Network Adapter is enabled. RRS feed

  • Question

  • Hi I have done as much research on this as I can and would like some sample code on the best way around doing this.

    I am using Visual Basic 2008, I have a application that has a 7 step process to check various things on the machine. Step one and two is what i am struggling with.

    Screenshot of form Screen Shot Of Application

    Step1 .  Need some code to check the wireless adapter on the laptops is enabled if not msgbox "adapter is disabled"

    Now this program will run on several laptops so needs to check for the name wireless in the description of the apapter.

    Step2.  Need some code to check the wireless apapter has been allocated an ip address if not msgbox "wireless connection not in range"

    Ideally i would like to check the signal strenth but that looks to be very tricky with code just checking if the apapter has ip address is fine for this project.

    I have managed to code all the other steps no problem.

    I do have some vbscript code which i will paste which does do this but having loads of trouble in vb using it. Im sure there are better things that can be used.

    '*----------------------------------------------------------------------------------------*'
    '* Name         : CheckStep(intStep)                                          *'
    '* Description  : Check what stage of connection laptop is at                        *'
    '*----------------------------------------------------------------------------------------*'
    Function CheckStep(intStep)

     '********************* Check Step 1 - Wireless is enabled *************************************
     intIP = 0
     If intStep = 1 Then
      If bolDebug then Msgbox "in Step 1"
      strLogText = vbCrLf & "*********************Step 1 - Wireless Enabled *************************" & vbCrLf
      Set objWMIService2 = GetObject("winmgmts:\\.\root\CIMV2")
      Set colItems1 = objWMIService2.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
      bolWireless = False
      For Each objItem in colItems1
       'If Wireless then....
       If Instr(UCase(objItem.Caption),"WIRELESS") Then
        bolWireless = True
        On Error Resume Next
        strIP = "0.0.0.0"
        strIP = Join(objItem.IPAddress, ",")
        strDefaultIPGateway = "0.0.0.0"
        strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
        On Error GoTo 0
        If Not objItem.IPEnabled Then
         strLogText = strLogText & "Disabled: " & objItem.Caption & vbCrLf
         strLogText = strLogText & "!!!Error: adapter not enabled." & vbCrLf
         msgbox "Error: Adapter is not enabled! Please contact support to get it enabled."
         CheckStep = 1
        Else
         strLogText = strLogText & "Enabled: " & objItem.Caption & vbCrLf
         strLogText = strLogText & "     IP: " & strIP & vbCrLf
         strLogText = strLogText & "     Default Gateway: " & strDefaultIPGateway & vbCrLf
         strLogText = strLogText &  "     MAC: " & objItem.MACAddress & vbCrLf
         strLogText = strLogText &  "     DNS Name: " & objItem.DNSHostName & vbCrLf
         strGate = strDefaultIPGateway
         strMAC = objItem.MACAddress
         strNIC = objItem.Caption
         strName = objItem.DNSHostName
         CheckStep = 2
        End If
       End If
      Next
      
      If bolWireless = False Then
       strLogText = strLogText & "No wireless adapters found. Please ensure an adapter is installed and turned on."
       Msgbox "No wireless adapters found. Please ensure an adapter is installed and turned on.",,"No adapters found"
      End If
      
      If strLogtext <> strLogtext1 Then
       If bolDebug Then DataArea2.InnerText = DataArea2.InnerText & strLogText
       strLogtext1 = strLogtext
      End If
     End If
     '********************* Check Step 2 - Wireless detected *************************************
     'checked by if it has an ip address
     intIP = 0
     If intStep = 2 Then
     
      If bolDebug then Msgbox "in Step 2"
      strLogText = vbCrLf & "*********************Step 2 - Wireless Network *************************" & vbCrLf
      Set objWMIService2 = GetObject("winmgmts:\\.\root\CIMV2")
      Set colItems1 = objWMIService2.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
      For Each objItem in colItems1
       'If Wireless then....
       If Instr(UCase(objItem.Caption),"WIRELESS") Then
        On Error Resume Next
        strIP = "0.0.0.0"
        strIP = Join(objItem.IPAddress, ",")
        strDefaultIPGateway = "0.0.0.0"
        strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
        On Error GoTo 0
        If Not objItem.IPEnabled Then
         strLogText = strLogText & "Disabled: " & objItem.Caption & vbCrLf
         strLogText = strLogText & "!!!Error: adapter not enabled." & vbCrLf
         CheckStep = 1
        Else
         strLogText = strLogText & "Enabled: " & objItem.Caption & vbCrLf
         strLogText = strLogText & "     IP: " & strIP & vbCrLf
         strLogText = strLogText & "     Default Gateway: " & strDefaultIPGateway & vbCrLf
         strLogText = strLogText &  "     MAC: " & objItem.MACAddress & vbCrLf
         strLogText = strLogText &  "     DNS Name: " & objItem.DNSHostName & vbCrLf
         strGate = strDefaultIPGateway
         strMAC = objItem.MACAddress
         strNIC = objItem.Caption
         strName = objItem.DNSHostName
         strDomain = objItem.DNSDomain
         If strIP <> "0.0.0.0" Then
          'has ip address
          strCurrIPs = strCurrIPs & objItem.Caption & ", " & strIP & ", " & strDefaultIPGateway & VbCrLf
          intIP = intIP + 1

          strIP = CalcSpaces(strIP, 16)
          DataAreaIP.innerHTML = "<font face=Arial color=#0000FF size=2><b>&nbsp;&nbsp;" & strIP  & "</b></font>"
          
          strDomain = CalcSpaces(strDomain, 68)
          DataAreaIP.innerHTML = DataAreaIP.innerHTML & "<font face=Arial color=#0000FF size=2><b>" & strDomain & "</b></font>"
          
          CheckStep = 3
          SignalStrength()
         Else
          strLogText = strLogText & "     no ip address received " & IPCheck & vbCrLf
          If bolDebug then msgbox "no ipaddress to loop step 2"
          If k = 0 Then
           If bolDebug then msgbox "change 1"
           ChangePicture(1)
           Sleep(1)
           k = 1
          Else
           If bolDebug then msgbox "change 2"
           ChangePicture(2)
           Sleep(1)
           k = 0
          End If
          CheckStep = 2
         End If
        End If
       ElseIf objItem.IPEnabled Then
        strLogText = strLogText &  "Enabled: " & objItem.Caption & vbCrLf
        strIP = Join(objItem.IPAddress, ",")
        On Error Resume Next
        strDefaultIPGateway = "0.0.0.0"
        strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
        On Error GoTo 0
              'strLogText = strLogText &  "DefaultIPGateway: " & strDefaultIPGateway
        strLogText = strLogText &  "     " & strIP & vbCrLf
        strLogText = strLogText &  "     " & strDefaultIPGateway & vbCrLf
        If strIP <> "0.0.0.0" Then
         'it has an ip address through something else rather than wireless that could upset the rest of the script
         strCurrIPs = strCurrIPs & objItem.Caption & ", " & strIP & ", " & strDefaultIPGateway & VbCrLf
         intIP = intIP + 1
        End If
       End If
      Next
      
      If intIP > 1 Then
       strLogText = strLogText &  "More than one IP Address. Could also be plugged into the LAN, have 3G card connected or a windows mobile device." &  VbCrLf & strCurrIPs
      End If
      
      If strLogtext <> strLogtext2 Then
       If bolDebug Then DataArea2.InnerText = DataArea2.InnerText & strLogText
       strLogtext2 = strLogtext
      End If


     
    Wednesday, November 26, 2008 11:12 AM

Answers

  • Some code since thread is about wireless

     Dim adapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces() 
            Dim wirelessfound As Boolean = False 
            For Each adapter As NetworkInterface In adapters 
                If adapter.NetworkInterfaceType = NetworkInterfaceType.Wireless80211 Then 
                    Debug.WriteLine("Name " & adapter.Name) 
                    Debug.WriteLine("Status:" & adapter.OperationalStatus.ToString) 
                    Debug.WriteLine("Speed:" & adapter.Speed.ToString()) 
                    Debug.WriteLine(adapter.GetIPProperties.GetIPv4Properties) 
                    MessageBox.Show(adapter.GetIPProperties.GetIPv4Properties.IsDhcpEnabled.ToString) 
                    If adapter.GetIPProperties.GetIPv4Properties.IsDhcpEnabled Then 
                        Debug.WriteLine("Dynamic IP"
                    Else 
                        Debug.WriteLine("Static IP"
                    End If 
                    wirelessfound = True 
     
                End If 
                
            Next 
            If Not wirelessfound Then 
                Debug.WriteLine("Wireless is either disabled or not present"
            End If 


    Arjun Paudel
    • Marked as answer by Xingwei Hu Monday, December 1, 2008 5:57 AM
    Wednesday, November 26, 2008 4:46 PM
    • Marked as answer by Xingwei Hu Monday, December 1, 2008 5:57 AM
    Wednesday, November 26, 2008 12:51 PM

All replies

    • Marked as answer by Xingwei Hu Monday, December 1, 2008 5:57 AM
    Wednesday, November 26, 2008 12:51 PM
  • I am not really sure, If your concept is only funky, or if it is really bit of "scratch" (not regarding Scratch language). Do you want to check wireless connectivity, or second level factors, as IP's, FW's, DNS's etc? Because if you want to check such parameters, as signal etc, you are going, as I told you before, to write software, probably for only one device, very specialised, working with some low level drivers etc. But if you want to check also so large range of connection features, from WAN connectivity to LAN connectivity and, god knows why, LAN drives (what about printers?), you will be messed with very hard struggles on every side. And as you seem to work yourself at it, you can't work it out... Also I am convinced, that checking drives is nonsense... what they have to do with wl connectivity, or either with checking? I would check only if all computers, which I have "checked as required to access", have same domain - it means, I would only checked If I see them all. But it seems really to be very hot-needle work. I would just take month to reconcept it... but take is only as opinion of not-knower, and don't really take offences. If you know what you want, go for it, but at least "Wirelss" is not correct much :) I agree with "Main Network" term with great silence, for there is term LAN for it, and also there is none "Slave network" at your presentation :) et cetera... But really main problem is that "checking of signal" combined with from "checking windows settings" to "defragment and optimize" like topics... bf

    Sorry, I am confused by your interface much, sorry
    Regards, Matt
    This contributor is MSDN testing bot, using BabelFish translator, passive NN "Thread integrator" and Google's VBCContextSearch api to help you resolving threads, if you want to improve it, just react to its posts. Your MSDN team :)
    Wednesday, November 26, 2008 2:32 PM
  • Hi Mike,

    Is your server down?

    I can not view.>>

    http://www.totallabelmanagement.com/images/form.jpg

    What no homepage yet either?

    Additionally are you using VB6 or earlier?

    If so please see my reply here.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/504640c4-fb1b-46b8-99ea-49ce28b84ad9

    By the way Mike. It might be easier to try to PING a website from Visual Basic to see if you are connected rather than try to detect the IP address and whether or NOT the wireless adapter is enabled.

    If a wireless router is connected as a separate item to an ADSL or a CABLE modem then depending on the setup of the router and modem you still may NOT get a browser to show website pages.  


    Regards,

    John


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835

    Wednesday, November 26, 2008 2:55 PM
  • Hi Many thanks to
    dbasnett for the reply ,

    I have now managed to code Button 2 and its working great with the below code still looking for a soloution for button 1

    PictureBox2.Visible = False
            PictureBox9.Visible = False
            TextBox2.Clear()

            Dim strBldr As New System.Text.StringBuilder
            Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties

            Dim blnFoundOne As Boolean

            strBldr.Append("Local" & " - " & Date.Now.ToLongDateString & "  " & DateTime.Now.ToString("HH:mm:ss"))
            strBldr.Append("    UTC" & " - " & Date.Now.ToUniversalTime.ToLongDateString & "  " & Date.Now.ToUniversalTime.ToString("HH:mm:ss") & vbCrLf)
            strBldr.Append(Format(Math.Ceiling(My.Computer.Info.TotalPhysicalMemory / (1024 * 1024)), "#,##0.0  MB RAM Total   "))
            strBldr.Append(Format(Math.Ceiling(My.Computer.Info.AvailablePhysicalMemory / (1024 * 1024)), "#,##0.0  MB RAM Available") & vbCrLf)
            strBldr.AppendLine(My.Computer.Info.OSFullName & "  " & My.Computer.Info.OSVersion & vbCrLf)
            strBldr.Append("Host name: " & ipProperties.HostName & ControlChars.NewLine)
            strBldr.Append("Domain name: " & ipProperties.DomainName & ControlChars.NewLine)
            For Each networkCard As System.Net.NetworkInformation.NetworkInterface _
                In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces

                If InStr(LCase(networkCard.Description), "wireless") Then
                    If networkCard.GetIPProperties().UnicastAddresses.Count > 0 Then
                        TextBox2.ForeColor = System.Drawing.Color.Green
                        TextBox2.Font = New Font(TextBox2.Font, _
                        FontStyle.Bold)
                        TextBox2.Text = "CONNECTED"
                        PictureBox2.Visible = True
                        blnFoundOne = True
                    End If
                End If

            Next
            If Not blnFoundOne Then
                TextBox2.ForeColor = System.Drawing.Color.Red
                TextBox2.Font = New Font(TextBox2.Font, _
                FontStyle.Bold)
                TextBox2.Text = "FAILED"
                PictureBox9.Visible = True
                MsgBox("Please check you are in range of a wireless network")
            End If
    Wednesday, November 26, 2008 3:58 PM
  • Some code since thread is about wireless

     Dim adapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces() 
            Dim wirelessfound As Boolean = False 
            For Each adapter As NetworkInterface In adapters 
                If adapter.NetworkInterfaceType = NetworkInterfaceType.Wireless80211 Then 
                    Debug.WriteLine("Name " & adapter.Name) 
                    Debug.WriteLine("Status:" & adapter.OperationalStatus.ToString) 
                    Debug.WriteLine("Speed:" & adapter.Speed.ToString()) 
                    Debug.WriteLine(adapter.GetIPProperties.GetIPv4Properties) 
                    MessageBox.Show(adapter.GetIPProperties.GetIPv4Properties.IsDhcpEnabled.ToString) 
                    If adapter.GetIPProperties.GetIPv4Properties.IsDhcpEnabled Then 
                        Debug.WriteLine("Dynamic IP"
                    Else 
                        Debug.WriteLine("Static IP"
                    End If 
                    wirelessfound = True 
     
                End If 
                
            Next 
            If Not wirelessfound Then 
                Debug.WriteLine("Wireless is either disabled or not present"
            End If 


    Arjun Paudel
    • Marked as answer by Xingwei Hu Monday, December 1, 2008 5:57 AM
    Wednesday, November 26, 2008 4:46 PM