Radio.SetStateAsync(RadioState) Method doesn't work for MobileBroadband RadioKind (VB+Windows10) - vb.net

I want to control RadioState of Wifi and Mobile(cellular) connection in Windows 10 x64 using custom application written in VB. It works for Wifi radio but doesn't for MobileBroadband.
Actually code does the same job as click on ActionCenter in Windows 10 and press Wifi or Cellular button.
Private Async Sub TurnMobileOnOff(arg As Integer)
Dim access = Await Windows.Devices.WiFi.WiFiAdapter.RequestAccessAsync
Dim radios = Await Windows.Devices.Radios.Radio.GetRadiosAsync
If access = Windows.Devices.WiFi.WiFiAccessStatus.Allowed Then
For Each radio In radios
If radio.Kind = Windows.Devices.Radios.RadioKind.MobileBroadband Then
If arg = 1 Then
Await radio.SetStateAsync(Windows.Devices.Radios.RadioState.On)
RichTextBox1.AppendText(vbCrLf & "Mobile connection is turninng on")
ElseIf arg = 0 Then
Await radio.SetStateAsync(Windows.Devices.Radios.RadioState.Off)
RichTextBox1.AppendText(vbCrLf & "Mobile connection is turninng off")
End If
End If
Next
End If
End Sub
There's no error message, just radiostate is unchanged. The same code works for WiFi as expected.
Source:https://learn.microsoft.com/en-us/uwp/api/windows.devices.radios.radiostate
Thank for any help.

Related

Telnet (winsock) connection in VBA - class not registered

I have GSM gateway that use telnet for sending/recivieng SMS. I'am making a small app in my ms-access for sending sms notifications for my clients. For that I need winsock connection from my client computer -> gsm gateway. It supposed to be easy :)
I can't establish an object. I'm receiving run-time error -2147221164(80040154) - Class not registered. Error is indicated in "Set winsock = ..." line. I have added reference MS Winsock Control 6.0 (SP6) - MSWINSCK.OCX. I have a feeling this is a problem with reference, but I don't have any idea how to correct it (code below is simplified) :( .
Option Explicit
Private WithEvents Winsock1 As Winsock
Sub Start_Telnet_Session()
Dim Data As String
Dim Winsock1
Set Winsock1 = New MSWinsockLib.Winsock
Winsock1.RemoteHost = "192.168.1.1"
Winsock1.RemotePort = "23"
Winsock1.connect
Do Until Winsock1.State = 7
DoEvents
If Winsock1.State = sckError Then
Exit Sub
End If
Loop
Winsock1.SendData "user" & vbCrLf
Winsock1.SendData "pass" & vbCrLf
Dim I As Integer
I = 5
While TelnetCommands <> ""
Winsock1.SendData "TelnetCommands" & vbCrLf
Winsock1.GetData Data
Data = VBA.Replace(Data, vbLf, "")
Data = VBA.Replace(Data, vbCr, vbNewLine)
MsgBox Data
I = I + 1
Wend
Winsock1.Close
End Sub

Connect to selected wifi VB

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.

VB.net send keypress to game

ive seen other questions about this issue but none of them resolved it for me so here i go. I'm trying to send a keypress to a game to save the progress(its a 64bit game). what i've coded so far is :
Dim p() As Process
Dim GameID As Integer = 0
Dim p As Process() = Process.GetProcessesByName("Gamename")
If p.Length > 0 Then
For i As Integer = 0 To p.Length - 1
GameID = (p(i).Id)
Next
End If
AutoSaveTimer.Enabled = True
Dim Test As Integer = 0
GetAsyncKeyState(Test)
AppActivate("GameName")
My.Computer.Keyboard.SendKeys(";", True)
now i've tried Sendkeys.Send(";") but without luck, and the game runs under "GameName" but then the keypress needs to be sent in a window under the game :
Blacked out is the game and under the first window is where the keypress needs to be sent
thanks in advance for the help
I use InputSimulator in my app and it works great.
https://inputsimulator.codeplex.com/
It could be this easy to send keys. The active window will see that as if the user actually used those keys.
var sim = new InputSimulator();
sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
sim = null;
Here's a screen shot of the methods you can use for keyboard.
You can simulate keyboard input to a program like this:
<DllImport("user32.dll")> _
Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function
Public Sub Send()
Dim p As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("notepad")
'search for process notepad
If p.Length > 0 Then
'check if window was found
'bring notepad to foreground
SetForegroundWindow(p(0).MainWindowHandle)
End If
SendKeys.SendWait("a")
'send key "a" to notepad
End Sub

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!

SEND SMS from samsung galaxy win celphone using vb.net

How to send sms from your cellphone to any number using vb.net? I have searched over the internet including youtube but it seems that i could not find or cant figure out where to start. My CP cant event detected in the "phone and modem" in windows...
I have a perfect way to send SMS in visual basic, using AT-commands.
AT-commands:are instructed through which you can send and receive SMS messages, make calls from cell phone, and this is an example to Send a message:
Firstly:
Write this code in the top:
Imports System.IO.Ports
Imports System.IO
Secondly:
Write this code in the public class of form:
Dim SerialPort As New System.IO.Ports.SerialPort()
Dim CR As String
Thirdly:
Create a textBox(TextmsgTextBox) to write the text message, and TextBox2(MobileNumberTextBox) to enter the mobile number, and Button(SendBUT) to Send message.
And write this code in the button click event.
If SerialPort.IsOpen Then
SerialPort.Close()
End If
SerialPort.PortName = COM4
SerialPort.BaudRate = 9600
SerialPort.Parity = Parity.None
SerialPort.StopBits = StopBits.One
SerialPort.DataBits = 8
SerialPort.Handshake = Handshake.RequestToSend
SerialPort.DtrEnable = True
SerialPort.RtsEnable = True
SerialPort.NewLine = vbCrLf
Dim message As String
message = MsgRichTextBox.Text
SerialPort.Open()
If SerialPort.IsOpen() Then
SerialPort.Write("AT" & vbCrLf)
SerialPort.Write("AT+CMGF=1" & vbCrLf)
SerialPort.Write("AT+CMGS=" & Chr(34) & phoneNumBox.Text & Chr(34) & vbCrLf)
SerialPort.Write(message & Chr(26))
SentPicture.Visible = True
SentLabel.Visible = True
SentTimer.Start()
Else
MsgBox("Port not available")
End If
I Hope My Answer Was Useful To You.
Important Note
Port value change from time to another and from computer to another.
I will show you how to know port value of your device by pictures.
1:Enter to Device Manager from Control Panel.
2:Right click on the device, and choose Properties.
3:Choose Modem tap, and look for port name, and use it in your application.