Enabling / Disabling network card exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kevinn7
    New Member
    • Jul 2009
    • 8

    Enabling / Disabling network card exception

    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?

    Code:
     Dim strComputer As String
            Dim coladapters As Object
            strComputer = "."
            objWmiService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
            coladapters = objWmiService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled='false'")
            For Each Adapter In coladapters
                Adapter.enable()
            Next
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    • kevinn7
      New Member
      • Jul 2009
      • 8

      #3
      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?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

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

        I threw this together from what I read on msdn:
        [code=vbnet]
        Public Shared Function DoStuff() As Integer
        Try
        Dim classInstance As New ManagementObjec t("root\CIMV2", "Win32_NetworkA dapter",Nothing )

        Dim outParams As ManagementBaseO bject = classInstance.I nvokeMethod("En able", Nothing, Nothing)
        'Dim outParams As ManagementBaseO bject = classInstance.I nvokeMethod("Di sable", Nothing, Nothing)

        ' List outParams
        Console.WriteLi ne("Out parameters:")
        Console.WriteLi ne("ReturnValue : {0}", outParams("Retu rnValue"))

        Catch err As ManagementExcep tion
        MessageBox.Show ("An error occurred while trying to execute the WMI method: " & err.Message)
        End Try
        End Function
        [/code]

        Comment

        • kevinn7
          New Member
          • Jul 2009
          • 8

          #5
          nice.. ill give tht a try. thx alot. yr help is much appreciated. =)

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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.

            Comment

            Working...