Connecting Tech Pros Worldwide Forums | Help | Site Map

Enabling / Disabling network card exception

Newbie
 
Join Date: Jul 2009
Posts: 8
#1: Aug 2 '09
Below is my code which enables and disables all network cards in my PC.

It works fine but when I execute this code on a normal User Account, i get the exception " Unable to read beyond the end of the stream" .

Im also aware that as a normal user, one will not be able to enable and disable network cards. I tried changing the settings in gpedit.msc bt it still doesnt work.

How do I grant permission to the user so that he wud b able to disable and enable the network card using the code below?

Expand|Select|Wrap|Line Numbers
  1.  Dim strComputer As String
  2.         Dim coladapters As Object
  3.         strComputer = "."
  4.         objWmiService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  5.         coladapters = objWmiService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled='false'")
  6.         For Each Adapter In coladapters
  7.             Adapter.enable()
  8.         Next
  9.  
  10.  
  11.  

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,167
#2: Aug 5 '09

re: Enabling / Disabling network card exception


You might be able to change the user you use to get into the WMI calls.
(Is there a reason you are not using the .NET WMI implimentation for this?
Newbie
 
Join Date: Jul 2009
Posts: 8
#3: Aug 5 '09

re: Enabling / Disabling network card exception


ok im kinda lost.. i thought i was? lol

mind showing me how it'll be like if i were to use the .net wmi?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,167
#4: Aug 5 '09

re: Enabling / Disabling network card exception


Def not. Take a look at the System.Management namespace. (You will have to add the assembly reference, it doesn't come added)

I threw this together from what I read on msdn:
Expand|Select|Wrap|Line Numbers
  1. Public Shared Function DoStuff() As Integer
  2.     Try
  3.         Dim classInstance As New ManagementObject("root\CIMV2","Win32_NetworkAdapter",Nothing)
  4.  
  5.         Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("Enable", Nothing, Nothing)
  6.         'Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("Disable", Nothing, Nothing)
  7.  
  8.         ' List outParams
  9.         Console.WriteLine("Out parameters:")
  10.         Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
  11.  
  12.     Catch err As ManagementException
  13.         MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
  14.     End Try
  15. End Function
  16.  
Newbie
 
Join Date: Jul 2009
Posts: 8
#5: Aug 6 '09

re: Enabling / Disabling network card exception


nice.. ill give tht a try. thx alot. yr help is much appreciated. =)
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,167
#6: Aug 6 '09

re: Enabling / Disabling network card exception


I also recommend the "WMI Code Creator" for fiddling with WMI. It has a little gui that creates the code for you, let you see how it all works.
Reply


Similar Visual Basic .NET bytes