Set ethernet adapter to DHCP in Visual Basic application

Hi,

I need to be able to set an ethernet adapter to DHCP in a visual basic application,

Is anyone able to assist?

Thanks,
LVL 1
locdangAsked:
Who is Participating?
 
grayeCommented:
This can be easily done with Windows Management Instrumentation (WMI)...  The toughest part is figuring out which network adpater (there will be several psuedo adapters) to use.   In the example below, it turns on DHCP for all adapters that support IP.
http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa390378(VS.85).aspx 

Imports System
Imports System.Management
Imports System.Windows.Forms
 
Namespace WMISample
    Public Class CallWMIMethod
        Public Overloads Shared Function Main() As Integer
            Try
                Dim classInstance As New ManagementObject( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
 
                ' Execute the method and obtain the return values.
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod("EnableDHCP", Nothing, Nothing)
 
                ' List outParams
                Console.WriteLine("Out parameters:")
                Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
 
            Catch err As ManagementException
                MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
            End Try
        End Function
    End Class
End Namespace

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.