Danyfirex

Enable/Disable Network connection

13 posts in this topic

#1 ·  Posted (edited)

I was needing to enable and disable a network connection. I google and find a good C++ example using interfaces. so I ported to Autoit (ObjCreateInterface  :wub: )

Global Const $NCME_DEFAULT = 0
Global Const $S_OK = 0

Global Const $sCLSID_ConnectionManager = '{BA126AD1-2166-11D1-B1D0-00805FC1270E}'
Global Const $sIID_INetConnectionManager = '{C08956A2-1CD3-11D1-B1C5-00805FC1270E}'
Global Const $sIID_IEnumNetConnection = '{C08956A0-1CD3-11D1-B1C5-00805FC1270E}'
Global Const $sIID_INetConnection = '{C08956A1-1CD3-11D1-B1C5-00805FC1270E}'

Global Const $sINetConnectionManager = "EnumConnections hresult(int;ptr*)"
Global Const $sTag_IEnumNetConnection = "Next hresult(int;ptr*;ulong*)"
Global Const $sTag_INetConnection = "Connect hresult();Disconnect hresult();Delete hresult();Duplicate hresult(wstr;ptr*);GetProperties hresult(ptr)"
Global Const $sTag_NETCON_PROPERTIES = "byte guidId[16];ptr pszwName;ptr pszwDeviceName;dword Status;dword MediaType;dword dwCharacter;byte clsidThisObject[16];byte clsidUiObject[16]"




Func NetWorkEnableDisable($sNetWorkName, $bEnable_Disable = true)
    Local $hResult = 0
    Local $iCount = 0
    Local $pIEnumNetConnection = 0
    Local $oIEnumNetConnection = 0
    Local $pConnection = 0
    Local $tNETCON_PROPERTIES = 0
    Local $tName = 0
    Local $sNetName = ""
    Local $tDeviceName = 0
    Local $oConnection = 0
    Local $pPROPERTIES = 0
    Local $tPtr = 0
    Local $iState=0
    Local $oNetCManager = ObjCreateInterface($sCLSID_ConnectionManager, $sIID_INetConnectionManager, $sINetConnectionManager)


    If IsObj($oNetCManager) Then
        ConsoleWrite("$oNetCManager:" & IsObj($oNetCManager) & @CRLF)

        $oNetCManager.EnumConnections($NCME_DEFAULT, $pIEnumNetConnection)
        If $pIEnumNetConnection Then
            ConsoleWrite("$pIEnumNetConnection: " & $pIEnumNetConnection & @CRLF)
            $oIEnumNetConnection = ObjCreateInterface($pIEnumNetConnection, $sIID_IEnumNetConnection, $sTag_IEnumNetConnection)
            If IsObj($oIEnumNetConnection) Then
                ConsoleWrite("$oIEnumNetConnection: " & IsObj($oIEnumNetConnection) & @CRLF)

                While ($oIEnumNetConnection.Next(1, $pConnection, $iCount) = $S_OK)
                    ConsoleWrite("$pConnection: " & $pConnection & " $iCount: " & $iCount & @CRLF)
                    $oConnection = ObjCreateInterface($pConnection, $sIID_INetConnection, $sTag_INetConnection)

                    If IsObj($oConnection) Then
                        $tNETCON_PROPERTIES = DllStructCreate($sTag_NETCON_PROPERTIES)
                        $tPtr = DllStructCreate("ptr Pointer")
                        ConsoleWrite("$oConnection: " & IsObj($oConnection) & @CRLF)
                        $hResult = $oConnection.GetProperties(DllStructGetPtr($tPtr))
                        If SUCCEEDED($hResult) Then
                            $tNETCON_PROPERTIES = DllStructCreate($sTag_NETCON_PROPERTIES, $tPtr.Pointer)
                            $tName = DllStructCreate("wchar[260]", $tNETCON_PROPERTIES.pszwName)
                            $sNetName = DllStructGetData($tName, 1)
                            If $bEnable_Disable Then
                                If $sNetName = $sNetWorkName Then
                                    If SUCCEEDED($oConnection.Connect()) Then
                                        DllCall("netshell.dll", "none", "NcFreeNetconProperties", "ptr", DllStructGetPtr($tNETCON_PROPERTIES))
                                         Return True
                                    EndIf

                                EndIf
                            Else
                                If $sNetName = $sNetWorkName Then
                                    If SUCCEEDED($oConnection.Disconnect()) Then
                                     DllCall("netshell.dll", "none", "NcFreeNetconProperties", "ptr", DllStructGetPtr($tNETCON_PROPERTIES))
                                     Return True
                                    EndIf
                                EndIf
                            EndIf

                             DllCall("netshell.dll", "none", "NcFreeNetconProperties", "ptr", DllStructGetPtr($tNETCON_PROPERTIES))
                            $tPtr = 0
                            $tName = 0
                            $tNETCON_PROPERTIES = 0
                            $oConnection = 0
                        Else
                            Return False
                        EndIf
                    Else
                        Return False
                    EndIf


                WEnd
            Else
                Return False
            EndIf
        Else
            Return False
        EndIf

    Else
        Return False
    EndIf
EndFunc   ;==>NetWorkEnableDisable


Func SUCCEEDED($hr)
    Return ($hr >= 0)
EndFunc   ;==>SUCCEEDED

Saludos

Edited by Danyfirex
3 people like this

Share this post


Link to post
Share on other sites



Cool. It's better than netsh.

But the depth of the blocks if a little strange. Why not just write If Not .. Then Return False

Share this post


Link to post
Share on other sites

Rewrite it as you want. for that is the source :)

Saludos

Share this post


Link to post
Share on other sites

Sure something like this would come in handy at some point, except it does not appear to work for me, meaning the connection is not disabled, or indeed enabled.

Win 7 32

$oNetCManager:1
$pIEnumNetConnection: 5543260
$oIEnumNetConnection: 1
$pConnection: 5543404 $iCount: 1
$oConnection: 1
$pConnection: 5543404 $iCount: 1
$oConnection: 1

 

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

 

Sure something like this would come in handy at some point, except it does not appear to work for me, meaning the connection is not disabled, or indeed enabled.

Win 7 32

 

 

¿Do you get some return value? I made the code in window 7 x64. Thanks for report. I'll check in Windows x86 Saludos

Very impressive (as usual)!

Thank you for sharing.

Also, better than WMI ....

Thanks jguinch :) Saludos

Share this post


Link to post
Share on other sites

Great job! :thumbsup:


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

 

Sure something like this would come in handy at some point, except it does not appear to work for me, meaning the connection is not disabled, or indeed enabled.

Win 7 32

 

 

I've checked in windows 7 x86 and work correctly.  be sure that your're running with  admin rights. Saludos

Share this post


Link to post
Share on other sites

Yes, my mistake, works just fine using #RequireAdmin.

Thanks.

You're Wellcome :)  :thumbsup:

Saludos

Share this post


Link to post
Share on other sites

That disables all adapters.

 
This is how it is individually done. Hope you still need it or are interested =)
actual cmd.exe commands are in blue. The number you get from your wmic nic get name, index list is x
 
Start elevated Command Prompt. (administrator rights)
 
Get NIC list and index number: (using this commands is what tells you what x you are looking for)
wmic nic get name, index
 
Enable NIC with index number: (for example 7, or 12)
wmic path win32_networkadapter where index=x call enable
 
Disable NIC with index number: (for example 7, or 12)
wmic path win32_networkadapter where index=x call disable
 
for me I wanted to cycle the wireless usb internet connection which was device ID 12.
7 is my LAN adapter

post-89858-0-19131800-1422689374_thumb.p

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