thebatfink
Well-known member
- Joined
- Mar 29, 2012
- Messages
- 47
- Programming Experience
- Beginner
Hi, I have some code which I found for turning off a wireless connection when a wired lan connection is made (for use on my laptop). It works great as a vbscript but I am trying to re-write it in VB.NET so that I can create a Windows Service with Visual Studio and have it running as a service. I'm currently trying to replicate it in a console application but its complaining when it runs. I think maybe because the vbscript is passed arguments (the network connection names) in the command line and these are of object type. I'm trying to hard code these in and they are string values, but I'm not sure so any help would be much appreciated. Heres the code (commented out the original lines)..
It errors here
Any pointers would be much appreciated!!! Thanks!
VB.NET:
Imports System.Threading
Module Module1
Sub Main()
Dim bCurrentStatus
Dim bChanged
Dim sWatchNetworkCard
Dim sSwitchNetworkCard
'If (WScript.Arguments.Count < 2) Then
' WScript.Echo "*********************************"
' WScript.Echo "* IntelliAdmin Net Switcher *"
' WScript.Echo "* [url=http://www.intelliadmin.com]Remote Administration For Windows[/url] *"
' WScript.Echo "*********************************"
' WScript.Echo vbCrLf & "Usage: " & vbCrLf
' WScript.Echo " NetSwitch.vbs <Card To Watch> <Card To Switch>"
' WScript.Echo vbCrLf & "Explanation: " & vbCrLf
' WScript.Echo " Net Switcher can be used to make sure your"
' WScript.Echo " wireless card is only enabled when no "
' WScript.Echo " ethernet connection is available"
' WScript.Echo vbCrLf & "Example: " & vbCrLf
' WScript.Echo " NetSwitch.vbs " & Chr(34) & "Local Area Connection" & Chr(34) & " " & _
' Chr(34) & "Wireless Connection" & Chr(34)
' WScript.Quit()
'End If
'sWatchNetworkCard = WScript.Arguments(0)
'sSwitchNetworkCard = WScript.Arguments(1)
sWatchNetworkCard = "Local Area Connection"
sSwitchNetworkCard = "Wireless Network Connection"
If (Not (AdapterExists(sWatchNetworkCard))) Then
Console.WriteLine("Error: Could not find the adapter (" & sWatchNetworkCard & ")")
'WScript.Echo "Error: Could not find the adapter (" & sWatchNetworkCard & ")"
'WScript.Quit()
End If
If (Not (AdapterExists(sSwitchNetworkCard))) Then
Console.WriteLine("Error: Could not find the adapter (" & sSwitchNetworkCard & ")")
'WScript.Echo "Error: Could not find the adapter (" & sSwitchNetworkCard & ")"
'WScript.Quit()
End If
bChanged = True
While (True)
If (bChanged) Then
bChanged = False
bCurrentStatus = AdapterStatus(sWatchNetworkCard)
If (bCurrentStatus) Then
EnableAdapter(sSwitchNetworkCard, False)
Else
EnableAdapter(sSwitchNetworkCard, True)
End If
End If
'WScript.Sleep(1000))
Thread.Sleep(1000)
If (bCurrentStatus <> AdapterStatus(sWatchNetworkCard)) Then
bChanged = True
End If
End While
End Sub
'*********************************************
'* Net Switcher - Watches one network card *
'* for connectivity, and toggles another *
'* [url=http://www.intelliadmin.com]Remote Administration For Windows[/url] *
'*********************************************
Sub EnableAdapter(sAdapterName, bStatus)
Dim objWMIService, colItems
objWMIService = GetObject("winmgmts:\\.\root\cimv2")
colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter", , 48)
For Each objItem In colItems
If (UCase(Trim(objItem.NetConnectionID)) = UCase(Trim(sAdapterName))) Then
If (bStatus) Then
objItem.Enable()
Else
objItem.Disable()
End If
End If
Next
End Sub
Function AdapterStatus(sAdapterName)
Dim objWMIService, colItems
objWMIService = GetObject("winmgmts:\\.\root\cimv2")
colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter", , 48)
AdapterStatus = False
For Each objItem In colItems
If (UCase(Trim(objItem.NetConnectionID)) = UCase(Trim(sAdapterName))) Then
AdapterStatus = (objItem.NetConnectionStatus = 2)
End If
Next
End Function
Function AdapterExists(sAdapterName)
Dim objWMIService, colItems
objWMIService = GetObject("winmgmts:\\.\root\cimv2")
colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter", , 48)
AdapterExists = False
For Each objItem In colItems
If (UCase(Trim(objItem.NetConnectionID)) = UCase(Trim(sAdapterName))) Then
AdapterExists = True
End If
Next
End Function
End Module
It errors here
VB.NET:
If (Not (AdapterExists(sWatchNetworkCard))) Then
Unhandled Exception: System.Runtime.InteropServices.COMException: Unspecified error
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at System.Runtime.InteropServices.CustomMarshalers.EnumerableViewOfDispatch.GetEnumerator()
at System.Collections.IEnumerable.GetEnumerator()
at ConsoleApplication1.Module1.AdapterExists(Object sAdapterName) in C:\Users\JMTAYLOR\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 101
at ConsoleApplication1.Module1.Main) in C:\Users\JMTAYLOR\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 32
Any pointers would be much appreciated!!! Thanks!