Popular Post Danyfirex Posted November 22, 2014 Popular Post Share Posted November 22, 2014 (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 ) expandcollapse popupGlobal 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 November 22, 2014 by Danyfirex Inververs, kid1232, JohnOne and 2 others 5 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Inververs Posted November 23, 2014 Share Posted November 23, 2014 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 Link to comment Share on other sites More sharing options...
Danyfirex Posted November 23, 2014 Author Share Posted November 23, 2014 Rewrite it as you want. for that is the source Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
JohnOne Posted November 23, 2014 Share Posted November 23, 2014 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. Link to comment Share on other sites More sharing options...
jguinch Posted November 23, 2014 Share Posted November 23, 2014 Very impressive (as usual)! Thank you for sharing. Also, better than WMI .... Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Danyfirex Posted November 24, 2014 Author Share Posted November 24, 2014 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 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
MikahS Posted November 24, 2014 Share Posted November 24, 2014 Great job! Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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 Link to comment Share on other sites More sharing options...
Danyfirex Posted November 24, 2014 Author Share Posted November 24, 2014 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 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
JohnOne Posted November 24, 2014 Share Posted November 24, 2014 I've checked in windows 7 x86 and work correctly. be sure that your're running with admin rights. Saludos Yes, my mistake, works just fine using #RequireAdmin. Thanks. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Danyfirex Posted November 24, 2014 Author Share Posted November 24, 2014 Yes, my mistake, works just fine using #RequireAdmin. Thanks. You're Wellcome Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
ricky Posted November 25, 2014 Share Posted November 25, 2014 I found my best solution : Site web : https://social.technet.microsoft.com/Forums/windows/en-US/b4a3c5ad-6c5d-4d83-bcd6-df12d8aea5a1/enabledisable-all-network-connections-using-cmd Disable: wmic path win32_networkadapter where PhysicalAdapter=True call disable Enable: wmic path win32_networkadapter where PhysicalAdapter=True call enable Link to comment Share on other sites More sharing options...
JohnOne Posted November 25, 2014 Share Posted November 25, 2014 That disables all adapters. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
pSKratCH Posted January 31, 2015 Share Posted January 31, 2015 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now