How to get the MAC address

  • Thread starter Thread starter Roman Sallin
  • Start date Start date
R

Roman Sallin

How can I get the MAC address from the network interface card? Is there a
class in the .NET framework that does the job? I wasn't able to find
anything.

Thanks,
Roman Sallin
 
Roman Sallin said:
How can I get the MAC address from the network interface card? Is there a
class in the .NET framework that does the job? I wasn't able to find
anything.

Roman, keep in mind that there may be no NIC card, or there may be more than
one.
 
You can use WMI (from the System.Management namespace) to get the MAC
address(es) (see below).

Regards,
Martin Bischoff

-----------
using System;
using System.Management;

class Sample_ManagementObjectSearcher
{
public static int Main(string[] args)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from win32_networkadapter");

foreach (ManagementObject adapter in searcher.Get())
{
Console.WriteLine("Adapter Name: " + adapter["name"]);
Console.WriteLine("Adapter Type: " + adapter["AdapterType"]);
Console.WriteLine("MAC Address: " + adapter["MACAddress"]);
Console.WriteLine("\n");
}
return 0;
}
}
 
Martin Bischoff said:
You can use WMI (from the System.Management namespace) to get the MAC
address(es) (see below).

Martin,

Yes, you can use WMI to get the MAC address:

1) Assuming it exists.
2) If there is more than one NIC, which address should you pick as being
"the MAC address?".
3) What if the MAC addresses are not unique (00-00-00-50-50-50 comes to
mind)?
4) Also, your code might want to display whether the adapter is enabled or
not. The address of a disabled adapater might not be useful.

The MAC address is something that's easy to display, but harder to use as
useful data.
 
Martin Bischoff said:
Hi John,
your comments are certainly correct, but I just wanted to show a way to
get the MAC addresses, nothing more.

For some applications it might be sufficient to check whether a specific
MAC address can be found on one of the NICs (e.g. for a simple licensing
solution, to restrict your software to a specific computer).

Martin, this is _exactly_ what I'm trying to discourage!

A software licensing scheme shouldn't break when I change NIC cards (or
upgrade my motherboard, or add memory or disk drives, or replace disk
drives). None of those things are about licensing, so people expect that
changing the hardware will not change software licensing.
 
Thanks for the sample. In the meantime I found an almost identical piece of
VB.NET code:

Imports System.Management
Module Module1
Sub Main()
Dim query As New ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration")
Dim obj As Management.ManagementBaseObject
For Each obj In query.Get()
If Not obj("MacAddress") Is Nothing Then
Console.WriteLine(obj("Caption") & " " & obj("MacAddress"))
End If
Next
Console.ReadLine()
End Sub
End Module

BTW: I use this for licensing because it's a simple way to have a PC
identified. Users with more than one NIC are extremly rare (in my business).
Users without a NIC are no problem for my licencing software piece.

Best regards
Roman

Martin Bischoff said:
You can use WMI (from the System.Management namespace) to get the MAC
address(es) (see below).

Regards,
Martin Bischoff

-----------
using System;
using System.Management;

class Sample_ManagementObjectSearcher
{
public static int Main(string[] args)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from win32_networkadapter");

foreach (ManagementObject adapter in searcher.Get())
{
Console.WriteLine("Adapter Name: " + adapter["name"]);
Console.WriteLine("Adapter Type: " + adapter["AdapterType"]);
Console.WriteLine("MAC Address: " + adapter["MACAddress"]);
Console.WriteLine("\n");
}
return 0;
}
}
-----------

Roman said:
How can I get the MAC address from the network interface card? Is there a
class in the .NET framework that does the job? I wasn't able to find
anything.

Thanks,
Roman Sallin
 
Roman Sallin said:
BTW: I use this for licensing because it's a simple way to have a PC
identified. Users with more than one NIC are extremly rare (in my business).
Users without a NIC are no problem for my licencing software piece.

Roman,

If it works for you, great. Good luck.

What I've been trying to express (and failing to express!) is that it won't
necessarily work for long. For instance, will it work when a customer
changes NIC cards, or moves to a new PC? Will it work if the customer had a
NIC card when he installed the product, but then tries to use the product
while his NIC card is not installed?

If so, then you've got an interesting set of customers, and they're
different from any customers I've ever had! My customers haven't been the
type who like to call the software vendor they've paid money to, just
because they changed a piece of hardware. They especially don't like it when
they change their hardware at a time of day when the vendors customer
support staff are not available.

This is a lesson which has been learned by many software vendors (including
Microsoft). So, welcome, Student. Class is in session.
 
If you use hardware as a way to restrict a license, you should supply a way
for users to transfer their license. You should also be supplying a phone
number or web site for users to get a new license if they changed their
hardware before they transferred the license.

If you are really afraid of piracy, use a subscription licensing model.
Your users must have at least a periodic internet connection because your
software will need to use a webservice. Save the licensee's information
locally along with a short timespan license file (4 weeks?) Each time an
internet connection is detected, but no more than once a week, recheck the
web service and update the local license file. The locally stored license
file can then be tied to the hardware, such as the mac address. If they
upgrade hardware, all that is required to continue running the application
is a web connection to update the local license file.

If you are going to be charging the user on a regular basis, make sure you
make it worthwhile by supplying regular updates with new features. However,
just because you license with the subscription model, doesn't mean you need
to charge on a regular basis (and thus supply updates).

This subscription model is currently used by "Revit" (used by architects)
now owned by "AutoDesk".
 
Back
Top