Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting MAC Address (NetBIOS Method) within C/C++-program in Windows2000

16 views
Skip to first unread message

Alex Vinokur

unread,
Sep 4, 2002, 2:44:55 PM9/4/02
to
===============================
Windows 2000 Professional
-------------------
CYGWIN_NT-5
gcc/g++ version 2.95.3-5 (cygwin special)
-------------------
MINGW-1.1
gcc/g++ version 2.95.3-6 (mingw special)
===============================

Here is a program getmac-netbios.cpp "How to Get the Ethernet MAC Address, NetBIOS Method"
from http://tangentsoft.net/wskfaq/examples/getmac-netbios.html
P.S. The only line has been added :
cout << "MY INFO : AdapterList.length = " << static_cast<int>(AdapterList.length) << endl;

We can see that AdapterList.length == 0.
So, we can't get MAC address.

Is something wrong ?

P.S.S. The program was running while I was working offline. Is it essential for this program ?

========= C++ Code : BEGIN =========
// File x2.cpp

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <strstream>
#include <string>

using namespace std;

bool GetAdapterInfo(int nAdapterNum, string & sMAC)
{
// Reset the LAN adapter so that we can begin querying it
NCB Ncb;
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = nAdapterNum;
if (Netbios(&Ncb) != NRC_GOODRET) {
char acTemp[80];
ostrstream outs(acTemp, sizeof(acTemp));
outs << "error " << Ncb.ncb_retcode << " on reset" << ends;
sMAC = acTemp;
return false;
}

// Prepare to get the adapter status block
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = nAdapterNum;
strcpy((char *) Ncb.ncb_callname, "*");
struct ASTAT {
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[30];
} Adapter;
memset(&Adapter, 0, sizeof(Adapter));
Ncb.ncb_buffer = (unsigned char *)&Adapter;
Ncb.ncb_length = sizeof(Adapter);

// Get the adapter's info and, if this works, return it in standard,
// colon-delimited form.
if (Netbios(&Ncb) == 0) {
char acMAC[18];
sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
int (Adapter.adapt.adapter_address[0]),
int (Adapter.adapt.adapter_address[1]),
int (Adapter.adapt.adapter_address[2]),
int (Adapter.adapt.adapter_address[3]),
int (Adapter.adapt.adapter_address[4]),
int (Adapter.adapt.adapter_address[5]));
sMAC = acMAC;
return true;
}
else {
char acTemp[80];
ostrstream outs(acTemp, sizeof(acTemp));
outs << "error " << Ncb.ncb_retcode << " on ASTAT" << ends;
sMAC = acTemp;
return false;
}
}

int main()
{
// Get adapter list
LANA_ENUM AdapterList;
NCB Ncb;
memset(&Ncb, 0, sizeof(NCB));
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (unsigned char *)&AdapterList;
Ncb.ncb_length = sizeof(AdapterList);
Netbios(&Ncb);

// Get all of the local ethernet addresses
string sMAC;
cout << "MY INFO : AdapterList.length = " << static_cast<int>(AdapterList.length) << endl;
for (int i = 0; i < AdapterList.length; ++i) {
if (GetAdapterInfo(AdapterList.lana[i], sMAC)) {
cout << "Adapter " << int (AdapterList.lana[i]) <<
"'s MAC is " << sMAC << endl;
}
else {
cerr << "Failed to get MAC address! Do you" << endl;
cerr << "have the NetBIOS protocol installed?" << endl;
break;
}
}
return 0;
}


========= C++ Code : END ===========

========= Compilation & Run : BEGIN =========

% g++ x2.cpp -lnetapi32

% a
MY INFO : AdapterList.length = 0

========= Compilation & Run : END ===========


==================
Alex Vinokur
mailto:ale...@go.to
http://up.to/alexvn
==================


0 new messages