Get MAC Address
Post your questions regarding programming in C++ in here.
2 posts
Page 1 of 1
Hello
I know in C# how can get MAC Address but in C++ I don't know.
C# look like:
I know in C# how can get MAC Address but in C++ I don't know.
C# look like:
Code: Select all
How can I do it in C++?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;
}
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023