Getting MAC Address from a Network Card

M

Mark Prenter

Hello, I'm trying to find a way to retrieve the MAC address from a network
card in a Visual C++ .NET managed application.

I've found some examples in C#, but I just can't get them to work in C++.
Here's what I've found in C#:

using System.Management;
.....
ManagementClass mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address\t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}

Now, I'm trying to convert this to make it usable in C++, but the second
line has me thrown for a loop. Here's the beginning of what I've got :

#using <system.management.dll>
using namespace System::Management;

int _tmain(void)
{
ManagementClass* mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
return 0;
}

But even this won't compile....giving me the error
"System::ComponentModel::Component", the compiler cannot find this type; it
is defined in the assembly 'System'

Can anyone help me out with this? Please? I'm losing hair by the hour.
:)

/\/\ark
 
B

Bruno van Dooren

Hello Mark,
Now, I'm trying to convert this to make it usable in C++, but the second
line has me thrown for a loop. Here's the beginning of what I've got :

#using <system.management.dll>
using namespace System::Management;

int _tmain(void)
{
ManagementClass* mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
return 0;
}

But even this won't compile....giving me the error
"System::ComponentModel::Component", the compiler cannot find this type;
it is defined in the assembly 'System'

Did you create a new console project for this?
I've pasted your code in a new Managed console project, and it compiles
without a problem.
#include "stdafx.h"
#using <mscorlib.dll>
#using <system.management.dll>

using namespace System;
using namespace System::Management;

int _tmain()
{
Console::WriteLine(S"Hello World");
ManagementClass* mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
return 0;
}

If you reused an older project or something, then try starting from a new
console project.
If that doesn't work, then maybe you can post a reproduction project?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
W

Willy Denoyette [MVP]

The error message clearly states that you are missing an assembly reference,
it also tells you which assembly contains the type, so you need to add this:

#using <system.dll>


Willy.

| Hello, I'm trying to find a way to retrieve the MAC address from a network
| card in a Visual C++ .NET managed application.
|
| I've found some examples in C#, but I just can't get them to work in C++.
| Here's what I've found in C#:
|
| using System.Management;
| ....
| ManagementClass mc = new
| ManagementClass("Win32_NetworkAdapterConfiguration");
| ManagementObjectCollection moc = mc.GetInstances();
| foreach(ManagementObject mo in moc)
| {
| if((bool)mo["IPEnabled"] == true)
| Console.WriteLine("MAC address\t{0}", mo["MacAddress"].ToString());
| mo.Dispose();
| }
|
| Now, I'm trying to convert this to make it usable in C++, but the second
| line has me thrown for a loop. Here's the beginning of what I've got :
|
| #using <system.management.dll>
| using namespace System::Management;
|
| int _tmain(void)
| {
| ManagementClass* mc = new
| ManagementClass("Win32_NetworkAdapterConfiguration");
| return 0;
| }
|
| But even this won't compile....giving me the error
| "System::ComponentModel::Component", the compiler cannot find this type;
it
| is defined in the assembly 'System'
|
| Can anyone help me out with this? Please? I'm losing hair by the hour.
| :)
|
| /\/\ark
|
|
 
M

Mark Prenter

Thanks to all for the replies. I've never used ManagementObjects before so
I guess I have quite a bit of learning to do.

For those of you playing along at home.....here's the translated C++ .NET
code for looping through all network adapters and retrieving the MAC
addresses:

#include "stdafx.h"
#include <tchar.h>

#using <mscorlib.dll>
#using <system.management.dll>
#using <system.dll>

using namespace System;
using namespace System::Management;
using namespace System::Collections;

// This is the entry point for this application
int _tmain(void)
{
ManagementClass* mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection* moc = mc->GetInstances();
ManagementObjectCollection::ManagementObjectEnumerator* mo =
moc->GetEnumerator();
while (mo->MoveNext())
{
ManagementBaseObject* CurrentManagementObject = mo->get_Current();
Object* IPEnabled =
CurrentManagementObject->GetPropertyValue(L"IPEnabled");
if (Convert::ToBoolean(IPEnabled->ToString()) == true)
{
Object* MacAddress =
CurrentManagementObject->GetPropertyValue(L"MacAddress");
Console::WriteLine("MAC address\t{0}", MacAddress->ToString());
}
}
return 0;
}

Thanks again!!!!

/\/\ark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top