Page 1 of 1

Get MAC Address

Posted: Wed Nov 05, 2014 10:59 pm
by 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++?

Re: Get MAC Address

Posted: Sat Nov 08, 2014 6:29 pm
by 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;
}