0

I'm developing a simple network monitoring application that will adjust IPv4 settings on the machine based on what SSID you are connected to, everything I want to achieve is done and ready however my current method of detecting a change in SSID errors out if it cycles whilst I am switching Access Point. The detection itself is very rudimentary and rather primitive, though I lack enough experience in VB.Net to set up a proper event handler myself.

I tried waiting for

Wlan.WlanInterfaceState.NotReady

However this proved unsuccessful. I did go through the motions of googling my issue and looking to see if anyone else encountered a similar issue and the solutions they found also proved entirely unsuccessful in my case.

My main looping function is:

    Public Sub WiFiScan()
        If Wlan.WlanInterfaceState.NotReady Then
            WhatAPResult = "WiFi is Off"
            MsgBox("")
        Else
            WhatAP()
        End If
        MainForm.NetLabelAP.Text = ("Current AP: " + WhatAPResult)
    End Sub

The WhatAP() function referenced here is:

    Public Function WhatAP()
        If wifiON() = True Then
            Dim wlanx = New WlanClient()
            If Wlan.WlanInterfaceState.NotReady Then
            Else
                Dim connectedSsids As Collection(Of [String]) = New Collection(Of String)()

                For Each wlanInterface As WlanClient.WlanInterface In wlanx.Interfaces
                    Dim ssid As Wlan.Dot11Ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid
                    connectedSsids.Add(New [String](Encoding.ASCII.GetChars(ssid.SSID, 0, CInt(ssid.SSIDLength))))

                    For Each item As String In connectedSsids
                        WhatAPResult = item
                        Return True 'CHANGE THE LABEL TO A TEXTBOX OR WHERE EVER YOU WANT TO DISPLAY YOUR CONNECTED WIFI'S NAME.
                    Next
                Next
                ''CONTINUE ON FROM HERE.
            End If
        Else
            Return False
        End If
    End Function

And WifiON() is a basic function in of itself:

    Public Function wifiON()
        Dim nics() As System.Net.NetworkInformation.NetworkInterface =
    System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
        For Each nic In nics
            If nic.Name = "WiFi" And nic.OperationalStatus = OperationalStatus.Up Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

The error I get is:

System.ArgumentException: 'Type 'NativeWifi.Wlan+WlanReasonCode' cannot be marshaled as an unmanaged structure; 

All I want is for the program to accurately detect a change in connected SSID so that I can react accordingly, however this issue is proving to best me well and truly.

Any insight or help would be greatly appreciated.

  • Which line does the error happen on? Also, [pinvoke.net/default.aspx/wlanapi/WlanQueryInterface.html](http://pinvoke.net/default.aspx/wlanapi/WlanQueryInterface.html) might lead you to something useful. – Andrew Morton Apr 29 '19 at 09:21
  • Sadly the error gives me no indication as to where in the API it stems from. As for your link, I don't quite see where it can be applied to my current code, though thankyou for the suggestion. – Josh Watson Apr 29 '19 at 09:29
  • Which line *in your code* does the error happen on? In the page I linked to, there is a "There is a managed WiFi API available..." which links to something which might lead to useful information and/or code. – Andrew Morton Apr 29 '19 at 09:45
  • Looks like you are using a library that is not tested very well. There is another victim that got closer to the underlying problem in [this Q+A](https://stackoverflow.com/questions/31605782/type-native-wifi-wlan-wlanreasoncode-cannot-be-marshaled-error). – Hans Passant Apr 29 '19 at 09:45
  • Apologies, @AndrewMorton, my mistake. While it doesnt give me an exact line to which the error occurred from, I can confidently assume it stems from line containing `Dim ssid As Wlan.Dot11Ssid = ...` As for your link, @HansPassant, I have came across that before but have been unable to locate that particular line. Would I have to download the API source code, edit it, then rebuild it myself? – Josh Watson Apr 29 '19 at 09:48

0 Answers0