Home All Groups Group Topic Archive Search About

Getting IP address & Physical Address from code in c#.

Author
6 Dec 2008 6:03 AM
Pioneer
Hi,

I need to get MAC address/physical ip address of machine. I am using
below code:


ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
string macAddr = mo["MACAddress"] as string;
if ( macAddr != null && macAddr.Trim() != "" )
return macAddr.ToString();



}


But this returns multiple IP addresses out of which Physical address
is shown twice and rest of the addresses I could not find in
ipconfig/
all command output on cmd.

Could you help me understand the output?


I only want the physical address. IF there is any better alternative,
kindly let me know.


Thanks
Pioneer

Author
6 Dec 2008 1:10 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 6, 1:03 am, Pioneer <adiid***@gmail.com> wrote:
Show quoteHide quote
> Hi,
>
> I need to get MAC address/physical ip address of machine. I am using
> below code:
>
> ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
> foreach (ManagementObject mo in mc.GetInstances())
> {
> string macAddr = mo["MACAddress"] as string;
> if ( macAddr != null && macAddr.Trim() != "" )
> return macAddr.ToString();
>
> }
>
> But this returns multiple IP addresses out of which Physical address
> is shown twice and rest of the addresses I could not find in
> ipconfig/
> all command output on cmd.
>
> Could you help me understand the output?
>
> I only want the physical address. IF there is any better alternative,
> kindly let me know.
>
> Thanks
> Pioneer

check for the AdapterType.
Also check the archives, I'm pretty sure this has been answered before
Are all your drivers up to date? click for free checkup

Author
6 Dec 2008 4:01 PM
Mark Rae [MVP]
"Pioneer" <adiid***@gmail.com> wrote in message
news:1170d9a6-c413-4f53-8158-b02bcaad8b7c@w24g2000prd.googlegroups.com...

> I only want the physical address. IF there is any better alternative,
> kindly let me know.

ManagementClass objMC = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection colMO = objMC.GetInstances();
string strMACAddress = String.Empty;
foreach (ManagementObject objMO in colMO)
{
    if (strMACAddress == String.Empty)
    {
        if ((bool)objMO["IPEnabled"])
        {
            strMACAddress = objMO["MacAddress"].ToString();
        }
    }
    objMO.Dispose();
}
return strMACAddress;


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
6 Dec 2008 5:17 PM
Ignacio Machin ( .NET/ C# MVP )
Show quote Hide quote
On Dec 6, 11:01 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Pioneer" <adiid***@gmail.com> wrote in message
>
> news:1170d9a6-c413-4f53-8158-b02bcaad8b7c@w24g2000prd.googlegroups.com...
>
> > I only want the physical address. IF there is any better alternative,
> > kindly let me know.
>
> ManagementClass objMC = new
> ManagementClass("Win32_NetworkAdapterConfiguration");
> ManagementObjectCollection colMO = objMC.GetInstances();
> string strMACAddress = String.Empty;
> foreach (ManagementObject objMO in colMO)
> {
>     if (strMACAddress == String.Empty)
>     {
>         if ((bool)objMO["IPEnabled"])
>         {
>             strMACAddress = objMO["MacAddress"].ToString();
>         }
>     }
>     objMO.Dispose();}
>
> return strMACAddress;
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

I think that sometimes a virtual device will also support IP,  I've in
mind when a 2003 PPC sync with Active Sync the later create a virtual
device with IP 192.168.55.100
Author
6 Dec 2008 6:47 PM
Mark Rae [MVP]
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
message
news:26f5f229-fe6a-42a8-a597-e465285bf964@v13g2000yqm.googlegroups.com...

> I think that sometimes a virtual device will also support IP,  I've in
> mind when a 2003 PPC sync with Active Sync the later create a virtual
> device with IP 192.168.55.100

So what would you suggest...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
6 Dec 2008 11:42 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 6, 1:47 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> messagenews:26f5f229-fe6a-42a8-a597-e465285bf***@v13g2000yqm.googlegroups..com...
>
> > I think that sometimes a virtual device will also support IP,  I've in
> > mind when a 2003 PPC sync with Active Sync the later create a virtual
> > device with IP 192.168.55.100
>
> So what would you suggest...?
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

To check other properties, like AdapterType. The fact that an adapter
support IP does not garantee that it;s a physical device
Author
7 Dec 2008 12:06 AM
Ignacio Machin ( .NET/ C# MVP )
On Dec 6, 6:42 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac***@gmail.com> wrote:
Show quoteHide quote
> On Dec 6, 1:47 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
>
> > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> > messagenews:26f5f229-fe6a-42a8-a597-e465285bf***@v13g2000yqm.googlegroups.com...
>
> > > I think that sometimes a virtual device will also support IP,  I've in
> > > mind when a 2003 PPC sync with Active Sync the later create a virtual
> > > device with IP 192.168.55.100
>
> > So what would you suggest...?
>
> > --
> > Mark Rae
> > ASP.NET MVPhttp://www.markrae.net
>
> To check other properties, like AdapterType. The fact that an adapter
> support IP does not garantee that it;s a physical device

The correct property is DeviceTypeId
and of course check that the MAC exist , in my computer (XP home) some
of the devices have no MAC address
Author
7 Dec 2008 1:12 AM
Mark Rae [MVP]
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
message
news:623d4c57-eb03-4165-9f6c-f66e3cd25edf@f3g2000yqf.googlegroups.com...

> The correct property is DeviceTypeId

Where are you retrieving that? It doesn't seem to be part either of the
Win32_NetworkAdapter or the Win32_NetWorkAdapterConfiguration class...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
7 Dec 2008 5:55 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 6, 8:12 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> messagenews:623d4c57-eb03-4165-9f6c-f66e3cd25***@f3g2000yqf.googlegroups.com...
>
> > The correct property is DeviceTypeId
>
> Where are you retrieving that? It doesn't seem to be part either of the
> Win32_NetworkAdapter or the Win32_NetWorkAdapterConfiguration class...
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

MSDN:
http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx

Win32_NetworkAdapter Class
class Win32_NetworkAdapter : CIM_NetworkAdapter
{
  string AdapterType;
  uint16 AdapterTypeID;
Author
7 Dec 2008 8:56 PM
Mark Rae [MVP]
Show quote Hide quote
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
message
news:29a9c034-16b2-4cf6-9a3d-818a58847594@x8g2000yqk.googlegroups.com...

>>> The correct property is DeviceTypeId
>>
>> Where are you retrieving that? It doesn't seem to be part either of the
>> Win32_NetworkAdapter or the Win32_NetWorkAdapterConfiguration class...
>
> MSDN:
> http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx
>
> Win32_NetworkAdapter Class
> class Win32_NetworkAdapter : CIM_NetworkAdapter
> {
>  string AdapterType;
>  uint16 AdapterTypeID;

Like I said, where are you retrieving a property called DeviceTypeId...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
16 Dec 2008 4:25 AM
Pioneer
Show quote Hide quote
On Dec 8, 1:56 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> messagenews:29a9c034-16b2-4cf6-9a3d-818a58847***@x8g2000yqk.googlegroups.com...
>
> >>> The correct property is DeviceTypeId
>
> >> Where are you retrieving that? It doesn't seem to be part either of the
> >> Win32_NetworkAdapter or the Win32_NetWorkAdapterConfiguration class...
>
> > MSDN:
> >http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx
>
> > Win32_NetworkAdapter Class
> > class Win32_NetworkAdapter : CIM_NetworkAdapter
> > {
> >  string AdapterType;
> >  uint16 AdapterTypeID;
>
> Like I said, where are you retrieving a property called DeviceTypeId...?
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Hi,
The code worked just fine!! Thanks a lot.

I have one more query. Does MAC address change due to any reason? I
believe as its related to hardware of the PC, it should not/never
change.

Best Regards,
Aditya
Author
16 Dec 2008 4:43 AM
Peter Duniho
On Mon, 15 Dec 2008 20:25:05 -0800, Pioneer <adiid***@gmail.com> wrote:

> I have one more query. Does MAC address change due to any reason? I
> believe as its related to hardware of the PC, it should not/never
> change.

The MAC address can change for any variety of reasons.  Nominally, it's 
embedded in the network adapter, but not all things that look like network 
adapters are actually real hardware, and even hardware network adapaters 
often allow the user to configure the MAC address to be whatever they 
want.  Never mind that any given computer may well have multiple devices 
with MAC addresses, any of which may or may not be visible at any given 
moment (due to being disabled or other).

The fact is, if you think you need the MAC address and you're not writing 
a network analysis program, you're almost certainly wrong about needing 
the MAC address.  It definitely is not a way to uniquely identify the 
computer, in case that's what you're thinking of using it for.

Pete
Author
16 Dec 2008 10:20 AM
Pioneer
Show quote Hide quote
On Dec 16, 9:43 am, "Peter Duniho" <NpOeStPe***@nnowslpianmk.com>
wrote:
> On Mon, 15 Dec 2008 20:25:05 -0800, Pioneer <adiid***@gmail.com> wrote:
> > I have one more query. Does MAC address change due to any reason? I
> > believe as its related to hardware of the PC, it should not/never
> > change.
>
> The MAC address can change for any variety of reasons.  Nominally, it's  
> embedded in the network adapter, but not all things that look like network  
> adapters are actually real hardware, and even hardware network adapaters  
> often allow the user to configure the MAC address to be whatever they  
> want.  Never mind that any given computer may well have multiple devices  
> with MAC addresses, any of which may or may not be visible at any given  
> moment (due to being disabled or other).
>
> The fact is, if you think you need the MAC address and you're not writing  
> a network analysis program, you're almost certainly wrong about needing  
> the MAC address.  It definitely is not a way to uniquely identify the  
> computer, in case that's what you're thinking of using it for.
>
> Pete

Thats interesting. I precisely need to make sure that I minimize the
piracy of a program that I am gonna deliver.
Host name, IP address and mac address are the ways I thought of. I
belive in this scenario, does host name fits better?
Author
16 Dec 2008 10:43 AM
Manu
On Dec 16, 3:20 pm, Pioneer <adiid***@gmail.com> wrote:
Show quoteHide quote
> On Dec 16, 9:43 am, "Peter Duniho" <NpOeStPe***@nnowslpianmk.com>
> wrote:
>
>
>
> > On Mon, 15 Dec 2008 20:25:05 -0800, Pioneer <adiid***@gmail.com> wrote:
> > > I have one more query. Does MAC address change due to any reason? I
> > > believe as its related to hardware of the PC, it should not/never
> > > change.
>
> > The MAC address can change for any variety of reasons.  Nominally, it's  
> > embedded in the network adapter, but not all things that look like network  
> > adapters are actually real hardware, and even hardware network adapaters  
> > often allow the user to configure the MAC address to be whatever they  
> > want.  Never mind that any given computer may well have multiple devices  
> > with MAC addresses, any of which may or may not be visible at any given  
> > moment (due to being disabled or other).
>
> > The fact is, if you think you need the MAC address and you're not writing  
> > a network analysis program, you're almost certainly wrong about needing  
> > the MAC address.  It definitely is not a way to uniquely identify the  
> > computer, in case that's what you're thinking of using it for.
>
> > Pete
>
> Thats interesting. I precisely need to make sure that I minimize the
> piracy of a program that I am gonna deliver.
> Host name, IP address and mac address are the ways I thought of. I
> belive in this scenario, does host name fits better?

It depends on your policy.
I would never use MAC address or any other hardware/resource id as a
means to tackle software piracy.
Before you think about using Hardware resources for this purpose
Critically examine all the options.
The user of of your application might change its hardware someday (due
to any reason) and having a failed application due to that reason is
really troublesome and irritating.
You can use some way like using some kind of HASP device if you still
want to tie it with hardware. Consider using Aladdin's Products.
Author
16 Dec 2008 11:10 AM
Peter Duniho
On Tue, 16 Dec 2008 02:20:49 -0800, Pioneer <adiid***@gmail.com> wrote:

> Thats interesting. I precisely need to make sure that I minimize the
> piracy of a program that I am gonna deliver.

I figured as much.

Keep in mind, those who know me know I have a strong dislike of any kind 
of DRM, including copy protection.  As an end user, I have many times had 
otherwise-usable, legally licensed products rendered useless because of 
DRM.

I will point out the same thing to you that I point out to anyone else who 
brings it up: DRM never stops the people who want to copy your code, but 
it often does stop the people who have already paid for a legitimate 
license for your code.  Anyone who can write code can understand why those 
facts mean that DRM is logically the opposite of what a responsible 
software developer will do.

If you are interested in more extensive essays on the topic from me, you 
can search this newsgroup for previous posts I've made on the subject.  
Though, frankly I have found that by the time a person winds up here 
asking how to do copy protection, they have already made up their mind and 
no amount of new information or logic will dissuade them.

> Host name, IP address and mac address are the ways I thought of. I
> belive in this scenario, does host name fits better?

None of those are reliable ways of uniquely identifying a computer.  In 
fact, there is _no_ reliable way of uniquely identifying a computer.  If 
there were, Microsoft's own "Product Activation" crap would actually 
work.  Instead, I regularly have to re-activate my Microsoft software 
because of some subtle change to my computer that makes it think I've 
changed my computer when I haven't (and of course, the corallary is that 
someone who wants to copy your software can cause more than one computer 
to look exactly like each other).

It is nearly impossible to do reliable, user-friendly copy protection.  A 
hardware dongle is about as close as you can get, and even that isn't 
hacker-proof.  But it at least carries the least risk of users being 
locked out of their own software (but there is still the risk, due to the 
user losing the dongle, or a bug in your code, or whatever).

If you want to do copy protection, I recommend you don't attempt to do 
anything strong.  One of the least-intrusive implementations I've seen 
uses an encrypted key that the user has to enter in order to get the 
program to run.  Depending on how long you want to make the key, you can 
put user identifying information in the key.  In any case, such a system 
doesn't prevent a user from sharing his key with someone else, but it 
provides enough of a barrier that casual copiers won't bother, without 
creating a situation that has a high chance of failing when a legitimate 
user wants to use the software.

Pete
Author
16 Dec 2008 1:53 PM
Matthias Krug
Peter Duniho schrieb:

> I will point out the same thing to you that I point out to anyone else
> who brings it up: DRM never stops the people who want to copy your code,
> but it often does stop the people who have already paid for a legitimate
> license for your code.  Anyone who can write code can understand why
> those facts mean that DRM is logically the opposite of what a
> responsible software developer will do.

100% !!!

We have one CAD software which maybe great for its original purpose, but
is literally unusable - at least if you do care for efficiency.

We paid (and keep paying annual fees) for 9 seats. The license model is
a per seat/per connection model, _NOT_ a named user one.

Every now and then the complete software won't work because of some
minor bug in the DRM modules. Sometimes, because of that, we are losing
half a day for 9 expert(= high salary) CAD workers PLUS 1 from IT dept.

And it is - of course - impossible to hire part time employees, unless
all of them will share indeed the same office with the same machine.

We struggled for 2 years now to achieve an improvement, but there won't
be any.

Consequence: we are direly looking for an alternative and soon will
change the CAD application. That company is losing 9 licenses and an
annual turnover of 18.000 Euro.

Meanwhile, all those who won't care can just go to one of many sites in
the web and get a crack which enables them to use the application
without any payment at all on as many machines as they desire to do.

We did so for testing purposes once. Works like a charm. But paying for
all software we use is not for discussion.
Author
16 Dec 2008 10:30 AM
Mark Rae [MVP]
"Pioneer" <adiid***@gmail.com> wrote in message
news:011fdcd1-cc7b-41d7-92b0-5ede1ecbda39@v39g2000pro.googlegroups.com...

> I have one more query. Does MAC address change due to any reason?
> I believe as its related to hardware of the PC, it should not/never
> change.

Most modern Ethernet cards allow their MAC address to be changed (at least,
as far as the rest of the PC is concerned) as part of their configuration:
http://www.thinkdigit.com/forum/showthread.php?t=41503


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
7 Dec 2008 1:07 AM
Mark Rae [MVP]
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
message
news:a18e6783-805b-4e44-b031-c42560c222f8@j39g2000yqn.googlegroups.com...

>>> I think that sometimes a virtual device will also support IP, I've in
>>> mind when a 2003 PPC sync with Active Sync the later create a virtual
>>> device with IP 192.168.55.100
>>
>> So what would you suggest...?
>
> To check other properties, like AdapterType. The fact that an adapter
> support IP does not garantee that it;s a physical device

OK, so what do you think of this...?

ManagementClass objMC = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection colMO = objMC.GetInstances();
string strMACAddress = String.Empty;
foreach (ManagementObject objMO in colMO)
{
    if (objMO["AdapterType"] != null)
    {
        if (objMO["AdapterType"].ToString().StartsWith("Ethernet"))
        {
            if ((bool)objMO["NetEnabled"])
            {
                strMACAddress = objMO["MacAddress"].ToString();
                break;
            }
        }
    }
    objMO.Dispose();
}

On the machine on which I tested the above code I have two physical NICs,
one of which is disabled, as well as a Wi-Fi adapter, a BlueTooth adapter
and several virtual adapters including the two VMWare ones - the above code
correctly returned the MAC address of the enabled Ethernet NIC...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Bookmark and Share