Get MAC Address

Post your questions regarding programming in C++ in here.
2 posts Page 1 of 1
Contributors
User avatar
MrAlicard
VIP - Donator
VIP - Donator
Posts: 54
Joined: Thu Aug 05, 2010 4:08 pm

Get MAC Address
MrAlicard
Hello
I know in C# how can get MAC Address but in C++ I don't know.
C# look like:
Code: Select all
private string GetMacAddress()
{
    const int MIN_MAC_ADDR_LENGTH = 12;
    string macAddress = string.Empty;
    long maxSpeed = -1;

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        log.Debug(
            "Found MAC Address: " + nic.GetPhysicalAddress() +
            " Type: " + nic.NetworkInterfaceType);

        string tempMac = nic.GetPhysicalAddress().ToString();
        if (nic.Speed > maxSpeed &&
            !string.IsNullOrEmpty(tempMac) &&
            tempMac.Length >= MIN_MAC_ADDR_LENGTH)
        {
            log.Debug("New Max Speed = " + nic.Speed + ", MAC: " + tempMac);
            maxSpeed = nic.Speed;
            macAddress = tempMac;
        }
    }

    return macAddress;
}
How can I do it in C++?
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Get MAC Address
mandai
In Managed C++ it would look something like this:
Code: Select all
		private: String^ GetMacAddress() {
	
    const int MIN_MAC_ADDR_LENGTH = 12;
    String^ macAddress = String::Empty;
    __int64 maxSpeed = -1;

    for each (NetworkInterface^ nic in NetworkInterface::GetAllNetworkInterfaces())
    {
        log.Debug("Found MAC Address: " + nic->GetPhysicalAddress() +
            " Type: " + nic->NetworkInterfaceType.ToString());

        String^ tempMac = nic->GetPhysicalAddress()->ToString();
        if (nic->Speed > maxSpeed &&
            !String::IsNullOrEmpty(tempMac) &&
            tempMac->Length >= MIN_MAC_ADDR_LENGTH)
        {
            log.Debug("New Max Speed = " + nic->Speed + ", MAC: " + tempMac);
            maxSpeed = nic->Speed;
            macAddress = tempMac;

        }

	}

    return macAddress;
}
2 posts Page 1 of 1
Return to “General coding help”