Enabling/Disabling Wireless Adapter when running a specific program Topic is solved
Enabling/Disabling Wireless Adapter when running a specific program
Hi, I'm a newbie to the AHK and I was wondering if it's possible to automatically disable the wireless network connection when I run a program and enable it when the program is closed?
Re: Enabling/Disabling Wireless Adapter when running a specific program
Yes. devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck
Re: Enabling/Disabling Wireless Adapter when running a specific program
I've searched quite a bit but I didn't find any that related to my specific queryBoBo wrote:Yes. devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck
Re: Enabling/Disabling Wireless Adapter when running a specific program
... means you've to become a bit less specific: https://www.google.de/search?q=site:aut ... fi+disableaj811 wrote:I've searched quite a bit but I didn't find any that related to my specific queryBoBo wrote:Yes. devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck
Re: Enabling/Disabling Wireless Adapter when running a specific program
You can also do it by running device manager, going to network adapters, and then identify which is your wireless adapter and disable it.
You can do it via using MouseClick, MouseClickDrag, and ControlClick if you want to do it that way. You will also have to run it as an administrator or give it such permissions.
Code: Select all
Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
Last edited by SOTE on 20 Dec 2017, 01:57, edited 2 times in total.
Re: Enabling/Disabling Wireless Adapter when running a specific program
If you want to disconnect instead you could just use something like to disconnect
and to connect
Code: Select all
RunWait, % comspec " /c netsh wlan disconnect",, hide
Code: Select all
RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide
Re: Enabling/Disabling Wireless Adapter when running a specific program
TygerByte wrote:If you want to disconnect instead you could just use something like to disconnectand to connectCode: Select all
RunWait, % comspec " /c netsh wlan disconnect",, hide
Code: Select all
RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide
This is what I'm currently using right now. I've made two separate scripts to do this job and placed shortcuts for them at my task bar. It's working so far but I just wanted to automate the process so that in case I forget it'd still disconnect.
Re: Enabling/Disabling Wireless Adapter when running a specific program
SOTE wrote:You can also do it by running device manager, going to network adapters, and then identify which is your wireless adapter and disable it.
You can do it via using MouseClick, MouseClickDrag, and ControlClick if you want to do it that way. You will also have to run it as an administrator or give it such permissions.Code: Select all
Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
I think I would still have to do it myself if I'm not wrong. I was looking to make it a more automated process so that the script or a file would automatically run and disable the internet or disconnect it so that it would relieve me of the task of doing so.
Re: Enabling/Disabling Wireless Adapter when running a specific program
Code: Select all
SetTimer, Check, % 1000*10 ; check every 10 sec
Return
Check:
Process, Exist, blabla.exe
If (ErrorLevel)
RunWait, % comspec " /c netsh wlan disconnect",, hide
Else
RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide ; enter your wifi's name here
Return
F10::Run, blabla.exe
Re: Enabling/Disabling Wireless Adapter when running a specific program
Using AutoScriptWriter, The Macro Creator, Mouse And Keyboard Macro Recorder, AHK ScriptWriter, etc... can help you do it through the GUI, if you want to go that direction. Going through the command line would be the most reliable, but sometimes going through the GUI has other advantages or is easier for some people who use a macro recorder. There is ActiveWindowInfo.ahk (https://github.com/fincs/SciTE4AutoHotk ... owInfo.ahk), AHK Window Info 1.7 (old), Window Spy (old), etc... They give you info about the window or control you are clicking. You would probably end up with a script something like the below. Remember that when going through the GUI, the script might not be compatible or transferable between computers. You may need to tweak or change it, so that it works for your configuration. And there are different ways a person can accomplish the same thing, with very different looking scripts.aj811 wrote:I think I would still have to do it myself if I'm not wrong. I was looking to make it a more automated process so that the script or a file would automatically run and disable the internet or disconnect it so that it would relieve me of the task of doing so.SOTE wrote:You can also do it by running device manager, going to network adapters, and then identify which is your wireless adapter and disable it.
You can do it via using MouseClick, MouseClickDrag, and ControlClick if you want to do it that way. You will also have to run it as an administrator or give it such permissions.Code: Select all
Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
Code: Select all
Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
WinWait, Device Manager
WinActivate, Device Manager
WinWaitActive, Device Manager
MouseClick, Right, 176, 425, 1
Sleep, 2000
MouseClick, Left, 229, 476, 1
Sleep, 2000
WinWait, Wireless LAN Adapter
WinActivate, Wireless LAN Adapter
WinWaitActive, Wireless LAN Adapter
ControlFocus, &Yes, Wireless LAN Adapter
ControlClick, Button1, Wireless LAN Adapter, , Left, 1, NA,
WinWait, Device Manager
WinActivate, Device Manager
WinWaitActive, Device Manager
MouseClick, Left, 34, 58, 1
Sleep, 1000
MouseClick, Left, 67, 84, 1
Exit
Code: Select all
If not A_IsAdmin ; Placed here as permissions can affect macro recording or playback.
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
Code: Select all
#Persistent
SetTimer, CheckTime, 20000
Return
CheckTime:
TimeIs = %A_Hour%%A_Min%
If (TimeIs = 0900)
{
Run, Script (whatever location and name of your script)
}
Code: Select all
WakeTime := "9:00"
AlarmClock(WakeTime)
If WakeTime = 9:00
{
Run, Script (the location and name of your script)
}
Return
AlarmClock(wakeTime) {
RegExMatch(wakeTime, "^(?<H>\d{1,2}):(?<M>\d{2})$", t)
if (tH = "" || tM = "") {
MsgBox, Wrong wakeTime format!
Return
}
secondToWake := CalcSecondsToWake(tH*3600 + tM*60)
hTimer := DllCall("CreateWaitableTimer", Ptr, 0, UInt, 0, Str, "MyTimer", Ptr)
DllCall("SetWaitableTimer", Ptr, hTimer, Int64P, -secondToWake*10000000, UInt, 0, Ptr, 0, Ptr, 0, UInt, 1)
DllCall("WaitForSingleObject", Ptr, hTimer, UInt, INFINITE := 0xFFFFFFFF)
DllCall("CloseHandle", Ptr, hTimer)
}
CalcSecondsToWake(wakeTime) {
nowTime := A_Hour*3600 + A_Min*60 + A_Sec
ts1 := ts2 := 1601
if (wakeTime < nowTime)
ts1 += 1, d
ts1 += wakeTime, s
ts2 += nowTime, s
ts1 -= ts2, s
Return ts1
}
Last edited by SOTE on 20 Dec 2017, 17:34, edited 1 time in total.
Re: Enabling/Disabling Wireless Adapter when running a specific program
This is exactly what I was looking for! Thank you so much! I tried doing this with the same code but it wasn't working for me so I must've made some stupid mistake.BoBo wrote:Code: Select all
SetTimer, Check, % 1000*10 ; check every 10 sec Return Check: Process, Exist, blabla.exe If (ErrorLevel) RunWait, % comspec " /c netsh wlan disconnect",, hide Else RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide ; enter your wifi's name here Return F10::Run, blabla.exe
I just have one question how do I make the checking time shorter. Say a single digit like 5 seconds or less?
- KuroiLight
- Posts: 327
- Joined: 12 Apr 2015, 20:24
- Contact:
Re: Enabling/Disabling Wireless Adapter when running a specific program Topic is solved
^ SetTimer
though I'd do it like this:
though I'd do it like this:
Code: Select all
TargetProcess := "explorer.exe"
Loop {
Process, Wait, %TargetProcess%
RunWait, % comspec " /c netsh wlan disconnect",, hide
Process, WaitClose, %TargetProcess%
RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide
}
return
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]Re: Enabling/Disabling Wireless Adapter when running a specific program
This one is even better. Thank you so much. You've made my job a whole lot easier.KuroiLight wrote:^ SetTimer
though I'd do it like this:Code: Select all
TargetProcess := "explorer.exe" Loop { Process, Wait, %TargetProcess% RunWait, % comspec " /c netsh wlan disconnect",, hide Process, WaitClose, %TargetProcess% RunWait, % comspec " /c netsh wlan connect name=""WifiName""",, hide } return
Re: Enabling/Disabling Wireless Adapter when running a specific program
Code: Select all
1::
run netsh interface set interface name="Wireless Network Connection" admin = disabled,,hide
return
2::
run netsh interface set interface name="Wireless Network Connection" admin = enabled,,hide
return
Change network connection name according to the one you want to control.