473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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;
}
---
Nov 16 '05 #1
5 3395
"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
> 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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon | last post by:
Hi all, I'm hoping that someone could help with the following general question: I'm making a website that manages beta releases of software. It does various things like store bugs, comments,...
1
by: jitendar.rawat | last post by:
Hi, I am trying to create a library and want its user to define a function in their executable if that library is linked. some thing like, if some uses my library and haven't defined a function...
1
by: pnr | last post by:
I am makeing a system where every user have there ovn menu, they can chose between about 20 different sides they want in there menu. Eache side contains that menu, the user logs in with username...
1
by: MailYouLike | last post by:
Hi ! i need some help and advice i am using asp.net to make a email application it is complete but just one thing , i want to create a pop account at the mail server from my scripts is...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
4
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r)...
1
by: SemSem | last post by:
iwant to know if there any way to make awell design forms as ithink .net can offered to me good design tools ineed to kow how idsign an asp well design site and if there techneques are used in...
3
by: skneife | last post by:
it seems that it is not possible to make a project Reference from a project class library to a website in the same solution, because the website doesn't contains any assembly (dll). So, if in the...
4
by: Nethali | last post by:
Hi folks, Is there any way to embed shared library directly, to executable file it self so that executable file can run stand alone without the shared library. I don't want to compile the...
5
by: puneetsardana88 | last post by:
Hi I tried to make a library using code blocks. For which I went file->new->project->static library and name it as mylib and then i created a file mylib1.c and mylib1.h (with some sample functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.