AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Enable/Disable a Network Connection
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Sat Apr 28, 2007 7:35 am    Post subject: Enable/Disable a Network Connection Reply with quote

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.

Code:
#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()
}


Last edited by Sean on Sun Jul 22, 2007 12:48 pm; edited 5 times in total
Back to top
View user's profile Send private message
Arnoud



Joined: 24 Mar 2007
Posts: 15

PostPosted: Tue May 01, 2007 5:37 pm    Post subject: Reply with quote

Quote:
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!
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Tue May 01, 2007 11:40 pm    Post subject: Reply with quote

Arnoud wrote:
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.
Back to top
View user's profile Send private message
azure



Joined: 07 Jun 2007
Posts: 296

PostPosted: Wed Jul 04, 2007 4:48 am    Post subject: Reply with quote

very nice script

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

thanks !
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Wed Jul 04, 2007 5:15 am    Post subject: Reply with quote

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

Code:
#Include CoHelper.ahk

NetConnect(0)
Sleep, 5000
NetConnect(1)

NetConnect(bEnable = True, sConnection = "Local Area Connection")
{
...
}
Back to top
View user's profile Send private message
azure



Joined: 07 Jun 2007
Posts: 296

PostPosted: Wed Jul 04, 2007 5:30 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Wed Jul 04, 2007 5:46 am    Post subject: Reply with quote

azure wrote:
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:

Code:
NetConnect(0, "dialup connection")
Sleep, 5000
NetConnect(1, "dialup connection")
Back to top
View user's profile Send private message
Thalon



Joined: 12 Jul 2005
Posts: 640

PostPosted: Wed Jul 04, 2007 8:43 am    Post subject: Reply with quote

Seems to need more changes for german windows.
A simple change of sConnection (and bEnable) didn't help here...
_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum
SacredVault
Back to top
View user's profile Send private message
azure



Joined: 07 Jun 2007
Posts: 296

PostPosted: Wed Jul 04, 2007 9:23 am    Post subject: Reply with quote

this is my code:

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:



PS: I dont think it should work anyway, because dialup connections dont get enabled/disabled, they only connect/disconnect (from context menu)
Back to top
View user's profile Send private message
Tarch



Joined: 23 Jun 2007
Posts: 87

PostPosted: Wed Jul 04, 2007 9:43 am    Post subject: Reply with quote

Hi,
you have the include statement in a function:
Code:

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

I think that the error is that!

Bye! Shocked Smile
Back to top
View user's profile Send private message
azure



Joined: 07 Jun 2007
Posts: 296

PostPosted: Wed Jul 04, 2007 10:30 pm    Post subject: Reply with quote

mm, so where should I put it?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Thu Jul 05, 2007 2:24 am    Post subject: Reply with quote

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

Code:
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.
Back to top
View user's profile Send private message
TheLaughingMan



Joined: 21 Aug 2006
Posts: 41

PostPosted: Thu Jul 05, 2007 2:45 am    Post subject: Reply with quote

Interesting. Why do I not see NetConnect in AHK help chm? Is it a variable?

TLM
_________________
Back to top
View user's profile Send private message
azure



Joined: 07 Jun 2007
Posts: 296

PostPosted: Thu Jul 05, 2007 5:43 am    Post subject: Reply with quote

Sean wrote:
azure wrote:
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.

Code:
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:



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
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1323

PostPosted: Thu Jul 05, 2007 5:53 am    Post subject: Reply with quote

azure wrote:
this is the error I get now:

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

Quote:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group