Question MAC Address and Enable/Disable Network adapter Local use only

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
130
Location
UK
Programming Experience
1-3
:eek: Hi, i new here so be gentle lol, Im trying to Get the mac of the local computer(s) on a network and Enable/Disable the Connectiong adapter that is UP (In use), i know i can do this with the system as it stands and for a while i've used a *.bat file to do this for me but not i want a more elabrate way of doing it using a program i make for this, i will also be making it Control other computers remote on my network the same with a simple server side, in the future!, i just need it to Work on the local computer, I've read up about diffrent ways of doing this, like Getting the MAC via Registry like so:

VB.NET:
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{0000000000-0000-0000-BFC1-00000000000}\0008\", "NetworkAddress", "0")

but from what i gather, i have tried this program on another pc with a test app that it wouldnt get the MAC as the Registry string can be placed in a diffrent location, How would i get program to find the string for each pc's MAC, i know this maybe easy for some of you's but i haven't really messed with registry programming,

Also to disable and enable the Network Adapter which is selected via a Listview or combobox select accompanied by a button,

Thanks in advance

p's if im unclear at all please ask :)
 
Hi i have figured part of this out, with extensive searching, and reading some of this forum, to get the adapter by name i used the Following code:

Imports System
Imports System.Management

Public Sub MyWMIQuery()

'Public Overloads Shared Function Main() As Integer

Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_NetworkAdapter")

For Each queryObj As ManagementObject In searcher.Get()
cboAdapters.Items.Add(queryObj("ProductName").ToString)
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub

with further extensive coding you can use this to get other details like MAC Address, now what im stuck on is using this with the registry, say for instance i wish to change my mac address on local machine and updating it so it takes effect, I'm trying to figure out now how to Disable/Enable the given adapter, if you have any info then please dont hesitate in helping.

cheers in advance :)
 
OOOOOoooooooooooook.................. lol, ive figured alot of this out now, with brain straining and reading, now what i need to try and figure out is the registry side part of things as i formentioned

My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{0000000000-0000-0000-BFC1-00000000000}\0008\", "NetworkAddress", "0")

now this works fine For my machine but if i try another it wont be valid, what im asking is is their away to find out how to get the value to be Changed/Undo the string for any machine by using a registry search function or is their an easier method? using the WMI or System.Net.NetworkInformation?, cheers in advance :)
 
Ok here goes so far:

VB.NET:
Option Explicit On
Option Strict On
Imports System.Management

VB.NET:
    Private Sub frmNetWorks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyWMIQuery()
    End Sub

VB.NET:
    Public Sub MyWMIQuery()
        'Public Overloads Shared Function Main() As Integer

        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_NetworkAdapter WHERE AdapterType = 'Ethernet 802.3' AND NetConnectionID = 'Local Area Connection'")

            For Each queryObj As ManagementObject In searcher.Get()
                cboAdapters.Items.Add(queryObj("ProductName").ToString)
                cboAdapters.Text = queryObj("ProductName").ToString
            Next

        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try

        Dim strRegiVal As String
        strRegiVal = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0008\", "NetworkAddress", "0").ToString
        If strRegiVal = txtMacAddy.Text Then
            btnChange.Text = "Change"
        Else
            btnChange.Text = "Restore"
        End If

    End Sub

VB.NET:
    Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        'Single button two executions depending on Button.Text
        Select Case btnChange.Text
            Case "Change"
                'Change MAC Address
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0008\", "NetworkAddress", txtChange.Text)
                btnChange.Text = "Restore"
            Case "Restore"
                'Restores MAC Address
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0008\", "NetworkAddress", txtMacAddy.Text)
                btnChange.Text = "Change"
        End Select

    End Sub

VB.NET:
   Private Sub cboAdapters_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAdapters.SelectedIndexChanged
        'Below Brings up the list of items and add's it to the list box 
        Dim itemz As New ListViewItem
        lstDevices.Items.Clear()
        Try
            Dim searcher As New ManagementObjectSearcher( _
              "root\CIMV2", _
              "SELECT * FROM Win32_NetworkAdapter WHERE ProductName = '" & cboAdapters.Text & "'")

            For Each queryObj As ManagementObject In searcher.Get()
                itemz.Text = queryObj("MACAddress").ToString.Replace(":", "") 'MACAddress - Replace removes the ":" in the mac address
                itemz.SubItems.Add(queryObj("Description").ToString) 'Device Type
                itemz.SubItems.Add(queryObj("NetConnectionID").ToString) 'Area Name
                itemz.SubItems.Add(queryObj("SystemName").ToString) 'Host
                itemz.SubItems.Add(queryObj("AdapterType").ToString) 'Type
                itemz.SubItems.Add(queryObj("MACAddress").ToString) 'State
                lstDevices.Items.Add(itemz)
                txtMacAddy.Text = itemz.Text
            Next

        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try

    End Sub

as you can tell ive been racking my head on this, all's is left now is to Disable/Enable the Network adapter ive tried the DHCP way and this doesn't work the way i need it to, i literally want it to close the Adapter off and reinitialise it so settings take effect, Help would be apriciated, cheers :)
 
Id take a go at this, but im just a little afraid of messing with my registry. i dont like changing values in it except the currentversion/run. Now, this has been bothering me ever since i saw it, and i really DONT understand why people do it...

VB.NET:
Dim searcher As New ManagementObjectSearcher( _
              "root\CIMV2", _
              "SELECT * FROM Win32_NetworkAdapter WHERE ProductName = '" & cboAdapters.Text & "'")
What is the point of making one line of code multilines using "_" ?
i just dont really see the point of this... but thats just me.

Sorry i couldnt be of much use.. =(
 
Because, you code doesn't run off the side of the page, and keeps it in a tidy format so you see all the code in the current source page, so you only have to deal with a single vertical scroll bacr instead of both vert and horizontal too, just simplefies moving around the Page. its no real bother too :)
 
Back
Top