Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Enable/Disable a Network Connection


  • Please log in to reply
59 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
It will enable/disable a network connection in the Network Connections folder.
Assign the variable sConnection to the target connection name in the script.
And, assign the variable bEnable to 0 to disable the connection.

PS. Tested only in XPSP2!
And, as the Verbs are language-dependent, it may not work in other than English version.
Then, please try to replace the accelerator keys, &A and &B, in the script with the appropriate ones.

Need CoHelper.ahk.

#Include CoHelper.ahk

NetConnect(0)
Sleep, 5000
NetConnect(1)


NetConnect(bEnable = False, sConnection = "Local Area Connection")
{
	CoInitialize()
	psh := ActiveXObject("Shell.Application")

	pns := Invoke_(psh, "Namespace", 3, CSIDL_Connections:=0x0031)
	pitems := Invoke(pns, "Items")

	Loop, % Invoke(pitems, "Count")
	{
		pid := Invoke_(pitems, "Item", 3, A_Index-1)
		If (Invoke(pid, "Name") = sConnection)
		{
		   bRes := True
		   Break
		}
		Release(pid)
	}

	If !bRes
	ExitApp

	pverbs := Invoke(pid, "Verbs")
	pvb := Invoke_(pverbs, "Item", 3, nVB:=0)

	If pvb
	{
		sVerbName := Invoke(pvb, "Name")
		If (bEnable && InStr(sVerbName, "&a")) || (!bEnable && InStr(sVerbName, "&b"))	; &a and &b can be Language dependent.
		Invoke(pvb, "DoIt")
		WinWait, %sConnection% ahk_class #32770,, 1
		WinWaitClose
		Release(pvb)
	}

	Release(pverbs)
	Release(pid)
	Release(pitems)
	Release(pns)
	Release(psh)
	CoUninitialize()
}


Arnoud
  • Members
  • 15 posts
  • Last active: Oct 22 2007 02:17 PM
  • Joined: 23 Mar 2007

Then, please try to replace the accelerator keys, &A and &B, in the script with the appropriate ones.


Where can I find those? Where did you see the &A and &B.

Very usefull script this!

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Where can I find those? Where did you see the &A and &B.

It's in the right-click context menu of the connection item.
They'll appear as underlined characters there. For example, in English Windows,

Enable
Disable

I.e., they are &a, &b, in English Windows.

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
very nice script

can you tell me please how can I, with one hotkey, disable, wait some seconds and then enable the connection?

thanks !

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

can you tell me please how can I, with one hotkey, disable, wait some seconds and then enable the connection?

Just encapsulate the content of the script inside the function NetConnect().
Of course, you have to remove the the first two lines starting with bEnable, and sConnection there as they now become parameters.

#Include CoHelper.ahk

NetConnect(0)
Sleep, 5000
NetConnect(1)

NetConnect(bEnable = True, sConnection = "Local Area Connection")
{
...
}


azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
mm, I just figured out that my dsl connection appears as a "dialup connection" in the network folder, so I conclude it wont work, right?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

mm, I just figured out that my dsl connection appears as a "dialup connection" in the network folder, so I conclude it wont work, right?

I don't have a dial-up connection anymore, however, I can't see any reason why not.
Assuming dialup connection is the correct name:

NetConnect(0, "dialup connection")
Sleep, 5000
NetConnect(1, "dialup connection")


Thalon
  • Members
  • 641 posts
  • Last active: Jan 02 2017 12:17 PM
  • Joined: 12 Jul 2005
Seems to need more changes for german windows.
A simple change of sConnection (and bEnable) didn't help here...

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
this is my code:

#r::

NetConnect(0, "TheNameOfMyDialUpConnection") 
Sleep, 5000 
NetConnect(1, "TheNameOfMyDialUpConnection") 

NetConnect(bEnable = True, sConnection = "TheNameOfMyDialUpConnection") 
{
#Include CoHelper.ahk
;SetTitleMatchMode, 2

bEnable := True         ; True to enable, False to disable.
sConnection := "Local Area Connection" ; Replace it with the name of the target connection in Network Connections Folder

nVB := 0         ; 0-based index of the verb, i.e., 0 for Enable/Disable.
CSIDL_Connections := 0x0031   ; CSIDL of the Network Connections folder.

CoInitialize()
psh := ActiveXObject("Shell.Application")

pns := Invoke(psh, "Namespace", varg := DispParams(1, varg, 3, CSIDL_Connections))
pitems := Invoke(pns, "Items")

Loop, % Invoke(pitems, "Count")
{
   pid := Invoke(pitems, "Item", varg := DispParams(1, varg, 3, A_Index-1))
   pName := Invoke(pid, "Name")
   Unicode2Ansi(pName, sName)
   SysFreeString(pName)
   If (sName = sConnection)
   {
      bRes := True
      Break
   }
   Release(pid)
}

If !bRes
ExitApp

pverbs := Invoke(pid, "Verbs")
pvb := Invoke(pverbs, "Item", varg := DispParams(1, varg, 3, nVB))

If pvb
{
   pVerbName := Invoke(pvb, "Name")
   Unicode2Ansi(pVerbName, sVerbName)
   SysFreeString(pVerbName)
   If (bEnable && InStr(sVerbName, "&a") || !bEnable && InStr(sVerbName, "&b"))   ; &A and &B can be Language dependent.
   Invoke(pvb, "DoIt")
   WinWait, %sConnection% ahk_class #32770,, 1
   WinWaitClose
   Release(pvb)
}

Release(pverbs)
Release(pid)
Release(pitems)
Release(pns)
Release(psh)
CoUninitialize()
}

and this is the error I get:

Posted Image

PS: I dont think it should work anyway, because dialup connections dont get enabled/disabled, they only connect/disconnect (from context menu)

Tarch
  • Members
  • 87 posts
  • Last active: Mar 24 2009 12:29 AM
  • Joined: 23 Jun 2007
Hi,
you have the include statement in a function:
NetConnect(bEnable = True, sConnection = "TheNameOfMyDialUpConnection")
{
#Include CoHelper.ahk
;SetTitleMatchMode, 2 
I think that the error is that!

Bye! :shock: :)

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
mm, so where should I put it?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

mm, so where should I put it?

OK, I updated the script using the function form. And, I also updated CoHelper.ahk to not specify the nParams, so please recopy it too.

NetConnect(0, "dialup connection")
Sleep, 5000
NetConnect(1, "dialup connection")

BTW, what's the acceleration keys of the context menu names?
You have to replace &a and &b with them. See the comments in the script.

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Interesting. Why do I not see NetConnect in AHK help chm? Is it a variable?

TLM

Posted Image

don't duplicate, iterate!


azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007

mm, so where should I put it?

OK, I updated the script using the function form. And, I also updated CoHelper.ahk to not specify the nParams, so please recopy it too.

NetConnect(0, "dialup connection")
Sleep, 5000
NetConnect(1, "dialup connection")

BTW, what's the acceleration keys of the context menu names?
You have to replace &a and &b with them. See the comments in the script.


this is the error I get now:

Posted Image

1) the key shortcut of context menu for "connect" or "disconnect" is "o"

2) can you add please before the "Sleep, 5000" to execute these commands:
ipconfig /release
ipconfig /renew

thanks

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

this is the error I get now:

So, you never read the posts... Just recopy CoHelper.ahk.

the key shortcut of context menu for "connect" or "disconnect" is "o"

Are you sure? Then replace &a and &b with c&onnect and disc&onnect.