How do I get the mac address/es in C for Windows XP?

M

marccruz

Is there an API where I can get the mac address/es of a computer using
C for Windows XP?
 
J

jmcgill

Is there an API where I can get the mac address/es of a computer using
C for Windows XP?

Before you get flamed for asking a not-strictly-on-topic question,
ponder this (from an old MSDN source). I'm a Unix systems programmer
that rarely touches windows, so you get the usual money back guarantee
on this:

// Fetches the MAC addresses and prints them
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
// for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save 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
}
 
J

jmcgill

goose said:
You mean that you'd rather get flamed instead of OP? How odd.

Sure, no problem. I honestly want to help, and I have very thick
asbestos and kevlar skin.
 
G

goose

jmcgill said:
Before you get flamed for asking a not-strictly-on-topic question,

You mean that you'd rather get flamed instead of OP? How odd.
ponder this (from an old MSDN source). I'm a Unix systems programmer
that rarely touches windows, so you get the usual money back guarantee
on this:

// Fetches the MAC addresses and prints them
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information

IP_ADAPTER_INFO is not a known type.
// for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer

instead of that use
sizeof AdapterInfo[0];
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo

DWORD? Whats that?
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

PIP_ADAPTER_INFO also not know at this point.
// current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address

'nother unknown function.
pAdapterInfo = pAdapterInfo->Next; // Progress through
// linked list
}
while(pAdapterInfo); // Terminate if last adapter
}

Sadly, even if there was a trivial error in the above
code, I would not ahve picked it up, as I am not a windows
developer (for example, I cannot understand why an array
is needed if the data is stored in a linked list).

That is a good reason to send the people asking platform
specific questions to the proper newsgroup, where experts
will attend to their questions. Answering badly here serves
two purposes:
1. The answer is not going to be verified as a good answer.
2. The newsgroup starts attracting so many platform questions
that the signal/noise ratio is going to put off people with
legitimate questions.

So, I urge you to direct the poster to the appropriate
newsgroup: clc is not the newsgroup for this question.

regards
goose,
 
J

jmcgill

goose said:
IP_ADAPTER_INFO is not a known type.
> DWORD? Whats that?
> PIP_ADAPTER_INFO also not know at this point.

#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <iphlpapi.h>


I'll try to refrain from answering any more windows programming
questions from now on. Just trying to help. Sheesh :)
 
K

Keith Thompson

jmcgill said:
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <iphlpapi.h>


I'll try to refrain from answering any more windows programming
questions from now on. Just trying to help. Sheesh :)

Thank you. I know you're trying to help, and I can certainly
understand the temptation, but really, the best help you can offer is
to redirect the questioner to some newsgroup that's full of actual
experts on the topic. (You may be an expert on this yourself, but
most of the rest of us aren't.)

Think about it. Suppose someone posted something like this to a
Windows programming group:

I need to allocate a 3-dimensional array in my C program, with the
length of each dimension determined at run time. What's the best
way to do this? (I'm programming in Windows, but I'd like the
code to be portable to other systems.)

Which would be more helpful, answering the question yourself in the
Windows group, or telling the poster about this other newsgroup that's
chock full of C geeks who will fall over themselves to give the best
possible advice for a portable solution?

(Or you could just point to question 6.16 of the comp.lang.c FAQ, but
let's pretend that it's something the FAQ doesn't address.)
 
A

Al Balmer

I'll try to refrain from answering any more windows programming
questions from now on. Just trying to help. Sheesh :)

The best help you can give is directing the OP to a newsgroup where he
can get the *correct* information, vetted by other readers who know
the subject.

What you did was not "help."
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top