By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,155 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,155 IT Pros & Developers. It's quick & easy.

How to make a library with this?

P: n/a
Hello all.

The code bellow prints (in the command line window) the MAC ADDRESSES of all
NIC cards enabled in the computer.

What I need is: a library based on this code, that will return a string with
the output of this command line tool. I need to use this library in my C#
(.NET) program. In other words, convert this native .EXE in a library that I
could use in my managed C# application.

This is really very important to me, if anyone can help me, I'd be forever
thankful.

Leo

---

#include "stdafx.h"
#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")

// Prints the MAC address stored in a 6 byte array to stdout
static void PrintMACaddress(unsigned char MACData[])
{
printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4],
MACData[5]);
}

// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size
of buffer

DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to
receive data
&dwBufLen); // [in] size of
receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value
is valid, no buffer overflow

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to
current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through
linked list
}
while(pAdapterInfo); // Terminate if last
adapter
}

int _tmain(int argc, _TCHAR* argv[])
{
GetMACaddress(); // Obtain MAC
address of adapters

return 0;
}
---
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Leonardo D'Ippolito" schrieb
The code bellow prints (in the command line window) the MAC ADDRESSES of
all
NIC cards enabled in the computer.

What I need is: a library based on this code, that will return a string
with
the output of this command line tool. I need to use this library in my C#
(.NET) program. In other words, convert this native .EXE in a library that
I
could use in my managed C# application.

This is really very important to me, if anyone can help me, I'd be forever
thankful.

Leo

---

#include "stdafx.h"
#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")

// Prints the MAC address stored in a 6 byte array to stdout
static void PrintMACaddress(unsigned char MACData[])
{
printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4],
MACData[5]);
}

// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size
of buffer

DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to
receive data
&dwBufLen); // [in] size
of
receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return
value
is valid, no buffer overflow

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to
current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through
linked list
}
while(pAdapterInfo); // Terminate if last
adapter
}

int _tmain(int argc, _TCHAR* argv[])
{
GetMACaddress(); // Obtain MAC
address of adapters

return 0;
}


Hi Leonardo,

I think you're going the wrong way.

We have excellent support for WMI in .NET, so we should use it.

The following code shows the MAC-Adresses for all IP-Enabled devices:
it will be easy for you to adapt this code to return a string by using a
string-array, ArrayList, or whatever.

//add a reference to System.Management.dll
using System.Management;

ManagementClass mc = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
{
Debug.WriteLine(
"MAC-Address: " + mo["MacAddress"].ToString());
}
}

Cheers

Arne Janning
Nov 16 '05 #2

P: n/a
> I think you're going the wrong way.

We have excellent support for WMI in .NET, so we should use it.


Thanks Arne, but I can't use WMI, since some clients are still Windows
98 and they don't have WMI service by default. I need a solution without
WMI.

[]'s

Leonardo
Nov 16 '05 #3

P: n/a
Dear Leonardo,
"Leonardo D'Ippolito" schrieb
Thanks Arne, but I can't use WMI, since some clients are still Windows
98 and they don't have WMI service by default. I need a solution without
WMI.


I have searched for a solution without WMI, but there is no working code on
groups.google.com or anywhere else on the net.

You'll have to deploy WMI on your target machines:
http://www.microsoft.com/downloads/d...displaylang=en

I'm sorry, but I think this is the only solution.

Cheers

Arne Janning
Nov 16 '05 #4

P: n/a
> It makes little sense to write a C library to encapsulate a C API call,
you
still have to PInvoke the library function.
Better use PInvoke to call the API directly.
Here's how you can call GetAdaptersInfo from C# using PInvoke.
Thanks Denoyette. It worked perfectly.
// Note this should only be used on Win98, NT4 an W2K not on XP and higher
[DllImport("IphlpApi", CharSet=CharSet.Ansi, SetLastError=true),


Why not XP and higher? Isn't there compatibility with this DLL ?
Leonardo
Nov 16 '05 #5

P: n/a
On XP and higher it's preferable to use GetAdaptersAddresses.
Check the Platform SDK doc's for details.
Willy.

"Leonardo D'Ippolito" <le**********@terra.com.br> wrote in message
news:ux*************@TK2MSFTNGP11.phx.gbl...
It makes little sense to write a C library to encapsulate a C API call,

you
still have to PInvoke the library function.
Better use PInvoke to call the API directly.
Here's how you can call GetAdaptersInfo from C# using PInvoke.


Thanks Denoyette. It worked perfectly.
// Note this should only be used on Win98, NT4 an W2K not on XP and
higher
[DllImport("IphlpApi", CharSet=CharSet.Ansi, SetLastError=true),


Why not XP and higher? Isn't there compatibility with this DLL ?
Leonardo

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.