Need to disable & enable a hardware device.
Need to disable & enable a hardware device.
I searched, but only a post from 2011 -- using devcon.exe, the only '64 bit' version is for Itanium, the 32bit seems to fail for me.
My laptop, the aggravating pos that it is, if the laptop suspends or hibernates -- I cannot get back on the network. I only have two choices: reboot (easy/slow) or disable the hardware device for wifi, re-enable, connect (cumbersome, fast). w/o using autoitscript, I'm hoping there's a way (batch file, or programmatically, which is preferred) to automate this so I can just click on an short cut and have it all done for me.
Devcon (32bit) fails to disable the device. (docs say it doesn't do everything on x64), and the api's I've found (DeviceProperties,DevicePropertiesEx,&DeviceAdvancedProperties) all seem to be undocumented.
Does anyone have a tip to get it done?
My laptop, the aggravating pos that it is, if the laptop suspends or hibernates -- I cannot get back on the network. I only have two choices: reboot (easy/slow) or disable the hardware device for wifi, re-enable, connect (cumbersome, fast). w/o using autoitscript, I'm hoping there's a way (batch file, or programmatically, which is preferred) to automate this so I can just click on an short cut and have it all done for me.
Devcon (32bit) fails to disable the device. (docs say it doesn't do everything on x64), and the api's I've found (DeviceProperties,DevicePropertiesEx,&DeviceAdvancedProperties) all seem to be undocumented.
Does anyone have a tip to get it done?
Re: Need to disable & enable a hardware device.
Hi jassing,
here an example that enable disable Network adapters over WMI ComatePlus is needed.
Best
jpd
here an example that enable disable Network adapters over WMI ComatePlus is needed.
Best
jpd
Code: Select all
Enumeration
#Window_0
#popup_disable
#popup_enable
#Combo_0
#Text_0
#Listview_0
#Button_scan
#Button_Close
#Button_disable
#Button_enable
#view_disable
EndEnumeration
Structure TCPV4_Setting
IPAddressV4.s [8] ;8 IP4 +8 IP6
SubNetV4.s[8]
Gateway.s[8]
DNS.s[8]
EndStructure
Structure TCPV6_Setting
IPAddressV6.s [8]
SubNetV6.s[8]
Gateway.s[8]
DNS.s[8]
EndStructure
#tcp_type_IP=0 ; IP
#tcp_type_SN=1 ;subnet
#tcp_type_GW=2 ;Gateway
#tcp_type_DNS=3 ; DNS
Structure Net_config_adapter
NetConnectID.s
Index.s
MACAddress.s
TCPV4.TCPV4_Setting;\IPAddressV4
TCPV6.TCPV6_Setting
Description.s
SettingID.s
AdapterType.s
Status.s
StatusInfo.w
AdapterTypeID.l
NetConnectionStatus.s
EndStructure
Global NewList ConfigNet.Net_config_adapter()
Global menuenable.l=0
;IncludePath "..\..\"
XIncludeFile "COMatePlus.pbi"
XIncludeFile "VariantHelper_Include.pb"
Declare GetStringfromVariant(*tosearch.VARIANT,tcp_type.l)
Procedure StringToBStr(string$)
Protected Unicode$ = Space(StringByteLength(string$, #PB_Unicode) + 1)
Protected bstr_string.l
PokeS(@Unicode$, String$, -1, #PB_Unicode)
bstr_string = SysAllocString_(@Unicode$)
ProcedureReturn bstr_string
EndProcedure
Procedure Open_Win()
If OpenWindow(#Window_0, 500, 300, 640, 480, "Win32_NetworkAdapter", #PB_Window_SystemMenu |#PB_Window_ScreenCentered| #PB_Window_SizeGadget | #PB_Window_TitleBar )
ListIconGadget(#Listview_0, 20, 60, 600, 300,"Index",40)
AddGadgetColumn(#Listview_0, 1, "NetConnectID", 560/5)
AddGadgetColumn(#Listview_0, 3, "MACAddress", 560/5)
AddGadgetColumn(#Listview_0, 4, "IP Adress", 560/5)
AddGadgetColumn(#Listview_0, 2,"Description" , 560/5)
AddGadgetColumn(#Listview_0, 5, "Connect State", 560/5)
ButtonGadget(#Button_scan,20,400,50,20,"Scan")
ButtonGadget(#Button_Close,570,400,50,20,"Close")
CheckBoxGadget(#view_disable, 260,400,180, 20,"View disabled Network Adapter")
EndIf
EndProcedure
Procedure Disable_Adapter(NetLuidIndex.l)
Define.COMateObject objWMIService, Adapter
colAdapters.COMateEnumObject
strComputer.s = "."
Net_Luid_Index$ = Str(NetLuidIndex)
objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "")
If objWMIService
colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapter Where Index= $0027" + Net_Luid_Index$ +"$0027')")
If colAdapters
Adapter= colAdapters\GetNextObject()
While Adapter
result.l = Adapter\Invoke("Disable()")
Adapter\Release()
Adapter = colAdapters\GetNextObject()
Wend
colAdapters\Release()
EndIf
objWMIService\Release()
EndIf
ProcedureReturn result ; if completed OK result returns 0
EndProcedure
Procedure Enable_Adapter(NetLuidIndex.l)
Define.COMateObject objWMIService, Adapter
colAdapters.COMateEnumObject
strComputer.s = "."
Net_Luid_Index$ = Str(NetLuidIndex)
objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "")
If objWMIService
colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapter Where Index= $0027" + Net_Luid_Index$ +"$0027')")
If colAdapters
Adapter= colAdapters\GetNextObject()
While Adapter
result.l = Adapter\Invoke("Enable()")
Adapter\Release()
Adapter = colAdapters\GetNextObject()
Wend
colAdapters\Release()
EndIf
objWMIService\Release()
EndIf
ProcedureReturn result ; if completed OK result returns 0
EndProcedure
Procedure.s Net_Connection_Status(NetConnectionStatus.l)
Select NetConnectionStatus
Case 0 ;0x0
ProcedureReturn "Disconnected"
Case 1 ;0x1
ProcedureReturn "Connecting"
Case 2 ;0x2
ProcedureReturn "Connected"
Case 3 ;0x3
ProcedureReturn "Disconnecting"
Case 4 ;0x4
ProcedureReturn "Hardware Not present"
Case 5 ;0x5
ProcedureReturn "Hardware disabled"
Case 6 ;0x6
ProcedureReturn "Hardware malfunction"
Case 7 ;0x7
ProcedureReturn "Media disconnected"
Case 8 ;0x8
ProcedureReturn "Authenticating"
Case 9 ;0x9
ProcedureReturn "Authentication succeeded"
Case 10 ;0xA
ProcedureReturn "Authentication failed"
Case 11 ;0x0B
ProcedureReturn "Invalid address"
Case 12 ;0xC
ProcedureReturn "Credentials required"
EndSelect
EndProcedure
Procedure GetStringfromVariant(*tosearch.VARIANT,tcp_type.l)
*sa.SafeArray
*varIP.VARIANT
If *tosearch\vt <> #VT_NULL
*sa = *tosearch\parray
IPv4.l=0
IPv6.l=0
For i = saLBound(*sa) To saUBound(*sa)
*varIP = SA_VARIANT(*sa, i)
If *varIP\vt <> #VT_BSTR
VariantChangeType_(*varIP, *varIP, 0, #VT_BSTR)
EndIf
Debug PeekS(*varIP\bstrVal, -1, #PB_Unicode)
tmpstring.s=PeekS(*varIP\bstrVal, -1, #PB_Unicode)
Select tcp_type
Case #tcp_type_IP
If tmpstring <>"" ;And tcp_type=0
If FindString(tmpstring,".",1)
ConfigNet()\TCPV4.TCPV4_Setting\IPAddressV4[IPv4]=tmpstring
IPv4=IPv4+1
Else
ConfigNet()\TCPV6.TCPV6_Setting\IPAddressV6[IPv6]=tmpstring
IPv6=IPv6+1
EndIf
EndIf
Case #tcp_type_SN
If tmpstring <>"" ;And tcp_type=0
If FindString(tmpstring,".",1)
ConfigNet()\TCPV4.TCPV4_Setting\SubNetV4[IPv4]=tmpstring
IPv4=IPv4+1
Else
ConfigNet()\TCPV6.TCPV6_Setting\SubNetV6[IPv6]=tmpstring
IPv6=IPv6+1
EndIf
EndIf
Case #tcp_type_GW
If tmpstring <>"" ;And tcp_type=0
If FindString(tmpstring,".",1)
ConfigNet()\TCPV4.TCPV4_Setting\Gateway[IPv4]=tmpstring
IPv4=IPv4+1
Else
ConfigNet()\TCPV6.TCPV6_Setting\Gateway[IPv6]=tmpstring
IPv6=IPv6+1
EndIf
EndIf
Case #tcp_type_DNS
If tmpstring <>"" ;And tcp_type=0
If FindString(tmpstring,".",1)
ConfigNet()\TCPV4.TCPV4_Setting\DNS[IPv4]=tmpstring
IPv4=IPv4+1
Else
ConfigNet()\TCPV6.TCPV6_Setting\DNS[IPv6]=tmpstring
IPv6=IPv6+1
EndIf
EndIf
EndSelect
VariantClear_(*varIP)
Next
saFreeSafeArray(*sa)
EndIf
VariantClear_(*tosearch) : FreeMemory(*tosearch)
EndProcedure
Procedure CIM_Net_Cfg()
Define.COMateObject objWMIService, NetCfg ,NetAdapter
colNetCfg.COMateEnumObject
Define colNetAdapter.COMateEnumObject
Define *IPAddress.VARIANT,*ConnectID.VARIANT
strComputer.s = "."
objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "")
If objWMIService
colNetCfg = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapter')")
If colNetCfg
NetCfg= colNetCfg\GetNextObject()
While NetCfg
*ConnectID= NetCfg\GetvariantProperty("NetConnectionID")
If PeekL(*ConnectID)=8 Or PeekL(*ConnectID)=1
AddElement(ConfigNet())
If PeekL(*ConnectID)=8
sConnectID.s= NetCfg\GetstringProperty("NetConnectionID")
ConfigNet()\NetConnectID=sConnectID
Else
ConfigNet()\NetConnectID="n/a"
EndIf
ConfigNet()\Index=Str(NetCfg\GetintegerProperty("Index"))
ConfigNet()\MACAddress=NetCfg\GetstringProperty("MACAddress")
ConfigNet()\AdapterType=NetCfg\GetstringProperty("AdapterType")
ConfigNet()\Status=NetCfg\GetstringProperty("Status")
ConfigNet()\StatusInfo=PeekW(NetCfg\GetvariantProperty("StatusInfo"))
ConfigNet()\AdapterTypeID=NetCfg\GetIntegerProperty("AdapterTypeID")
ConfigNet()\NetConnectionStatus=Net_Connection_Status(NetCfg\GetIntegerProperty("NetConnectionStatus"))
EndIf
NetCfg\Release()
NetCfg = colNetCfg\GetNextObject()
Wend
colNetCfg\Release()
EndIf
ResetList(ConfigNet())
colNetAdapter = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapterConfiguration')")
If colNetAdapter
NetAdapter= colNetAdapter\GetNextObject()
While NetAdapter
NextElement(ConfigNet())
*IPAddress= NetAdapter\GetvariantProperty("IPAddress")
*IPSubnet=NetAdapter\GetvariantProperty("IPSubnet")
*DefaultIPGateway=NetAdapter\GetvariantProperty("DefaultIPGateway")
*DNSServerSearchOrder=NetAdapter\GetvariantProperty("DNSServerSearchOrder")
GetStringfromVariant(*IPAddress,#tcp_type_IP)
GetStringfromVariant(*IPSubnet,#tcp_type_SN)
GetStringfromVariant(*DefaultIPGateway,#tcp_type_GW)
GetStringfromVariant(*DNSServerSearchOrder,#tcp_type_DNS)
Description.s= NetAdapter\GetstringProperty("Description")
ConfigNet()\Description=Description
SettingID.s=NetAdapter\GetstringProperty("SettingID")
ConfigNet()\SettingID=SettingID
NetAdapter\Release()
NetAdapter = colNetAdapter\GetNextObject()
Wend
colNetAdapter\Release()
EndIf
objWMIService\Release()
EndIf
EndProcedure
;-start
Open_Win()
If CreatePopupMenu(#popup_enable)
MenuItem(1,"enable")
EndIf
If CreatePopupMenu(#popup_disable)
MenuItem(1,"disable")
EndIf
Repeat
event=WaitWindowEvent()
Select Event
Case #PB_Event_Menu
MenuID = EventMenu()
Debug "Menuid: "+Str(Menuid)
Select MenuID
Case 1
If menuenable=1
Debug "menu enable"
Enable_Adapter(Net_Luid_Index)
Else
Debug "menu disable"
Disable_Adapter(Net_Luid_Index)
EndIf
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #Listview_0
gad.s=GetGadgetItemText(#Listview_0 ,GetGadgetState(#Listview_0 ),0)
GadgetToolTip(#Listview_0 , gad)
MB.l=EventType() ;<--- for checking if was pushed RMB or LMB, ...
GetCursorPos_(@var.TV_HITTESTINFO\pt) ;get mousepointer position screen coordenates
ScreenToClient_(GadgetID(#Listview_0 ),@var.TV_HITTESTINFO\pt) ;convert that coordenates to the Gadget 1 (TreeView Gadget) referenced by
SendMessage_(GadgetID(#Listview_0 ),#TVM_HITTEST,0,var) ;this is to know what is the item i am pointing.
SendMessage_(GadgetID(#Listview_0 ),#TVM_SELECTITEM,#TVGN_CARET,var\hItem);<-- and this selects the pointed item
itemsel=GetGadgetState(#Listview_0 ) ;save selected item
Select MB
Case #PB_EventType_RightClick
If GetGadgetState(#Listview_0 ) =>0
mac.s=GetGadgetItemText(#Listview_0 ,GetGadgetState(#Listview_0 ),3)
sNetConnectID.s=GetGadgetItemText(#Listview_0 ,GetGadgetState(#Listview_0 ),1)
Net_Luid_Index=Val(GetGadgetItemText(#Listview_0 ,GetGadgetState(#Listview_0 ),0))
If mac <> "" And sNetConnectID <> "n/a"
Debug "gad: "+gad
Debug Net_Luid_Index
Debug snetConnectID
DisplayPopupMenu(#popup_disable,WindowID(#Window_0))
menuenable=0
ElseIf mac = "" And sNetConnectID <> "n/a"
DisplayPopupMenu(#popup_enable,WindowID(#Window_0))
menuenable=1
EndIf
EndIf
EndSelect
Case #Button_scan
ClearGadgetItems(#Listview_0)
ClearList(ConfigNet())
CIM_Net_Cfg()
ResetList(ConfigNet())
x2=0
While NextElement(ConfigNet())
If Not ConfigNet()\NetConnectionStatus = "Disconnected" ;And GetGadgetState(#view_disable)=1
AddGadgetItem(#Listview_0,-1,ConfigNet()\Index)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectID,1)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\Description,2)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\MACAddress,3)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\TCPV4.TCPV4_Setting\IPAddressV4,4)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectionStatus,5)
x2=x2+1
ElseIf GetGadgetState(#view_disable)=1
AddGadgetItem(#Listview_0,-1,ConfigNet()\Index)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectID,1)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\Description,2)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\MACAddress,3)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\TCPV4.TCPV4_Setting\IPAddressV4,4)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectionStatus,5)
x2=x2+1
EndIf
Wend
Case #Button_disable
Disable_Adapter(17)
Case #Button_enable
Enable_Adapter(17)
Case #view_disable
If GetGadgetState(#view_disable) And ListSize(ConfigNet())
ClearGadgetItems(#Listview_0)
ResetList(ConfigNet())
x2=0
While NextElement(ConfigNet())
AddGadgetItem(#Listview_0,-1,ConfigNet()\Index)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectID,1)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\Description,2)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\MACAddress,3)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\TCPV4.TCPV4_Setting\IPAddressV4,4)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectionStatus,5)
x2=x2+1
Wend
EndIf
If Not GetGadgetState(#view_disable) And ListSize(ConfigNet())
ClearGadgetItems(#Listview_0)
ResetList(ConfigNet())
x2=0
While NextElement(ConfigNet())
If Not ConfigNet()\NetConnectionStatus = "Disconnected"
AddGadgetItem(#Listview_0,-1,ConfigNet()\Index)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectID,1)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\Description,2)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\MACAddress,3)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\TCPV4.TCPV4_Setting\IPAddressV4,4)
SetGadgetItemText(#Listview_0,x2,ConfigNet()\NetConnectionStatus,5)
x2=x2+1
EndIf
Wend
EndIf
Case #PB_Event_Menu
Case #Button_Close
End
Select EventMenu()
EndSelect
Case #PB_Event_CloseWindow
End
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
End
PB 5.10 Windows 7 x64 SP1
-
- Enthusiast
- Posts: 112
- Joined: Wed May 13, 2009 8:38 am
- Location: Arizona, USA
Re: Need to disable & enable a hardware device.
You can obtain the Windows 7 or 8 version of devcon including source code in the Windows Driver Kitjassing wrote:I searched, but only a post from 2011 -- using devcon.exe, the only '64 bit' version is for Itanium, the 32bit seems to fail for me.
I copied mine to the system32 folder so I can access it easily from the command line.
The C++ source code should help to understand the api's.
Path of the installed Windows 8 version
C:\Program Files (x86)\Windows Kits\8.0\Tools\x64\devcon.exe
Windows Driver Kit (WDK)
http://msdn.microsoft.com/en-us/windows ... 87428.aspx
1.) Find network adapterDevcon (32bit) fails to disable the device. (docs say it doesn't do everything on x64), and the api's I've found (DeviceProperties,DevicePropertiesEx,&DeviceAdvancedProperties) all seem to be undocumented.
Does anyone have a tip to get it done?
devcon hwids =Net
(example yours will differ)
Code: Select all
PCI\VEN_8086&DEV_1503&SUBSYS_849C1043&REV_04\3&11583659&0&C8
Name: Intel(R) 82579V Gigabit Network Connection
devcon find *DEV_1503
Code: Select all
PCI\VEN_8086&DEV_1503&SUBSYS_849C1043&REV_04\3&11583659&0&C8: Intel(R) 82579V Gigabit Network Connection
1 matching device(s) found.
devcon disable *DEV_1503
Code: Select all
PCI\VEN_8086&DEV_1503&SUBSYS_849C1043&REV_04\3&11583659&0&C8: Disabled
1 device(s) disabled.
devcon enable *DEV_1503
Code: Select all
PCI\VEN_8086&DEV_1503&SUBSYS_849C1043&REV_04\3&11583659&0&C8: Enabled
1 device(s) are enabled.
Re: Need to disable & enable a hardware device.
Thank you for the code!
Re: Need to disable & enable a hardware device.
Well; I tried the code "as is" (fixing some duplicate structure definitions, and native pointer types):jpd wrote:Hi jassing,
here an example that enable disable Network adapters over WMI ComatePlus is needed.
On x64 it worked w/o an issue
on x86 (two systems) and I get an IMA on:
Code: Select all
SetGadgetItemText(#Listview_0,x2,ConfigNet()\MACAddress,3)
Re: Need to disable & enable a hardware device.
Hi jassing,
I have now installed 5.20 b5 and not need change somethings on the Comateplus include for use the sample. strange ....
on my test with XP SP3 when listing the adapter occure an IMA.
this can fixed, change the line on the procedure GetStringformVariant
to
sure that you use the actually ComatePlus Version?jassing wrote:
Well; I tried the code "as is" (fixing some duplicate structure definitions, and native pointer types):
...tested pb 5.11 and pb5.2
I have now installed 5.20 b5 and not need change somethings on the Comateplus include for use the sample. strange ....
on my test with XP SP3 when listing the adapter occure an IMA.
this can fixed, change the line on the procedure GetStringformVariant
Code: Select all
VariantClear_(*tosearch) : FreeMemory(*tosearch)
Code: Select all
FreeMemory(*tosearch)
PB 5.10 Windows 7 x64 SP1
Re: Need to disable & enable a hardware device.
yes, absolutely, how would it have gotten that far if it couldn't include the commateplus files?jpd wrote: sure that you use the actually ComatePlus Version?
Thanks; that fixed it on win2003! Thanks.jpd wrote:toCode: Select all
VariantClear_(*tosearch) : FreeMemory(*tosearch)
Code: Select all
FreeMemory(*tosearch)
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Need to disable & enable a hardware device.
Trying this code on WinXP with PB5.30 but I get a compiler error at this ComatePlus procedure:
This line:
errorCode = COMatePLUS_CompileSubobjectInvokation(*hStatement, subObject, @parse())
"Bad parameter type: an array is expected."
Code: Select all
Procedure.i COMate_INTERNAL_PrepareStatement(command$, *ptrStatement.INTEGER)
Protected errorCode = #S_OK, *hStatement._COMatePLUSStatement
Protected Dim parse._COMateParse(#COMate_MAXNUMSUBOBJECTS), i, subObject
If command$
;Allocate memory for a statement handle.
*hStatement = AllocateMemory(SizeOf(_COMatePLUSStatement))
If *hStatement
;Tokenise the command string.
*hStatement\numSubObjects = COMatePLUS_TokeniseCommand(command$, "(),\'= ", parse())
If *hStatement\numSubObjects
For subObject = 1 To *hStatement\numSubObjects
;We need to parse/compile the tokenised command corresponding to each individual sub-object.
errorCode = COMatePLUS_CompileSubobjectInvokation(*hStatement, subObject, @parse())
If errorCode <> #S_OK
COMate_FreeStatementHandle(*hStatement)
Break
EndIf
Next
If errorCode = #S_OK
*ptrStatement\i = *hStatement
EndIf
Else
FreeMemory(*hStatement)
errorCode = #E_INVALIDARG
EndIf
Else
errorCode = #E_OUTOFMEMORY
EndIf
Else
errorCode = #E_INVALIDARG
EndIf
ProcedureReturn errorCode
EndProcedure
errorCode = COMatePLUS_CompileSubobjectInvokation(*hStatement, subObject, @parse())
"Bad parameter type: an array is expected."
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.