Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find IP's of NIC's on PC & Network? 1

Status
Not open for further replies.

cjtaylor

Programmer
Aug 12, 2001
69
US
How would one go about finding the IP address of the NIC on your PC and across the network?

Thanks
Chris Taylor
 

Try this one, I got it off of a website, but I don't know whick one. There are 1 other methods of finding it if this one doesn't work. Just tell me if you need them. It might get a little confusing if I stick all three methods in.


// Borland C++ 5.0: bcc32 getmac-netbios.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 << &quot;error &quot; << Ncb.ncb_retcode << &quot; on reset&quot; << 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, &quot;*&quot;);
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, &quot;%02X:%02X:%02X:%02X:%02X:%02X&quot;,
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 << &quot;error &quot; << Ncb.ncb_retcode << &quot; on ASTAT&quot; << 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;
for (int i = 0; i < AdapterList.length; ++i) {
if (GetAdapterInfo(AdapterList.lana, sMAC)) {
cout << &quot;Adapter &quot; << int (AdapterList.lana) <<
&quot;'s MAC is &quot; << sMAC << endl;
}
else {
cerr << &quot;Failed to get MAC address! Do you&quot; << endl;
cerr << &quot;have the NetBIOS protocol installed?&quot; << endl;
break;
}
}

return 0;
}


Best of luck, Cyprus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top