70,022
社区成员




#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, * PASTAT;
ASTAT Adapter;
void main (void)
{
NCB Ncb;
UCHAR uRetCode;
//char NetName[50];
LANA_ENUM lenum;
int i;
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios( &Ncb );
printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
for(i=0; i < lenum.length ;i++)
{
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios( &Ncb );
printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
memset( &Ncb, 0, sizeof (Ncb) );
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy( Ncb.ncb_callname, "* " );
Ncb.ncb_buffer = (char *) &Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &Ncb );
printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
if ( uRetCode == 0 )
{
printf( "The Ethernet Number on LANA %d is: %02x%02x%02x%02x%02x%02x\n",
lenum.lana[i],
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5] );
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
void err_sys(const char *errmsg);
int main(void)
{
int i, sockfd;
struct ifreq ifr;
struct arpreq arpr;
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
err_sys("socket");
/* get ip address */
if (ioctl(sockfd, SIOCGIFADDR, &ifr) == -1)
err_sys("1-ioctl");
/* get hardware address */
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)
err_sys("2-ioctl");
/* output hardware address */
for (i = 0; i < 6; i++) {
unsigned char *mac = (unsigned char *) ifr.ifr_hwaddr.sa_data;
printf("%x", (int) mac[i]);
if (i != 5)
printf("%c", ':');
}
exit(0);
}
void err_sys(const char *errmsg)
{
perror(errmsg);
exit(1);
}
#include <winsock2.h>
#include <Iphlpapi.h>
#include <stdio.h>
void byte2Hex(unsigned char bData,unsigned char hex[])
{
int high=bData/16,low =bData %16;
hex[0] = (high <10)?('0'+high):('A'+high-10);
hex[1] = (low <10)?('0'+low):('A'+low-10);
}
int getLocalMac(unsigned char *mac) //获取本机MAC地址
{
ULONG ulSize=0;
PIP_ADAPTER_INFO pInfo=NULL;
int temp=0;
temp = GetAdaptersInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfo=(PIP_ADAPTER_INFO)malloc(ulSize);
temp = GetAdaptersInfo(pInfo,&ulSize);
int iCount=0;
while(pInfo)//遍历每一张网卡
{
// pInfo->Address 是MAC地址
for(int i=0;i<(int)pInfo->AddressLength;i++)
{
byte2Hex(pInfo->Address[i],&mac[iCount]);
iCount+=2;
if(i<(int)pInfo->AddressLength-1)
{
mac[iCount++] = ':';
}else
{
mac[iCount++] = '#';
}
}
pInfo = pInfo->Next;
}
if(iCount >0)
{
mac[--iCount]='\0';
return iCount;
}
else return -1;
}
int main(int argc, char* argv[])
{
unsigned char address[1024];
if(getLocalMac(address)>0)
{
printf("mac-%s\n",address);
}else
{
printf("invoke getMAC error!\n");
}
return 0;
}