Display MAC Address using Local IP in C#

In this tutorial, i will teach you how to create a program that displays a MAC Address using Local IP in C#. We all know that 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 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add only one Button named Button1 and labeled it as "Get MAC Addres". Insert two textboxes named Textbox1 for inputting the IP Address and Textbox2 for displaying the MAC Address. You must design your interface like this: output 3. Now put this code for your code module.
  1. using System.Runtime.InteropServices;
  2.  
  3.  
  4.  
  5. public class Form1
  6. {
  7.  
  8. [DllImport("wsock32.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
  9. private static extern int inet_addr(string s);
  10.  
  11. [DllImport("iphlpapi.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
  12. private static extern int SendARP(int DestIP, int SrcIP, ref int pMACAddr, ref int PhyAddrLen);
  13.  
  14. [DllImport("kernel32",EntryPoint="RtlMoveMemory", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
  15. private static extern void CopyMemory(ref byte dst, ref int src, int bcount);
  16. private void Button1_Click(System.Object sender, System.EventArgs e)
  17. {
  18.  
  19. string sip = default(string);
  20. int inet = default(int);
  21. byte[] b = new byte[7];
  22. int pMACAddr = default(int);
  23. short i = default(short);
  24. string sResult = "";
  25.  
  26. sip = (string) TextBox1.Text;
  27. inet = System.Convert.ToInt32(inet_addr(sip));
  28. System.Int32 temp_PhyAddrLen = 6;
  29. if (SendARP(inet, 0, ref pMACAddr, ref temp_PhyAddrLen) == 0)
  30. {
  31. CopyMemory(ref b[0], ref pMACAddr, 6);
  32. for (i = 0; i <= 5; i++)
  33. {
  34. sResult = sResult + Microsoft.VisualBasic.Right("0" + Hex(b[i]), 2);
  35. if (i < 5)
  36. {
  37. sResult += "-";
  38. }
  39. }
  40. }
  41.  
  42. TextBox2.Text = sResult;
  43. }
  44.  
  45. }
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. For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment