• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2026 Replies
  • 331 Replies
    1928 Replies

Get MAC Address

User avatar
MrAlicard
VIP - Donator
Posts: 54

Get MAC Address

Wed Nov 05, 2014 11: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++?

User avatar
mandai
Coding God
Posts: 2585

Re: Get MAC Address

Sat Nov 08, 2014 7: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;
}

Post Reply

Return to “General coding help”