Get the MACAddress from the computer

  • Thread starter Thread starter akayoku
  • Start date Start date
A

akayoku

Hello all,

I'm working on a school project to make a security program. I need to
get the MAC Address from the computer. That is the "Physical Address"
when you type "ipconfig/all" in MSDOS command prompt.

It is urgent and I have spent pretty much time there, but still don't
know what to do. I am an amateur in programming. I'd greatly appreciate
your opinion. Thanks.

~~~ Landy
 
Hi akayoku!
I'm working on a school project to make a security program. I need to
get the MAC Address from the computer. That is the "Physical Address"
when you type "ipconfig/all" in MSDOS command prompt.

See: How To Get the MAC Address for an Ethernet Adapter
http://support.microsoft.com/kb/118623/en-us

Or try this one:

<code>
CString GetMACAddress(int adapternumber)
{
int nAdapterCount = 0;
ULONG ip;
ULONG buflen;
PIP_ADAPTER_INFO pAdInfo = NULL;
PIP_ADAPTER_INFO pAdInfo_c = NULL;


buflen = 0;
GetAdaptersInfo(pAdInfo, &buflen); //since buflen=0, buffer is
// too small. function returns required buffersize in buflen.
pAdInfo = (struct _IP_ADAPTER_INFO *)new UCHAR[buflen+1];
pAdInfo_c = pAdInfo;
if (GetAdaptersInfo(pAdInfo, &buflen) == ERROR_SUCCESS)
{
do
{
ip = inet_addr(pAdInfo->IpAddressLi­st.IpAddress.String);
if ((ip != 0)&&(ip != 0x7f000001))
{
nAdapterCount++;
if ((nAdapterCount == adapternumber)||(adapternumber == 0))
{
if (pAdInfo->AddressLength != 0)
{
CString macstr;
for (int i = 0; i < (int)pAdInfo->AddressLength; i++)
{
CString temp;
temp.Format(_T(" %02X"), pAdInfo->Address);
macstr += temp;
}
delete pAdInfo;
return macstr;
}
}
}
} while ((pAdInfo->Next != NULL)&&((pAdInfo = pAdInfo->Next) !=
pAdInfo));
}
delete pAdInfo_c;
return _T("");
}
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
akayoku said:
Hello all,

I'm working on a school project to make a security program. I need to
get the MAC Address from the computer. That is the "Physical Address"
when you type "ipconfig/all" in MSDOS command prompt.

It is urgent and I have spent pretty much time there, but still don't
know what to do. I am an amateur in programming. I'd greatly appreciate
your opinion. Thanks.
~~~ Landy

Hi,
since this is a school project and you don't have much time,
IMHO you can quite well spawn ipconfig and parse it's output.
Also, instead of ipconfig, you can use :
netsh interface ip show config

So, the task is reduced to using system(...) and trivial scanning of text
file.

/* A real "commercial strength" solution won't use anything dependent on
TCPIP, because IP protocol driver may be just not bound to your ethernet card
:)
Also it won't be fooled by "overriding" the built-in MAC address :))
Probably it will use WMI or low-level NDIS ioctls. */

Good luck,
--PA
 
I have tried with the code, but I have received the next mesage :

error C2065: 'PIP_ADAPTER_INFO' : undeclared identifier

After that, I added #include Iphlpapi.h with no result !!
Do you have any other suggestion ??
Thanks in advanced,

German
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Jochen Kalmbach said:
Hi akayoku!
I'm working on a school project to make a security program. I need to
get the MAC Address from the computer. That is the "Physical Address"
when you type "ipconfig/all" in MSDOS command prompt.

See: How To Get the MAC Address for an Ethernet Adapter
http://support.microsoft.com/kb/118623/en-us

Or try this one:

<code>
CString GetMACAddress(int adapternumber)
{
int nAdapterCount = 0;
ULONG ip;
ULONG buflen;
PIP_ADAPTER_INFO pAdInfo = NULL;
PIP_ADAPTER_INFO pAdInfo_c = NULL;


buflen = 0;
GetAdaptersInfo(pAdInfo, &buflen); //since buflen=0, buffer is
// too small. function returns required buffersize in buflen.
pAdInfo = (struct _IP_ADAPTER_INFO *)new UCHAR[buflen+1];
pAdInfo_c = pAdInfo;
if (GetAdaptersInfo(pAdInfo, &buflen) == ERROR_SUCCESS)
{
do
{
ip = inet_addr(pAdInfo->IpAddressLi­st.IpAddress.String);
if ((ip != 0)&&(ip != 0x7f000001))
{
nAdapterCount++;
if ((nAdapterCount == adapternumber)||(adapternumber == 0))
{
if (pAdInfo->AddressLength != 0)
{
CString macstr;
for (int i = 0; i < (int)pAdInfo->AddressLength; i++)
{
CString temp;
temp.Format(_T(" %02X"), pAdInfo->Address);
macstr += temp;
}
delete pAdInfo;
return macstr;
}
}
}
} while ((pAdInfo->Next != NULL)&&((pAdInfo = pAdInfo->Next) !=
pAdInfo));
}
delete pAdInfo_c;
return _T("");
}
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Back
Top