How to make a library with this? | | |
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;
}
--- | | | | re: How to make a library with this?
"Leonardo D'Ippolito" schrieb[color=blue]
> 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;
> }[/color]
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 | | | | re: How to make a library with this?
> I think you're going the wrong way.[color=blue]
>
> We have excellent support for WMI in .NET, so we should use it.[/color]
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 | | | | re: How to make a library with this?
Dear Leonardo,
[color=blue]
> "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.[/color]
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 | | | | re: How to make a library with this?
> It makes little sense to write a C library to encapsulate a C API call,
you[color=blue]
> 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.[/color]
Thanks Denoyette. It worked perfectly.
[color=blue]
> // Note this should only be used on Win98, NT4 an W2K not on XP and higher
> [DllImport("IphlpApi", CharSet=CharSet.Ansi, SetLastError=true),[/color]
Why not XP and higher? Isn't there compatibility with this DLL ?
Leonardo | | | | re: How to make a library with this?
On XP and higher it's preferable to use GetAdaptersAddresses.
Check the Platform SDK doc's for details.
Willy.
"Leonardo D'Ippolito" <leodippolito@terra.com.br> wrote in message
news:uxiMMdQpEHA.536@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
>> It makes little sense to write a C library to encapsulate a C API call,[/color]
> you[color=green]
>> 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.[/color]
>
> Thanks Denoyette. It worked perfectly.
>[color=green]
>> // Note this should only be used on Win98, NT4 an W2K not on XP and
>> higher
>> [DllImport("IphlpApi", CharSet=CharSet.Ansi, SetLastError=true),[/color]
>
> Why not XP and higher? Isn't there compatibility with this DLL ?
>
>
> Leonardo
>
>[/color] |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|