Connect to selected wifi VB - vb.net

I successfully list out remembered wifi in my windows profile using Native WI-FI from Nuget package. This is my code load list of WI-FI
Private Sub loadWifi()
listWifi.Items.Clear()
Dim wlan As WlanClient = New WlanClient()
Dim connectedSsids As List(Of String) = New List(Of String)()
For Each wlanIface As WlanClient.WlanInterface In wlan.Interfaces
For Each profileinfo As Wlan.WlanProfileInfo In wlanIface.GetProfiles()
listWifi.Items.Add(profileinfo.profileName)
Next
Next
End Sub
My intention is how can I get the selected WI-FI from combo box listWifi and connect to the network.
Then im using netsh command to connect with the network. Take from the combo box. It does not work
Private Sub ConnectTo(ByVal name As String)
Dim p = "netsh.exe"
Dim sInfo As New ProcessStartInfo(p, "wlan connect " & name)
sInfo.CreateNoWindow = True
sInfo.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(sInfo)
End Sub

I made decision to use SimpleWIFI API rather than Native since it will cause my application to crash (Exception: cannot be marshaled error).
To connect the network just simply netsh wlan disconnect first and connect the network using codes above. It works but with minor error (not all available network scanned sometimes) rather than my apps to close itself unexpectedly.

Related

Get List of all PC's in a local area network

The following code works intermittently. Why is that so? Sometimes result is blank,without any error.
Imports System.DirectoryServices
Public Class Form1
Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button.Click
Dim result As String = ""
Dim domainEntry As DirectoryEntry = New DirectoryEntry("WinNT://CA")'CA is the workgroup
domainEntry.Children.SchemaFilter.Add("Computer")
For Each computer As DirectoryEntry In domainEntry.Children
result = result & computer.Name & Environment.NewLine
Next
MsgBox(result)
End Sub
End class
Windows workgroups are a peer-to-peer arrangement, with no central server that controls who is on the workgroup. As such, the only way to list all the computers in the workgroup is to contact every computer in your network and check if they are part of the workgroup. That means each computer has to be powered on and connected to the same LAN.
My guess is that some computers are offline when you run that, some other reason that they cannot be contacted at the time you run it.
Old answer:
I'm not terribly familiar with using the WinNT provider, but see if you get different results with LDAP. This would find all computers on the domain (I assume "CA" is the name of your domain):
Dim ds As New DirectorySearcher(New DirectoryEntry("LDAP://CA"), "(objectClass=computer)", New String() {"cn"})
Using src As SearchResultCollection = ds.FindAll()
For Each sr As SearchResult In src
result = result & sr.Properties("cn")(0) & Environment.NewLine
Next
End Using

Check for valid connection between frontend and backend Access database

I have an Access database application that I've split into a Frontend and Backend. The backend sits on a shared network drive that all users can access. My issue is that when a user launches the frontend of this app, and they don't have a connection to the backend because the shared drive may not have been mounted locally, the initial form to be displayed when the app is launched, doesn't open and leaves the user questioning what's going on. I already have code to check if the backend is connected although for some reason, when it isn't connected the intro screen form never is displayed and the access app just sits there
Private Sub Form_Open(Cancel As Integer)
Dim strBackEndPath, LResult As String
Dim i, j, lenPath As Integer
'initialize variable status to 0
Me.BEDB_Status = 0
'define what to check in backend database
strBackEndPath = CurrentDb.TableDefs("VersionInfo-Available").Connect
' Now remove the datebase & password prefix
j = InStrRev(strBackEndPath, "=") + 1
strBackEndPath = Mid(strBackEndPath, j)
'Checking access to Backend database files...
Me.MessageText = "Checking access to Backend database files..."
On Error Resume Next
LResult = Dir(strBackEndPath)
'Set status to Length of LResult
Me.BEDB_Status = Len(LResult)
'Check length of BEDB_Status, if greater than 0, backend is connected. If 0, backend is not connected.
If Me.BEDB_Status > 0 Then
'length is greater than 0 so continue opening the app
DoCmd.OpenForm "IntroScreen"
Else
'length is 0 so backend is not connected. Alert user and quit the app
Me.MessageText = "The database isn't currently accessible. Program will now exit. Please ask the support team for assistance"
DoCmd.Quit acQuitSaveNone
End If
End Sub
It may be simpler and much faster to attempt opening one of the linked tables and ignore the error:
Public Function IsLinkedTable(ByVal TableName As String) As Boolean
Dim LinkOk As Boolean
On Error Resume Next
LinkOk = (DCount("*", TableName) >= 0)
IsLinkedTable = LinkOk
End Function
And do use the OnLoad event of the form as this allows the form to open.
Implemented Gustav's suggestion to use the On Load event rather than the On Open event along with his IsLinkedTable function and it's working great.
Thanks Gustav.

Already running application now gets socket error 10013

I have an application done in VB.NET that listen on a specific UDP port and answer through the same port to the IP that send the packet.
It was working ok from a couple of years to the last month; now when try to answer crash due to socket error 10013.
I even try an older version that I know it was working too and get the same crash.
I try disabling Microsoft Security Essentials real time protection and Windows firewall and didn't work.
In the code I have the line
MyUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
I have no clue about what to do, I'm lost.
Any idea how to solve this?
Edit:
Here's the code
#Region "UDP Send variables"
Dim GLOIP As IPAddress
Dim GLOINTPORT As Integer
Dim bytCommand As Byte() = New Byte() {}
#End Region
Dim MyUdpClient As New UdpClient()
Private Sub StartUdpBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartUdpBtn.Click
If StartUdpBtn.Tag = 0 Then
' If Not UdpOpen Then
StartUdpReceiveThread(CInt(ListeningPortLbl.Text))
'End If
Else
If ThreadReceive.IsAlive Then
ThreadReceive.Abort()
MyUdpClient.Close()
PrintLog("UDP port closed")
StartUdpBtn.Tag = 0
UdpOpen = False
StartUdpBtn.Text = "Start UDP"
End If
End If
If UdpOpen Then
StartUdpBtn.Tag = 1
StartUdpBtn.Text = "Stop UDP"
Else
StartUdpBtn.Tag = 0
StartUdpBtn.Text = "Start UDP"
TimerUDP.Enabled = False
TiempoUDP.Stop()
TiempoUdpLbl.Text = "--:--:--"
End If
End Sub
Private Sub StartUdpReceiveThread(ByVal Port As Integer)
Dim UdpAlreadyOpen As Boolean = False
Try
If Not UdpOpen Then
MyUdpClient = New UdpClient(Port)
MyUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
UdpAlreadyOpen = True
Else
Me.Invoke(Sub()
TiempoUDP.Restart()
If TimerUDP.Enabled = False Then
TimerUDP.Enabled = True
End If
End Sub)
End If
ThreadReceive = New System.Threading.Thread(AddressOf UdpReceive)
ThreadReceive.IsBackground = True
ThreadReceive.Start()
UdpOpen = True
If UdpAlreadyOpen Then
PrintLog(String.Format("UDP port {0} opened, waiting data...", Port.ToString))
End If
Catch ex As Exception
PrintErrorLog(ex.Message)
PrintErrorLog(ex.StackTrace)
End Try
End Sub
Private Sub UdpReceive()
Dim receiveBytes As [Byte]() = MyUdpClient.Receive(RemoteIpEndPoint)
DstPort = RemoteIpEndPoint.Port
IpRemota(RemoteIpEndPoint.Address.ToString)
Dim BitDet As BitArray
BitDet = New BitArray(receiveBytes)
Dim strReturnData As String = System.Text.Encoding.ASCII.GetString(receiveBytes)
If UdpOpen Then
StartUdpReceiveThread(CInt(ListeningPortLbl.Text))
End If
PrintLog("From: " & RemoteIpLbl.Text & ":" & ListeningPortLbl.Text & " - " & strReturnData)
AnswersProcessor(strReturnData)
End Sub
Private Sub UdpSend(ByVal txtMessage As String)
Dim pRet As Integer
GLOIP = IPAddress.Parse(RemoteIpLbl.Text)
'From UDP_Server3_StackOv
Using UdpSender As New System.Net.Sockets.UdpClient()
Dim RemoteEndPoint = New System.Net.IPEndPoint(0, My.Settings.UDP_Port)
UdpSender.ExclusiveAddressUse = False
UdpSender.Client.SetSocketOption(Net.Sockets.SocketOptionLevel.Socket, Net.Sockets.SocketOptionName.ReuseAddress, True)
UdpSender.Client.Bind(RemoteEndPoint)
UdpSender.Connect(GLOIP, DstPort)
bytCommand = Encoding.ASCII.GetBytes(txtMessage)
pRet = UdpSender.Send(bytCommand, bytCommand.Length)
End Using
PrintLog("No of bytes send " & pRet)
End Sub
10013 is WSAEACCES, which is documented as follows:
Permission denied.
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto without broadcast permission being set using setsockopt(SO_BROADCAST).
Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4.0 with SP4 and later), another application, service, or kernel mode driver is bound to the same address with exclusive access. Such exclusive access is a new feature of Windows NT 4.0 with SP4 and later, and is implemented by using the SO_EXCLUSIVEADDRUSE option.
In the comments you mentioned:
I tried the program on a XP x32 and works ok but on Windows 7 x32/x64 don't, even if I disable the firewall and Microsoft Security Essentials Live Protection.
Maybe it sounds almost obvious but you could try to start your program in all of the available Windows XP compatibility modes. You didn't say that you already tried this but maybe you're lucky and the problem will be "solved" by this workaround.
If the problem still exists afterwards and considering the error code of 10013, I would try or check the following things:
I know you disabled "Microsoft Security Essentials" and the Windows Firewall, but double check whether there are other security related programs/services like anti virus protection, anti malware tools etc. running. It really sounds like something is blocking your socket creation/bind.
In case your program created log output/data which allows you to see exactly when it started to fail:
Any new software installed at that time?
Were Windows Updates (maybe automatically) installed at that time? Especially security updates regarding network security?
Any other noticeable changes in your environment? What about log entries in your Windows system log?
Just as a little test to verify if the error occurs only with your UDP socket: Try to use a TCP socket instead of UDP.
Start the machine in Windows Safe Mode with network support and execute your program from there.
Run your program on another Windows 7 machine and see if the same problem occurs there. It could be a valuable starting point (in terms of localization) to know if the problem occurs only on specific versions of Windows.
Single step through your code with a debugger and carefully watch what happens. Perhaps this can reveal some additional info on what's going wrong.
Maybe some of the ideas above can help you to track down the problem a little bit more. Good luck!

Geting MAC address by using VB.NET 2010

I have tried to get the MAC address of each network interface card on a machine by using the below function in VB.NET, but I just realized that this function doesn't work in Windows XP:
Function getMacAddress()
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
Return nics(1).GetPhysicalAddress.ToString
End Function
How can I make this code to run on Windows XP? What other alternatives exist to get the list of MAC addresses on Windows XP?
I did some digging when connecting to different VPNs. So far the below seems pretty reliable. Relying on 0 or 1 for the actual physical adapter as suggested above does not work in many cases. In some cases my actual Ethernet adapter was the 3rd adapter. Excluding the loopbacks, tunnels, and ppp adapters should narrow it down. I found that many of my non physical adapters have the string "00000000000000E0" as the mac address.
Private Function getMacAddress() As String
Try
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim adapter As NetworkInterface
Dim myMac As String = String.Empty
For Each adapter In adapters
Select Case adapter.NetworkInterfaceType
'Exclude Tunnels, Loopbacks and PPP
Case NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback, NetworkInterfaceType.Ppp
Case Else
If Not adapter.GetPhysicalAddress.ToString = String.Empty And Not adapter.GetPhysicalAddress.ToString = "00000000000000E0" Then
myMac = adapter.GetPhysicalAddress.ToString
Exit For ' Got a mac so exit for
End If
End Select
Next adapter
Return myMac
Catch ex As Exception
Return String.Empty
End Try
End Function
Works for me on XP, except I've got a few interfaces and my first (0th) is my "real" MAC address, and it corresponds to the MAC address reported by a non-.NET program.

How do I check if an ftp server is online and get the error that it generates if it is not connected?

I am new to programming in vb.net. I have come a long ways in my development and understanding of vb, but there is one hurtle I can not seem to fix. I am hosting an ftp server on my pc and I am making an app for it to connect to my server and download files. The problem with all the sample code is that everyone ASSUMES the server WILL be ONLINE. My pc may not be running 24/7 and I also may not have the ftp service running.In the first case it shouldnt even register that it is connected. In the second case, it WILL say that is connected b/c the pc is on, but it will return that the machine ou are trying to connect to is actively refusing the connection. Is there a way to TRULY check if the program is indeed connected to the server WITHOUT generating a bunch of Exceptions in the debugger? All I want is a call like:
Dim ftponline As Boolean = False 'Set default to false
ftponline = checkftp()
If ftponline Then
'continue program
Else
'try a different server
End If
So it would be a function called checkftp that returns a boolean value of true or false.
Here is my info:
Using Visual Studio 2010 Pro
Using .Net framework 4
Can anyone help?
Thanks!
I have tried the rebex ftp pack as well as the Ultimate FTP Pack.
Here is the updated code:
Public Function CheckConnection(address As String) As Boolean
Dim logonServer As New System.Net.Sockets.TcpClient()
Try
logonServer.Connect(address, 21)
Catch generatedExceptionName As Exception
MessageBox.Show("Failed to connect to: " & address)
End Try
If logonServer.Connected Then
MessageBox.Show("Connected to: " & address)
Return True
logonServer.Close()
Else
Return False
End If
End Function
Public Sub ConnectFtp()
types.Clear()
models.Clear()
ListBox1.Items.Clear()
ListBox2.Items.Clear()
TextBox2.Clear()
Dim request As New Rebex.Net.Ftp
If CheckConnection(*) Then
Dim tempString As String()
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory("/atc3/HD_Models")
Dim list As Array
list = request.GetNameList()
Dim item As String = ""
For Each item In list
tempString = item.Split(New Char() {" "c})
If types.Contains(tempString(0)) = False Then
types.Add(tempString(0))
End If
If models.Contains(item) = False Then
models.Add(item)
End If
Next
request.Disconnect()
request.Dispose()
ElseIf CheckConnection(*) Then
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory(*)
Dim list2 As Array
list2 = request.GetNameList()
Dim item2 As String = ""
Dim tempString2 As String()
For Each item2 In list2
MessageBox.Show(item2)
tempString2 = item2.Split(New Char() {" "c})
If types.Contains(tempString2(0)) = False Then
types.Add(tempString2(0))
End If
If models.Contains(item2) = False Then
models.Add(item2)
End If
Next
request.Disconnect()
request.Dispose()
End If
End Sub
No matter what I do, the second server will not connect. I even put a messagebox to show what items were being returned in the second server, but there are no messageboxes apearing when I run the program with my server offline. Is there anyone who can help?
If your code is designed with proper exception catching, it shouldn't be generating a "bunch" of exceptions. The first exception you catch should be your indication that the connection failed and your code should cease attempting to communicate at that point. If for some reason you really need to check the connectivity before attempting the FTP connection, you should be able to simply attempt to synchronously open a TCP socket to the FTP server's port. If that works, it's up and running.
You could simply open a socket to the server's IP address on Port 21 (assuming default FTP port).
I'm not much of a VB.Net programmer, but here's a link to sample code:
http://vb.net-informations.com/communications/vb.net_Client_Socket.htm
If you can establish the socket connection, you know that something is listening on that port (though you have not yet proven it's an FTP server, or that it will accept your login credentials...).
If you wish to simply avoid exceptions in the debugger, you could place the connection code in a method and apply the DebuggerHidden attribute to that method.