ashtin9634
SEO Innovator
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:
3. Now put this code for your code module.
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
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:

3. Now put this code for your code module.
- using
System.Runtime.InteropServices
;
- public
class
Form1
- {
- [
DllImport(
"wsock32.dll"
, ExactSpelling=
true
, CharSet=
CharSet.
Ansi
, SetLastError=
true
)
]
- private
static
extern
int
inet_addr(
string
s)
;
- [
DllImport(
"iphlpapi.dll"
, ExactSpelling=
true
, CharSet=
CharSet.
Ansi
, SetLastError=
true
)
]
- private
static
extern
int
SendARP(
int
DestIP, int
SrcIP, ref
int
pMACAddr, ref
int
PhyAddrLen)
;
- [
DllImport(
"kernel32"
,EntryPoint=
"RtlMoveMemory"
, ExactSpelling=
true
, CharSet=
CharSet.
Ansi
, SetLastError=
true
)
]
- private
static
extern
void
CopyMemory(
ref
byte
dst, ref
int
src, int
bcount)
;
- private
void
Button1_Click(
System
.
Object
sender, System
.
EventArgs
e)
- {
- string
sip =
default
(
string
)
;
- int
inet =
default
(
int
)
;
- byte
[
]
b =
new
byte
[
7
]
;
- int
pMACAddr =
default
(
int
)
;
- short
i =
default
(
short
)
;
- string
sResult =
""
;
- sip =
(
string
)
TextBox1.
Text
;
- inet =
System
.
Convert
.
ToInt32
(
inet_addr(
sip)
)
;
- System
.
Int32
temp_PhyAddrLen =
6
;
- if
(
SendARP(
inet, 0
, ref
pMACAddr, ref
temp_PhyAddrLen)
==
0
)
- {
- CopyMemory(
ref
b[
0
]
, ref
pMACAddr, 6
)
;
- for
(
i =
0
;
i <=
5
;
i++
)
- {
- sResult =
sResult +
Microsoft.
VisualBasic
.
Right
(
"0"
+
Hex(
b[
i]
)
, 2
)
;
- if
(
i <
5
)
- {
- sResult +=
"-"
;
- }
- }
- }
- TextBox2.
Text
=
sResult;
- }
- }
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