If you have a PH account, you can customize your PH profile.

Internet Development

Moderators: None (Apply to moderate this forum)
Number of threads: 992
Number of posts: 1802

This Forum Only
Post New Thread

Report
Getting MAC address Posted by Josh Code on 2 Jun 2005 at 11:05 AM
This message was edited by Josh Code at 2005-6-2 11:13:7

Hello,

How can you get the MAC address programmatically from the computer the code runs on?

This should be a little easier. How do you get the IP address from the current computer? My methods are a little clumsy, for example parsing the output from ipconfig.

I'm using Windows XP on a Novell network, if this helps.


thanks




Report
Re: Getting MAC address Solved Posted by Josh Code on 5 Jun 2005 at 6:53 AM
This problem was mostly solved.

I'll explain the solution for those who may face similar problems later.

1.
I used redirection on the ipconfig command to store output in a textfile.

ipconfg/all > output.txt

2. One of my programs opens this file(output.txt) and searches the output for "Physical address" and "IP address" and tries to identify the one for the video card. There are many other lines for physical address so the program is somewhat guessing on the right one. I used the "Description" and title lines for each component to determine which was most likely for the Network Interface Card(NIC).

3. Output the extracted MAC address and IP address.


I'm still facing problems with making a flexible detection scheme that will detect the correct section of output for the NIC card as apposed to the router and other devices.


example of output from the command:
Windows 98 IP Configuration

	Host Name . . . . . . . . . : BLABLA
	DNS Servers . . . . . . . . : 206.47.244.106
	                              207.236.186.9
	                              191.162.133.254
	Node Type . . . . . . . . . : Broadcast
	NetBIOS Scope ID. . . . . . : 
	IP Routing Enabled. . . . . : Yes
	WINS Proxy Enabled. . . . . : No
	NetBIOS Resolution Uses DNS : No


0 Ethernet adapter :

	Description . . . . . . . . : PPP Adapter.
	Physical Address. . . . . . : 34-54-58-54-00-00
	DHCP Enabled. . . . . . . . : Yes
	IP Address. . . . . . . . . : 67.238.131.93
	Subnet Mask . . . . . . . . : 255.0.0.0
	Default Gateway . . . . . . : 67.238.131.93
	DHCP Server . . . . . . . . : 255.255.255.255
	Primary WINS Server . . . . : 
	Secondary WINS Server . . . : 
	Lease Obtained. . . . . . . : 01 01 80 12:00:00 AM
	Lease Expires . . . . . . . : 01 01 80 12:00:00 AM


1 Ethernet adapter :

	Description . . . . . . . . : PPP Adapter.
	Physical Address. . . . . . : 34-54-58-54-00-01
	DHCP Enabled. . . . . . . . : Yes
	IP Address. . . . . . . . . : 0.0.0.0
	Subnet Mask . . . . . . . . : 0.0.0.0
	Default Gateway . . . . . . : 
	DHCP Server . . . . . . . . : 255.255.255.255
	Primary WINS Server . . . . : 
	Secondary WINS Server . . . : 
	Lease Obtained. . . . . . . : 
	Lease Expires . . . . . . . : 

2 Ethernet adapter :

	Description . . . . . . . . : D-Link DL10050-based FAST Ethernet Adapter
	Physical Address. . . . . . : 00-05-5E-FF-11-B8
	DHCP Enabled. . . . . . . . : Yes
	IP Address. . . . . . . . . : 191.162.133.165
	Subnet Mask . . . . . . . . : 255.255.255.0
	Default Gateway . . . . . . : 191.162.133.254
	DHCP Server . . . . . . . . : 191.162.133.254
	Primary WINS Server . . . . : 
	Secondary WINS Server . . . : 
	Lease Obtained. . . . . . . : 06 04 05 5:29:47 PM
	Lease Expires . . . . . . . : 06 11 05 5:29:47 PM


: This message was edited by Josh Code at 2005-6-2 11:13:7

: Hello,
:
: How can you get the MAC address programmatically from the computer the code runs on?
:
: This should be a little easier. How do you get the IP address from the current computer? My methods are a little clumsy, for example parsing the output from ipconfig.
:
: I'm using Windows XP on a Novell network, if this helps.
:
:
: thanks
:
:
:
:
:

Report
Re: Getting MAC address Solved Posted by Steven Wilson on 20 Jan 2006 at 8:02 AM
I suggest that for just the MAC address the command getmac i sprobably more suited to your needs as it is more terse but it does not include IP address.

Programmatically I have used GetAdaptersInfo. You need to include the files
#include <iprtrmib.h>
#include <Iphlpapi.h>
from the microsoft paltform sdk.
The sort of code you need is as follows
##start of code sample

DWORD dwNumInterfaces;
GetNumberOfInterfaces(&dwNumInterfaces);

//IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
IP_ADAPTER_INFO* AdapterInfo = new IP_ADAPTER_INFO[dwNumInterfaces];

DWORD dwBufLen = sizeof(*AdapterInfo); // Save memory size of buffer

DWORD dwStatus = GetAdaptersInfo(
AdapterInfo, // [out]
&dwBufLen); // [in]
assert(dwStatus == ERROR_SUCCESS); // Verify return value is
// valid, no buffer overflow

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
FormatMACaddress(pAdapterInfo->Address,rstrMacAddress);

current adapter info
do {
DoSomethingFunction(pAdapterInfo->Address); // process MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through
// linked list
}
while(pAdapterInfo); // Terminate if last adapter

delete [] AdapterInfo;

##end of code sample

: This problem was mostly solved.
:
: I'll explain the solution for those who may face similar problems later.
:
: 1.
: I used redirection on the ipconfig command to store output in a textfile.
:
: ipconfg/all > output.txt
:
: 2. One of my programs opens this file(output.txt) and searches the output for "Physical address" and "IP address" and tries to identify the one for the video card. There are many other lines for physical address so the program is somewhat guessing on the right one. I used the "Description" and title lines for each component to determine which was most likely for the Network Interface Card(NIC).
:
: 3. Output the extracted MAC address and IP address.
:
:
: I'm still facing problems with making a flexible detection scheme that will detect the correct section of output for the NIC card as apposed to the router and other devices.
:
:
: example of output from the command:
:
: Windows 98 IP Configuration
: 
: 	Host Name . . . . . . . . . : BLABLA
: 	DNS Servers . . . . . . . . : 206.47.244.106
: 	                              207.236.186.9
: 	                              191.162.133.254
: 	Node Type . . . . . . . . . : Broadcast
: 	NetBIOS Scope ID. . . . . . : 
: 	IP Routing Enabled. . . . . : Yes
: 	WINS Proxy Enabled. . . . . : No
: 	NetBIOS Resolution Uses DNS : No
: 
: 
: 0 Ethernet adapter :
: 
: 	Description . . . . . . . . : PPP Adapter.
: 	Physical Address. . . . . . : 34-54-58-54-00-00
: 	DHCP Enabled. . . . . . . . : Yes
: 	IP Address. . . . . . . . . : 67.238.131.93
: 	Subnet Mask . . . . . . . . : 255.0.0.0
: 	Default Gateway . . . . . . : 67.238.131.93
: 	DHCP Server . . . . . . . . : 255.255.255.255
: 	Primary WINS Server . . . . : 
: 	Secondary WINS Server . . . : 
: 	Lease Obtained. . . . . . . : 01 01 80 12:00:00 AM
: 	Lease Expires . . . . . . . : 01 01 80 12:00:00 AM
: 
: 
: 1 Ethernet adapter :
: 
: 	Description . . . . . . . . : PPP Adapter.
: 	Physical Address. . . . . . : 34-54-58-54-00-01
: 	DHCP Enabled. . . . . . . . : Yes
: 	IP Address. . . . . . . . . : 0.0.0.0
: 	Subnet Mask . . . . . . . . : 0.0.0.0
: 	Default Gateway . . . . . . : 
: 	DHCP Server . . . . . . . . : 255.255.255.255
: 	Primary WINS Server . . . . : 
: 	Secondary WINS Server . . . : 
: 	Lease Obtained. . . . . . . : 
: 	Lease Expires . . . . . . . : 
: 
: 2 Ethernet adapter :
: 
: 	Description . . . . . . . . : D-Link DL10050-based FAST Ethernet Adapter
: 	Physical Address. . . . . . : 00-05-5E-FF-11-B8
: 	DHCP Enabled. . . . . . . . : Yes
: 	IP Address. . . . . . . . . : 191.162.133.165
: 	Subnet Mask . . . . . . . . : 255.255.255.0
: 	Default Gateway . . . . . . : 191.162.133.254
: 	DHCP Server . . . . . . . . : 191.162.133.254
: 	Primary WINS Server . . . . : 
: 	Secondary WINS Server . . . : 
: 	Lease Obtained. . . . . . . : 06 04 05 5:29:47 PM
: 	Lease Expires . . . . . . . : 06 11 05 5:29:47 PM
: 

:
: : This message was edited by Josh Code at 2005-6-2 11:13:7

: : Hello,
: :
: : How can you get the MAC address programmatically from the computer the code runs on?
: :
: : This should be a little easier. How do you get the IP address from the current computer? My methods are a little clumsy, for example parsing the output from ipconfig.
: :
: : I'm using Windows XP on a Novell network, if this helps.
: :
: :
: : thanks
: :
: :
: :
: :
: :
:
:




 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.