{ Learn Code. Share Code. Teach Code. Love Code. }

  • Get MAC Address

  • Post your questions regarding programming in C++ in here.
Post your questions regarding programming in C++ in here.
 #82431  by MrAlicard
 Wed Nov 05, 2014 10:59 pm
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++?
 #82449  by mandai
 Sat Nov 08, 2014 6:29 pm
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;
}