Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Getting MAC address

[b][red]This message was edited by Josh Code at 2005-6-2 11:13:7[/red][/b][hr]
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




Comments

  • Josh CodeJosh Code Member Posts: 675
    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:
    [code]
    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
    [/code]

    : [b][red]This message was edited by Josh Code at 2005-6-2 11:13:7[/red][/b][hr]
    : 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
    :
    :
    :
    :
    :

  • Steven WilsonSteven Wilson Member Posts: 1
    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
    #include
    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:
    : [code]
    : 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
    : [/code]
    :
    : : [b][red]This message was edited by Josh Code at 2005-6-2 11:13:7[/red][/b][hr]
    : : 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
    : :
    : :
    : :
    : :
    : :
    :
    :

Sign In or Register to comment.