471,582 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

Getting MAC Address from a Network Card

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
Feb 7 '06 #1
3 5894
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.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 7 '06 #2
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.

"Mark Prenter" <an**@anon.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| 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
|
|
Feb 7 '06 #3
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::ManagementObjectEnumer ator* 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
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
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.

"Mark Prenter" <an**@anon.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| 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
|
|

Feb 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ben Xia | last post: by
9 posts views Thread by John J. Hughes II | last post: by
15 posts views Thread by Roberto Hernández | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.