Skip to main content

Display MAC Address using Local IP in VB.NET

Body
A MAC (Media Access Control) address is a number that identifies the network adapter(s) installed on your computer. The address is composed of up to 6 pairs of characters, separated by colons. You may need to provide your MAC address to a router in order to successfully connect to a network. There are so many ways to find the MAC Address of your PC. But in this article, we will only locate and know what is our IP address and then we can know also our MAC Address. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one Button named Button1 and labeled it as "Get MAC Address". Insert two textboxes named Textbox1 for inputting the IP Address and Textbox2 for displaying the MAC Address. You must design your interface like this: design 3. Now put this code for your code module.
  1. Public Class Form1
  2.  
  3. Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Integer
  4.  
  5. Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Integer, ByVal SrcIP As Integer, ByRef pMACAddr As Integer, ByRef PhyAddrLen As Integer) As Integer
  6.  
  7. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef dst As Byte, ByRef src As Integer, ByVal bcount As Integer)
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9.  
  10. Dim sip As String
  11. Dim inet As Integer
  12. Dim b(6) As Byte
  13. Dim pMACAddr As Integer
  14. Dim i As Short
  15. Dim sResult As String = ""
  16.  
  17. sip = TextBox1.Text
  18. inet = inet_addr(sIP)
  19. If SendARP(inet, 0, pMACAddr, 6) = 0 Then
  20. CopyMemory(b(0), pMACAddr, 6)
  21. For i = 0 To 5
  22. sResult = sResult & Microsoft.VisualBasic.Right("0" & Hex(b(i)), 2)
  23. If i < 5 Then sResult &= "-"
  24. Next
  25. End If
  26.  
  27. TextBox2.Text = sResult
  28. End Sub
  29.  
  30. End Class

Explanation:

We declare first the library function of wsock32.dll named inet_addr . wsock32.dll is for winsock/windows socket creates a socket that is bound to a specific transport service provider. It also enables programmers to create advanced Internet, intranet, and other network-capable applications to transmit application data across the wire, independent of the network protocol being used. Next is the SendARP that has the library of iphlpapi.dll. The SendARP function sends an Address Resolution Protocol (ARP) request to obtain the physical address that corresponds to the specified destination IPv4 address. Last is the Copy Memory function which copies a block of memory from one location to another and is on the library of kernel32. sIP variable holds the IP address iputted in textbox1. Then the inet which holds the socket will also hold the value in textbox1. If SendARP(inet, 0, pMACAddr, 6) = 0 - inet = Destination IP, 0 = Source IP, pMACAddr = Present MAC Address, and 6 = Physical Address Length, then the value of it will be copied and held now by CopyMemory function. There are 6 pairs of characters in a MAC address so we have created a for loop which starts to 0 up to 5 in which all the value of the MAC Address is held by sResult and will be displayed in Textbox2. Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below and hire me. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Visit my website at: CampCodes

 

Add new comment