Enabling/Disabling Wireless Adapter when running a specific program Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 19 Dec 2017, 15:31

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?

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by BoBo » 19 Dec 2017, 15:42

Yes. :arrow: devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck :)

aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 19 Dec 2017, 15:57

BoBo wrote:Yes. :arrow: devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck :)
I've searched quite a bit but I didn't find any that related to my specific query

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by BoBo » 19 Dec 2017, 16:10

aj811 wrote:
BoBo wrote:Yes. :arrow: devcon.exe (or whatever native AHK script you will come up with after a few minutes of research). Good luck :)
I've searched quite a bit but I didn't find any that related to my specific query
... means you've to become a bit less specific: https://www.google.de/search?q=site:aut ... fi+disable

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by SOTE » 19 Dec 2017, 17:23

You can also do it by running device manager, going to network adapters, and then identify which is your wireless adapter and disable it.

Code: Select all

Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
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.
Last edited by SOTE on 20 Dec 2017, 01:57, edited 2 times in total.

TygerByte
Posts: 96
Joined: 12 Aug 2016, 05:22

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by TygerByte » 19 Dec 2017, 20:37

If you want to disconnect instead you could just use something like to disconnect

Code: Select all

RunWait, % comspec  " /c netsh wlan disconnect",, hide
and to connect

Code: Select all

RunWait, % comspec  " /c netsh wlan connect name=""WifiName""",, hide	

aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 20 Dec 2017, 03:25

TygerByte wrote:If you want to disconnect instead you could just use something like to disconnect

Code: Select all

RunWait, % comspec  " /c netsh wlan disconnect",, hide
and to connect

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.

aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 20 Dec 2017, 03:26

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.

Code: Select all

Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
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.

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.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by BoBo » 20 Dec 2017, 03:58

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

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by SOTE » 20 Dec 2017, 09:58

aj811 wrote:
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.

Code: Select all

Run, %windir%\system32\control.exe /name Microsoft.DeviceManager
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.
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.
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.

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
Also for various scripts, you may need to run them as administrator, for them to work properly. So would place this code at the very top of the script.

Code: Select all

If not A_IsAdmin ; Placed here as permissions can affect macro recording or playback.
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}
To have it automatically run, people usually use time as a trigger. You can use Task Scheduler or various scripts in AutoHotkey to accomplish this. Like if it's 9AM, and they want a script to do X. An easy way, in AutoHotkey, if your computer is not in sleep mode is...

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)
}
If your computer is in sleep mode and you need to wake it up, that will be a little more complicated of a script. Teadrinker showed a solution, using various DllCalls in his AlarmClock function. It would be something like this.

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.

aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 20 Dec 2017, 13:04

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
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.
I just have one question how do I make the checking time shorter. Say a single digit like 5 seconds or less?

User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Enabling/Disabling Wireless Adapter when running a specific program  Topic is solved

Post by KuroiLight » 20 Dec 2017, 13:38

^ 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
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]

aj811
Posts: 6
Joined: 19 Dec 2017, 15:26

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by aj811 » 20 Dec 2017, 13:49

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
This one is even better. Thank you so much. You've made my job a whole lot easier.

tuncel3
Posts: 1
Joined: 08 Jan 2019, 15:45

Re: Enabling/Disabling Wireless Adapter when running a specific program

Post by tuncel3 » 08 Jan 2019, 15:50

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
Pressing 1 disables the adapter.
Change network connection name according to the one you want to control.

Post Reply

Return to “Ask for Help (v1)”