Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

LAN-Switcher v1.14


  • Please log in to reply
15 replies to this topic
niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005

Hi,

some time ago I looked for a tool which will check a given list of netork cards in a computer if the LAN cable is plugged in. Depending of the cable status one or more other network connections should be en- or disabled.

I found some pieces of code which I could pack together and wrote the rest ofr this to work.

I need this tool because we have some WLAN access points and notebooks with port replicators. The replicator is connect to the local ethernet network. The WLAN access points and the Ethernet switches share the same IP domain so having a notebook with both active Ethernet and WLAN may cause trouble. The notebooks have a switch to turn WLAN off but sometimes someone will forget this. Our older DELL notebooks have a driver which will provide this function. But the newer notebooks wont.


You also have to download the COM.zip from this page COM Standard Library and extract com.ahk into the same directory you put LANSwitcher.ahk.

LANSwitcher.ahk:

; LAN-Switcher
; AutoHotkey Version: 1.0.47.06
; Language:       English / German
; Platform:       Windows XP, Windows 7
; Author:         Nils Winkler <nils.winkler@rheinchemie.com>
;
; Script Function:
; Load hardware ID's from the ini file, check network connections for unplugged
; cables and enable or disable other network connections.
;
; used scripts from www.autohotkey.com
; http://www.autohotkey.com/forum/topic18720.html
; http://www.autohotkey.com/forum/topic39033.html
;
; Using the COM.ahk script found here:
; http://www.autohotkey.com/forum/topic22923.html
;
; 2009-01-09: version 1.00
; 2009-01-14: version 1.01: reload after standby and hibernation
; 2009-03-06: version 1.02: - fixed a bug with more than one entry in ini file
;                           - show configuration entry for the tray icon added
;                           - added autorun entry (enable / disable and a part in the ini file
;                             for just display autorun status)
;                           - added ini parameter for allowing exiting this program
; 2009-03-10: version 1.03: - fixed a bug with reloading the script after wakeup from standby
; 2009-04-03: version 1.04: - fixed a bug with recognizing the cable status and WLAN enabling after restart
; 2009-04-17: version 1.05: restart after standby or hibernation using goto
; 2009-11-24: version 1.06: support for Windows 7
; 2010-01-14: version 1.07: - change "start on logon" for Windows 7 using task scheduler
;                           - check for administrative rights on start
; 2010-04-20: version 1.10: requires Windows 7
;                           - using netsh.exe for enabling / disabling the network interface
;                           - using WMI queries for detecting the LAN / wired and wireless interfaces
;                           - more details on the info message box
; 2010-04-27: version 1.11: - support for Modems
;                           - display all devices (Modem, LAN and WLAN)
;                           - auto-detect all or configured LAN devices and en- or disable WLAM and Modems
;                           - auto-detect all or configured WLAN connections if possible and enable or disable Modems
;                           - run as scheduled task as system or install as service
;                           - run after user log in
;                           - enable or disable program termination using a config tool
; 2010-05-18: version 1.12: - enable or disable connections after program start
; 2010-06-02: version 1.13: - enable or disable connections after program start does not work as expected, fixing
; 2012-07-23: version 1.14: - Modified XML file for task creation


; Commands for enabling / disabling interfaces
; %SystemRoot%\System32\netsh.exe interface set interface name="<Connection name>" admin=disabled
; %SystemRoot%\System32\netsh.exe interface set interface name="<Connection name>" admin=enabled

; WMI Query for detecting physical ethernet interfaces (Namespace: Root\CIMV2)
; SELECT Description, DeviceID, NetConnectionID FROM Win32_NetworkAdapter WHERE PhysicalAdapter=True AND NOT(PNPDeviceID LIKE 'ROOT\\NET\\%')
; AdapterType='Ethernet 802.3' will not work if an adapter is disabled
; NOT(PNPDeviceID LIKE 'ROOT\\NET\\%') for ignoring the Cisco VPN adapter


;--------------------------------------------

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Singleinstance force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#Include %A_ScriptDir%\COM.ahk

Start:
; configuration file
INIFILE = LANSwitcher.ini

; AHK min. version required
MINAHKVERSION = 1.0.47.00

; LAN-Switcher version
VERSION = 1.14

If 0 = 2
{
 dump = %1%
 If( dump = "dump" )
 {
  dump = %2%
  IfExist, %dump%
   FileDelete, %dump%
  FileAppend,LANSwitcher dump`n,%dump%
  FileAppend,----------------`n,%dump%
  FileAppend,Version: %Version%`n,%dump%
  FileAppend,AHK version: %A_AhkVersion%`n,%dump%
 }
 Else
  dump =
}

; detect Windows GUI language, for a MUI installation other keys were used
RegRead, GUI_Language, HKEY_CURRENT_USER, Control Panel\Desktop, MultiUILanguageId
If ErrorLevel
 RegRead, GUI_Language, HKEY_CURRENT_USER, Control Panel\International, Locale
 
StringRight, GUI_Language, GUI_Language, 4 ; the string has 8 characters
If GUI_Language in 0407,0807,0c07,1007,1407 ; german
{
 MSG_LANG = German
 MSG_EXITPROGRAM = LAN-Switcher beenden
 MSG_TRAYTOOLTIP = LAN-Switcher
 MSG_TRAYTIPENABLING = aktiviere
 MSG_TRAYTIPDISABLING = deaktiviere
 MSG_SHOWCONFIG = Konfiguration anzeigen
 MSG_CONFIGLANCHECK = Überwache folgende Netzwerkschnittstelle(n):
 MSG_CONFIGLANSWITCH = Schalte folgende Netzwerkverbindung(en) um:
 MSG_AUTORUN = automatisch mit Windows starten
 MSG_ABOUT = Über LAN-Switcher
 MSG_ABOUTTEXT = LAN-Switcher v %VERSION%
 MSG_ERROR = Fehler:
 MSG_ONLYW7 = Das Programm benötigt min. Windows 7.
 MSG_AHKVERSION = Das Programm benötigt min.`nAutoHotKey Version %MINAHKVERSION%.
 MSG_INIFILEDOESNOTEXIST = Die Konfigurationsdatei`n`"%INIFILE%`" wurde nicht gefunden.
 MSG_LANCHECKCOUNTZERO = Keine passende Netzwerkkarten gefunden.
 MSG_NOADMIN = Dieses Programm muß mit Administratoren-Rechten gestartet werden.
 MSG_ERRORCREATINGAUTORUN = Fehler beim Erstellen der Aufgabe für den automatischen Start.`nBitte prüfen Sie die Datei:
 MSG_ACTIVE = aktiv
 MSG_CONNECTED = verbunden
 MSG_NOT = nicht
 MSG_ENABLEWLANONEXIT = WLAN bei Programmende aktivieren
}
;Else If GUI_Language in 0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409 ; english
Else
{
 MSG_LANG = English
 MSG_EXITPROGRAM = Exit LAN switcher
 MSG_TRAYTOOLTIP = LAN switcher
 MSG_TRAYTIPENABLING = enabling
 MSG_TRAYTIPDISABLING = disabling
 MSG_SHOWCONFIG = Show configuration
 MSG_CONFIGLANCHECK = Monitoring the following network interface(s):
 MSG_CONFIGLANSWITCH =  Switching the follwing network connection(s):
 MSG_AUTORUN = start with Windows automatically
 MSG_ABOUT = About LAN switcher
 MSG_ABOUTTEXT = LAN switcher v %VERSION%
 MSG_ERROR = Error:
 MSG_ONLYW7 = This prorgam needs at least Windows 7.
 MSG_AHKVERSION = This program requires at least`nAutoHotKey version %MINAHKVERSION%.
 MSG_INIFILEDOESNOTEXIST = The configuration file "%INIFILE%" could not be found.
 MSG_LANCHECKCOUNTZERO = No matching network interfaces found.
 MSG_NOADMIN = This program has to be started with administrative rights.
 MSG_ERRORCREATINGAUTORUN = Error while creating task for automatically start.`nPlease check the file:
 MSG_ACTIVE = active
 MSG_CONNECTED = connected
 MSG_NOT = not
 MSG_ENABLEWLANONEXIT = Enable WLAN on exit
}

If (dump <> "")
 FileAppend,Language: %MSG_LANG%`n,%dump%

LANSwtichTask = LANSwitcher

If Not A_IsAdmin
{
 If (dump <> "")
  FileAppend,Admin: %A_IsAdmin%`n,%dump%
 MsgBox, %MSG_NOADMIN%
 ExitApp
}


;---------------------------------------------------------------
; do some error checks
;---------------------------------------------------------------
; only tested with Windows 7
If A_OSVersion <> WIN_VISTA
{
 If (dump <> "")
  FileAppend,OS: %A_OSVersion%`n,%dump%
 MsgBox, 16, %MSG_ERROR%, %MSG_ONLYW7%
 ExitApp
}

; check the AutoHotKey version
If A_AhkVersion < %MINAHKVERSION%
{
 MsgBox, 16, %MSG_ERROR%, %MSG_AHKVERSION%
 ExitApp
}

; check if the ini file exist
IfNotExist, %INIFILE%
{
 If (dump <> "")
  FileAppend,ini file: %INIFILE%`n,%dump%
 MsgBox, 16, %MSG_ERROR%, %MSG_INIFILEDOESNOTEXIST%
 ExitApp
}

; initialize COM
COM_Init()


;-----------------------------------------------
; read the network interfaces using a WMI query
;-----------------------------------------------
NICList := WMI_Query( "\\.\root\CIMV2", "Win32_NetworkAdapter WHERE PhysicalAdapter=True AND NOT(PNPDeviceID LIKE 'ROOT\\NET\\%')", "Description|DeviceID|NetConnectionID" )
LoopDevicesKey = SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}

LANCheckCount := 0
LANSwitchCount := 0

Loop, Parse, NICList, "`n"
{
 If( A_LoopField <> "" )
 {
  StringSplit, NIC, A_LoopField, "|"

  regSubKey := "0000" . NIC2
  StringRight, regSubKey, regSubKey, 4
  regSubKey := LoopDevicesKey . "\" . regSubKey

  ifType := 0
  RegRead, ifType, HKLM, %regSubKey%, *IfType

  If( ERRORLEVEL = 0 )
  {
   If( ifType = 6 )
   {
    ; LAN interface
    LANCheckCount := LANCheckCount + 1
    LANCheckInterface%LANCheckCount% = %NIC1%
    LANCheckConnection%LANCheckCount% = %NIC3%
    LANCheckPlugged%LANCheckCount% := 0
    If (dump <> "")
    {
     FileAppend,LAN interface %LANCheckCount%: %NIC1%`n,%dump%
     FileAppend,LAN connection %LANCheckCount%: %NIC3%`n,%dump%
    }
   } else if( ifType = 71 ) {
    ; WLAN interface
    LANSwitchCount := LANSwitchCount + 1
    LANSwitchInterface%LANSwitchCount% = %NIC1%
    LANSwitchConnection%LANSwitchCount% = %NIC3%
    LANSwitchPlugged%LANSwitchCount% := 0
    If (dump <> "")
    {
     FileAppend,WLAN interface %LANCheckCount%: %NIC1%`n,%dump%
     FileAppend,WLAN connection %LANCheckCount%: %NIC3%`n,%dump%
    }
   }
  }
 }
}

; error check
If( LANCheckCount = 0 OR LANSwitchCount = 0 ) ; no hardware ID for checking is found
{
 If (dump <> "")
  FileAppend,%LANCHECKCOUNTZERO%`n,%dump%
 MsgBox, 16, %MSG_ERROR%, %MSG_LANCHECKCOUNTZERO%
 COM_Term()
 ExitApp
}


;---------------------------------------------------------------
; create a custom tray menu
;---------------------------------------------------------------
Menu, Tray, NoStandard
Menu, Tray, Icon, Shell32.dll, 174
Menu, Tray, Tip, %MSG_TRAYTOOLTIP%
Menu, Tray, Add, %MSG_ABOUT%, About
Menu, Tray, Add, %MSG_SHOWCONFIG%, ShowConfig
Menu, Tray, Add, %MSG_AUTORUN%, SwitchAutoRun
Menu, Tray, Add, %MSG_ENABLEWLANONEXIT%, SwitchWLanOnExit
Menu, Tray, Add
Menu, Tray, Add, %MSG_EXITPROGRAM%, ExitProgram

; check if switching the autorun is allowed in .ini file
IniRead, allowToggleAutorun, %INIFILE%, General, AllowToggleAutorun, ""
If allowToggleAutorun != 1
{
 If (dump <> "")
  FileAppend,Toggle auto run: disabled`n,%dump%
 Menu, Tray, Disable, %MSG_AUTORUN%
} else {
 If (dump <> "")
  FileAppend,Toggle auto run: enabled`n,%dump%
}

; check if autorun is enabled
RunWait, %A_WinDir%\System32\schtasks.exe /Query /TN %LANSwtichTask%,, UseErrorLevel HIDE
If ErrorLevel = 0
{
 If (dump <> "")
  FileAppend,Auto run: enabled`n,%dump%
 Menu, Tray, Check, %MSG_AUTORUN%
} else {
 If (dump <> "")
  FileAppend,Auto run: disabled`n,%dump%
}

; check if exiting this program is allowed in .ini file
IniRead, allowExit, %INIFILE%, General, AllowExit, ""
If allowExit != 1
{
 If (dump <> "")
  FileAppend,Allow exit: not allowed`n,%dump%
 Menu, Tray, Disable, %MSG_EXITPROGRAM%
} else {
 If (dump <> "")
  FileAppend,Allow exit: allowed`n,%dump%
}

; check if the WLAN interfaces should be enabled after exiting the program in .ini file
IniRead, enableWLanOnExit, %INIFILE%, General, EnableWLanOnExit, ""
If enableWLanOnExit != 1
{
 enableWLanOnExit := 0
 If (dump <> "")
  FileAppend,Enabled WLAN on exit: no`n,%dump%
} else {
 If (dump <> "")
  FileAppend,Enabled WLAN on exit: yes`n,%dump%
 Menu, Tray, Check, %MSG_ENABLEWLANONEXIT%
}

;---------------------------------------------------------------
; initialize the program
;---------------------------------------------------------------
; set a low priority for this process
Process, priority, , Low

; wake up from standby or hibernation?
OnMessage( 0x218, "WM_POWERBROADCAST" )


;---------------------------------------------------------------
; check if cables are plugged in
; set the LANCheckPlugged variables to a false value
; so the main loop will recognize a change...
;---------------------------------------------------------------
Loop, %LANCheckCount%
{
 If CablePlugged( LANCheckInterface%A_Index% )
  LANCheckPlugged%A_Index% = 0
 Else
  LANCheckPlugged%A_Index% = 1
}


;---------------------------------------------------------------
; main loop
;---------------------------------------------------------------
;wifiActive := disableWiFi()
wifiActive := 0

Loop, %LANSwitchCount%
{
 If( NICEnabled(LANSwitchInterface%A_Index%) )
 {
  wifiActive := 1
  break
 }
}


Loop
{
 numPlugged := 0
 Loop, %LANCheckCount%
 {
  If CablePlugged( LANCheckInterface%A_Index% )
  {
   numPlugged := numPlugged + 1
   LANCheckPlugged%A_Index% = 1
  } else {
   LANCheckPlugged%A_Index% = 0
  }
 }

 If( numPlugged > 0 )
 {
  If( wifiActive <> 0 )
  {
   wifiActive := disableWiFi()
  }
 } Else {
  If( wifiActive <> 1 )
  {
   wifiActive := enableWiFi()
  }
 }

 Sleep, 500
}


;---------------------------------------------------------------
; exit application
ExitProgram:
 If ( enableWLanOnExit = 1 )
 {
  Loop, %LANSwitchCount%
  {
   tmpConnection := WMI_Query( "\\.\root\CIMV2", "Win32_NetworkAdapter WHERE PhysicalAdapter=True AND Description='" . LANSwitchInterface%A_Index% . "'", "NetConnectionID" )
   Loop, Parse, tmpConnection, "`n"
   {
    If( A_LoopField <> "" )
    {
     TrayTip,, %MSG_TRAYTIPENABLING% %A_LoopField%, 15, 1
     RunWait, %A_WinDir%\System32\netsh.exe interface set interface name="%A_LoopField%" admin=enabled,, Hide
    }
   }
  }
 }

 If (dump <> "")
  FileAppend,Program terminated on request`n,%dump%
 ; uninitialize COM
 COM_Term()
 ExitApp

;---------------------------------------------------------------
enableWiFi()
{
 global
 ; cable unplugged
 Loop, %LANSwitchCount%
 {
  tmpConnection := WMI_Query( "\\.\root\CIMV2", "Win32_NetworkAdapter WHERE PhysicalAdapter=True AND Description='" . LANSwitchInterface%A_Index% . "'", "NetConnectionID" )
  Loop, Parse, tmpConnection, "`n"
  {
   If( A_LoopField <> "" )
   {
    TrayTip,, %MSG_TRAYTIPENABLING% %A_LoopField%, 15, 1
    RunWait, %A_WinDir%\System32\netsh.exe interface set interface name="%A_LoopField%" admin=enabled,, Hide
   }
  }
 }
 return 1
}

;---------------------------------------------------------------
disableWiFi()
{
 global
 Loop, %LANSwitchCount%
 {
  tmpConnection := WMI_Query( "\\.\root\CIMV2", "Win32_NetworkAdapter WHERE PhysicalAdapter=True AND Description='" . LANSwitchInterface%A_Index% . "'", "NetConnectionID" )
  Loop, Parse, tmpConnection, "`n"
  {
   If( A_LoopField <> "" )
   {
    TrayTip,, %MSG_TRAYTIPDISABLING% %A_LoopField%, 15, 1
    RunWait, %A_WinDir%\System32\netsh.exe interface set interface name="%A_LoopField%" admin=disabled,, Hide
   }
  }
 }
 return 0
}

;---------------------------------------------------------------
About:
 MsgBox, 0, %MSG_TRAYTOOLTIP%, %MSG_ABOUTTEXT%
 Return


;---------------------------------------------------------------
ShowConfig:
 msg = %MSG_CONFIGLANCHECK%
 Loop, %LANCheckCount%
 {
  tmpMsg := LANCheckInterface%A_Index% . " ("

  If( !NICEnabled(LANCheckInterface%A_Index%) )
  {
   tmpMsg := tmpMsg . MSG_NOT . " " . MSG_ACTIVE . ")"
  } else {
   tmpMsg := tmpMsg . MSG_ACTIVE . ", "
   If( LANCheckPlugged%A_Index% = 0 )
    tmpMsg := tmpMsg . MSG_NOT . " "

   tmpMsg := tmpMsg . MSG_CONNECTED . ")"
  }
  msg = %msg%`n%tmpmsg%
 }

 msg = %msg%`n`n%MSG_CONFIGLANSWITCH%
 Loop, %LANSwitchCount%
 {
  tmpMsg := LANSwitchConnection%A_Index% . " ("

  If( !NICEnabled(LANSwitchInterface%A_Index%) )
  {
   tmpMsg := tmpMsg . MSG_NOT . " " . MSG_ACTIVE . ")"
  } else {
   tmpMsg := tmpMsg . MSG_ACTIVE . ", "
   If( !CablePlugged( LANSwitchInterface%A_Index% ) )
    tmpMsg := tmpMsg . MSG_NOT . " "

   tmpMsg := tmpMsg . MSG_CONNECTED . ")"
  }
  msg = %msg%`n%tmpmsg%
 }
 MsgBox, 0, %MSG_TRAYTOOLTIP%, %msg%
 tmpMsg =
 msg =
 Return


;---------------------------------------------------------------
; switch the auto run registry entry
SwitchAutoRun:
 ; use a scheduled task on VISTA or newer because the program need elevated access rights to run
 EnvGet, dom, USERDOMAIN
 If dom =
  username = %A_UserName%
 Else
  username = %dom%\%A_UserName%

 RunWait, %A_WinDir%\System32\schtasks.exe /Query /TN %LANSwtichTask%,, UseErrorLevel HIDE
 If ErrorLevel = 0
  RunWait, %A_WinDir%\System32\schtasks.exe /Delete /TN %LANSwtichTask% /F,, UserErrorLevel HIDE
 Else
 {
  EnvGet, tmpFile, TEMP
  tmpFile = %tmpFile%\CrLANSwitcher%A_TickCount%.xml

  IfExist, %tmpFile%
   FileDelete, %tmpFile%

  FileAppend,
(
<?xml version="1.0"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
   <RegistrationInfo>
    <Date>2012-01-01T00:00:00</Date>
    <Author>%username%</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Enabled>true</Enabled>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>%username%</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>true</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%A_ScriptFullPath%"</Command>
    </Exec>
  </Actions>
</Task>
), %tmpFile%

  RunWait, %A_WinDir%\System32\schtasks.exe /Create /TN %LANSwtichTask% /XML %tmpFile% /F,, UserErrorLevel HIDE
  If ErrorLevel <> 0
  {
   MsgBox, %MSG_ERRORCREATINGAUTORUN%`n%tmpFile%
   Menu, Tray, UnCheck, %MSG_AUTORUN%
   Return
  } else {
   IfExist, %tmpFile%
    FileDelete, %tmpFile%
  }
 }

 RunWait, %A_WinDir%\System32\schtasks.exe /Query /TN %LANSwtichTask%,, UseErrorLevel HIDE
 If ErrorLevel = 0
  Menu, Tray, Check, %MSG_AUTORUN%
 Else
  Menu, Tray, UnCheck, %MSG_AUTORUN%

 Return


;---------------------------------------------------------------
; switch the enable WLan on exit entry
SwitchWLanOnExit:
 enableWLanOnExit := 1 - enableWLanOnExit

 If ( enableWLanOnExit = 1 )
  Menu, Tray, Check, %MSG_ENABLEWLANONEXIT%
 Else
  Menu, Tray, UnCheck, %MSG_ENABLEWLANONEXIT%

 Return
 
;---------------------------------------------------------------
; check if a network cable is plugged in
;---------------------------------------------------------------
CablePlugged( NetworkCardDescription )
{
 ; SubStr() to remove a tailing newline
 CablePlugged := !SubStr( WMI_Query( "\\.\root\WMI", "MSNdis_MediaConnectStatus WHERE InstanceName='" . NetworkCardDescription . "'", "NdisMediaConnectStatus" ), 1, 1 )
 Return CablePlugged
}

;---------------------------------------------------------------
; check if a network interface is enabled
;---------------------------------------------------------------
NICEnabled( NetworkCardDescription )
{
 ; SubStr() to remove a tailing newline
 availableNICs := SubStr( WMI_Query( "\\.\root\WMI", "MSNdis_MediaConnectStatus WHERE InstanceName='" . NetworkCardDescription . "'", "NdisMediaConnectStatus" ), 1, 1 )
 If( availableNICs = "" )
  Return False
 Return True
}

;------------------------------------------------------------------------------
; WMI_Query()
; by Sean
; http://www.autohotkey.com/forum/topic16632.html
;------------------------------------------------------------------------------
WMI_Query( Namespace, Class, Property = "" )
{
 psvc := COM_GetObject( "winmgmts:{impersonationLevel=impersonate}!" . Namespace )
 pset := COM_Invoke( psvc, "ExecQuery", "SELECT * FROM " . Class )
 penm := COM_Invoke( pset, "_NewEnum" )
 Loop, % COM_Invoke( pset, "Count" )
 If COM_Enumerate( penm, pobj ) = 0
 {
  If Not Property
   MsgBox % COM_Invoke( pobj, "GetObjectText_" )
  Else
  {
   Loop, Parse, Property, |
    sResult .= COM_Invoke( pobj, A_LoopField ) . "|"   

   StringTrimRight, sResult, sResult, 1
   sResult .= "`n"
  }
  COM_Release( pobj )
 }
 COM_Release( penm )
 COM_Release( pset )
 COM_Release( psvc )
 Return sResult
}

; wake up from standby or hibernation => reload the script
WM_POWERBROADCAST( wparam, lparam, msg, hwnd )
{
 if ( wparam = 7 or wparam = 18 )
  SetTimer, WaitAndRestart, 5000
}

; After waking up from hibernation or standby restart the script to
; reinitialize. Also pass all given parameters to the new instance of
; the script, as first parameter set "/restart".
WaitAndRestart:
 Goto Start


You also need an LANSwitcher.ini file in the same directory as LANSwitcher. It contains just a "General" section and should have these three entries:

AllowToggleAutorun=0|1
AllowExit=0|1
EnableWLanOnExit=0|1


If the parameter "AllowToggleAutorun" is be set to 1, the user is able to toggle the autorun. Therefore the registry is used. Setting the parameter to any other value disables this feature.
If the parameter "AllowExit" is set to 1, the user can exit this program. Setting the parameter to anything else disables the exit entry in the tray menu.
If the parameter "EnableWLanOnExit" is set to 1, the Wifi will be enabled if the program is told to quit.

Best regards, Nils.

Edit: update to version 1.0.2 on march, 6th, 2009
Edit: update to version 1.0.4 on april, 3rd, 2009
Edit: update to version 1.0.7 on january, 15th, 2010

Edit: update to version 1.14



genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
Hey, I like the idea of this...
I have to do this all the time... moving my lappy around at work.

Which devices do I put in here? I know lan devices...
What order, just the order of them in the device manager?

In the LANCheck section put all of them?
In the LANSwitch section put all of them?
Will the program "know" which is the device is wireless?

Is there a way for you to make the ini file from devices?
Meaning, why not have the script get the ids and write the .ini file, automatically.
That way user doesn't have to do it?

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
[LANCheck]
ID1=V1394\NIC1394\2E153141384FC000
ID2=PCI\VEN_14E4&DEV_170C&SUBSYS_01D41028
ID3=PCI\VEN_14E4&DEV_4311&SUBSYS_00071028


[LANSwitch]
ID1=V1394\NIC1394\2E153141384FC000
ID2=PCI\VEN_14E4&DEV_170C&SUBSYS_01D41028
ID3=PCI\VEN_14E4&DEV_4311&SUBSYS_00071028



[General]
AllowToggleAutorun=0
AllowExit=1

does not want to work -
it disables the two below

I happen to have in my network adapters
in this order which is the order they are in from device manager.

1394 net adapater #2
Broadcom 440x (lan)
dell wireless (wlan)

When I run this it disables the id2 and id3 the lan and the wlan
Where are you tellilng it which ones to disable?

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi,

Which devices do I put in here? I know lan devices...
What order, just the order of them in the device manager?

The LANCheck section keeps the ID's of the devices to be checked, normally the 10/100/1000 MBit LAN devices.

The LANSwitch section contain the list of devices which will be switchted depending on the lan cable state.

You may append the ID list or start a new one, all devices will be checked if they exist. For the LANSwitch devices there is also a check if the device is used for a LAN connection.

In the LANCheck section put all of them?

Put just all your LAN devices in there, but not the WLAN devices!

In the LANSwitch section put all of them?

Just all the devices which should be en- or disabled.

Will the program "know" which is the device is wireless?

No, it won't. Therefore you have to use the two sections.

Is there a way for you to make the ini file from devices?
Meaning, why not have the script get the ids and write the .ini file, automatically.
That way user doesn't have to do it?

I haven't found a way yet.

When I run this it disables the id2 and id3 the lan and the wlan
Where are you tellilng it which ones to disable?

Just by putting the LAN device in the LANCheck section and the device to be switched in the LANSwitch section. :-) This shuold work.

Thanks for the response,

Nils.

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
I think I get it.

I still need to test with the lan cable... I'll do that later today.
I'll post back -


Thanks.

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
Works like a champ!
Awesome. I will use this one every day.
Perfect for my lappytop to go from home on a dock to work mobile to another dock with lan...
Nice work!

Thanks

Maybe some more specific directions for dense people like me...

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
Problem or feature request....

When running and when wlan on moving to lan cable no prob.

Problem occurs when computer is connected to lan and then shut down.
When I boot up, with no Lan wired connection - wireless does not automatically come up.

I have to go re-enable wlan. in preferences...

Can you make the script re-enable the wlan on boot up when Lan is not connected.

I have the script running at start up.

?

Was that the other switch in the .ini file? I'll go check.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi,

this should probably work, but I've got the same problem at home and thought it has to do with my network adapter.

I'll check this.

Thanks for the answer,

Nils.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
@genmce:
I think I've found and fixed the problem. Would you please give it another try?

There is no need to change the .ini file.

Regards, Nils.

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009

@genmce:
I think I've found and fixed the problem. Would you please give it another try?

There is no need to change the .ini file.

Regards, Nils.


Nils,

Thanks for fixing...
I think that is got it, my first test worked great, here at home.

I will test several times today in a variety of situaltions.

I'll post back by the end of the day with results.
Thanks

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
Test 1 :booted up - wlan off by lanswitcher, not connected to Lan -
turns on wlan.
Test 2: booted up after Wlan on.
LanSwitcher turns Wlan off and connects using Lan.
Test 3 : booted after Lan on Wlan off, with no Lan connection Lan switcher works as it should.

Well done!

Thank you.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Update to version 1.0.7
Now the tool should support Windows XP, Windows Vista and Windows 7, but only the 32bit versions.

Since I have no Vista running, could someonewho uses this tool check it?

guest - genmce
  • Guests
  • Last active:
  • Joined: --

Update to version 1.0.7
Now the tool should support Windows XP, Windows Vista and Windows 7, but only the 32bit versions.

Since I have no Vista running, could someonewho uses this tool check it?


Cool, I'll check it out, I have some thoughts on trying to automatically populate the ini file. I have been playing with that a bit.
I'll see if I can get those devices to load into ini....

genmce
  • Members
  • 143 posts
  • Last active: Feb 20 2014 04:16 PM
  • Joined: 10 Jan 2009
Hey Niwi,

I get error on load.

line 614
conndisable: ... error - This line does not contain recognized action.

?

also wondering if you could pick up the lan info out of ipconfig /all, then create ini with fileappend, and iniwrite commands.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi genmce,

please try again, I had a copy & paste mistake.

The problem with the ini file is, I need the hardware ID of the network adapter. The solution for checking the connetion state and enabling / disabling isn't mine. I use scripts from other guys. But I have tried to find other solutions, but wasn't successfull yet.

So if you have tips for doing this better I'l have a look at them. :-)

Best regards, Nils.