• We just launched and are currently in beta. Join us as we build and grow the community.

Display MAC Address using Local IP in C#

ashtin9634

SEO Innovator
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
115
Likes
164
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:

macdesign_0.png


3. Now put this code for your code module.

  1. using

    System.Runtime.InteropServices

    ;



  2. public

    class

    Form1
  3. {

  4. [

    DllImport(

    "wsock32.dll"

    , ExactSpelling=

    true

    , CharSet=

    CharSet.

    Ansi

    , SetLastError=

    true

    )

    ]
  5. private

    static

    extern

    int

    inet_addr(

    string

    s)

    ;

  6. [

    DllImport(

    "iphlpapi.dll"

    , ExactSpelling=

    true

    , CharSet=

    CharSet.

    Ansi

    , SetLastError=

    true

    )

    ]
  7. private

    static

    extern

    int

    SendARP(

    int

    DestIP, int

    SrcIP, ref

    int

    pMACAddr, ref

    int

    PhyAddrLen)

    ;

  8. [

    DllImport(

    "kernel32"

    ,EntryPoint=

    "RtlMoveMemory"

    , ExactSpelling=

    true

    , CharSet=

    CharSet.

    Ansi

    , SetLastError=

    true

    )

    ]
  9. private

    static

    extern

    void

    CopyMemory(

    ref

    byte

    dst, ref

    int

    src, int

    bcount)

    ;
  10. private

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  11. {

  12. string

    sip =

    default

    (

    string

    )

    ;
  13. int

    inet =

    default

    (

    int

    )

    ;
  14. byte

    [

    ]

    b =

    new

    byte

    [

    7

    ]

    ;
  15. int

    pMACAddr =

    default

    (

    int

    )

    ;
  16. short

    i =

    default

    (

    short

    )

    ;
  17. string

    sResult =

    ""

    ;

  18. sip =

    (

    string

    )

    TextBox1.

    Text

    ;
  19. inet =

    System

    .

    Convert

    .

    ToInt32

    (

    inet_addr(

    sip)

    )

    ;
  20. System

    .

    Int32

    temp_PhyAddrLen =

    6

    ;
  21. if

    (

    SendARP(

    inet, 0

    , ref

    pMACAddr, ref

    temp_PhyAddrLen)

    ==

    0

    )
  22. {
  23. CopyMemory(

    ref

    b[

    0

    ]

    , ref

    pMACAddr, 6

    )

    ;
  24. for

    (

    i =

    0

    ;

    i <=

    5

    ;

    i++

    )
  25. {
  26. sResult =

    sResult +

    Microsoft.

    VisualBasic

    .

    Right

    (

    "0"

    +

    Hex(

    b[

    i]

    )

    , 2

    )

    ;
  27. if

    (

    i <

    5

    )
  28. {
  29. sResult +=

    "-"

    ;
  30. }
  31. }
  32. }

  33. TextBox2.

    Text

    =

    sResult;
  34. }

  35. }

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

 

449,193

322,229

322,238

Top