Sign in to follow this  
Followers 0
Jade

Enabling An Interface In Network Connections

9 posts in this topic

Hi,

This is probably something simple that's been done before, but I can't find it in the forum, and I'm new to AutoIt...

I'm trying to write something that'll disable or enable an interface in the Network Connections window.

I got Network Connections to pop up by using Run("rundll32.exe shell32.dll, Control_RunDLL ncpa.cpl").

But once I get there, the AutoIt Window Info doesn't show any control ID for "Local Area Connection", so that means I can't use ControlClick to select that interface, right? So how do I do it? I could use Send("L"), but then what if there are multiple "Local Area Connection *" interfaces? How would I know that I picked the right one (besides verifying the window that pops up after clicking on one of them)?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I found this:

CODE
; Disable and Enable a Network card using 'Shell.Application'

;

; See also: http://blog.mvpcn.net/icuc88/articles/244.aspx

;

; To do: Rewrite this into a UDF. Parameters: $oLanConnection and $bEnabled

$oLanConnection = "Local Area Connection"; Change this to the name of the adapter to be disabled !

$bEnable = true ; Change this to 'false' to DISABLE the network adapter

if @OSType<>"WIN32_NT" then

Msgbox(0,"","This script requires Windows 2000 or higher.")

exit

endif

if @OSVersion="WIN_2000" then

$strFolderName = "Network and Dial-up Connections"

else

$strFolderName = "Network Connections"; Windows XP

endif

Select

Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,_

2409,2809,2c09,3009,3409", @OSLang) ; English (United States)

$strEnableVerb = "En&able"

$strDisableVerb = "Disa&ble"

; Add here the correct Verbs for your Operating System Language

EndSelect

;Virtual folder containing icons for the Control Panel applications. (value = 3)

Const $ssfCONTROLS = 3

$ShellApp = ObjCreate("Shell.Application")

$oControlPanel = $shellApp.Namespace($ssfCONTROLS)

; Find 'Network connections' control panel item

$oNetConnections=""

For $FolderItem in $oControlPanel.Items

If $FolderItem.Name = $strFolderName then

$oNetConnections = $FolderItem.GetFolder

Exitloop

Endif

Next

If not IsObj($oNetConnections) Then

MsgBox(0,"Error","Couldn't find " & $strFolderName & " folder."

Exit

EndIf

For $FolderItem In $oNetConnections.Items

If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then

$oLanConnection = $FolderItem

Exitloop

EndIf

Next

If not IsObj($oLanConnection) Then

MsgBox(0,"Error","Couldn't find " & $oLanConnection & " Item."

Exit

EndIf

$oEnableVerb=""

$oDisableVerb=""

For $Verb In $oLanConnection.Verbs

If $Verb.Name = $strEnableVerb Then

$oEnableVerb = $Verb

EndIf

If $Verb.Name = $strDisableVerb Then

$oDisableVerb = $Verb

EndIf

Next

If $bEnable then

If IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card

Endif

If not $bEnable then

If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card

EndIf

Sleep(1000)

I hope this will solve your problem. I found it here:http://www.autoitscript.com/forum/index.php?showtopic=12645

Edited by vollyman

Share this post


Link to post
Share on other sites

I wrote a script to enable and disable network interfaces using a command line tool called DevCon. It's feely available. It's cryptic and hard to use at first if you're not familiar with the Windows hardware scheme, but it works well.

Share this post


Link to post
Share on other sites

Thanks blindwig, volleyman,

I want to avoid adding DevCon if possible. I've got too many testing tools to deal with right now as it is. :) I will try the code volleyman provided and let you know how it goes. One question about the code though: Does the Control Panel need to be activated before we call this function or does it take care of enabling/disabling the interface even if Control Panel is not activated?

Thanks again.

Share this post


Link to post
Share on other sites

Ok, to answer my own question, no, Control Panel did not need to be activated beforehand. I guess that's the point of using the COM. I finally got it to work for me after fixing a few syntax errors and tweaking it a bit so that I could pass the interface name and the enable/disable option via the command line. I learned a lot while deceiphering this...thanks for sharing it!

Share this post


Link to post
Share on other sites

Ok, to answer my own question, no, Control Panel did not need to be activated beforehand. I guess that's the point of using the COM. I finally got it to work for me after fixing a few syntax errors and tweaking it a bit so that I could pass the interface name and the enable/disable option via the command line. I learned a lot while deceiphering this...thanks for sharing it!

I know that you said that you got it working - but take a look at this:

http://www.autoitscript.com/forum/index.ph...showtopic=21229


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

If you are new to AutoIT and would like to get the basics down quicker, check out this awesome new coaching tool...

And if you find it useful ...vote :)

http://www.autoitscript.com/forum/index.php?showtopic=24117


~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0