求用纯C获MAC地址的源码(不要Netbios实现的)

samchang 2007-10-29 05:33:12
求用纯C获MAC地址的源码(不要Netbios实现的),翻遍了论坛都找不到完整的可以编译成功例子,不是缺声明,就是缺头函数.
...全文
907 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuhuaitao226 2007-10-30
  • 打赏
  • 举报
回复
搞笑,还纯C,C语言支持网络特性吗?
你发问题时先把基本的理论东西搞清楚好不好?
BeRoy 2007-10-30
  • 打赏
  • 举报
回复
你既然要求纯C,还管他什么Unix和Win平台嘛..

GetAdaptersInfo是WIN32的API.
BeRoy 2007-10-30
  • 打赏
  • 举报
回复
你既然要求纯C,还管他什么Unix和Win平台嘛..

GetAdaptersInfo是WIN32的API.
fly4free 2007-10-30
  • 打赏
  • 举报
回复
C语言不支持网络特性?
从哪个角度说的?

我还说C语言不支持文件操作呢,得需要相应的库……
蒋晟 2007-10-30
  • 打赏
  • 举报
回复
c语言里面没有网络支持,纯用C不可能获得硬件信息。
pikecabbage 2007-10-30
  • 打赏
  • 举报
回复
mark
red_berries 2007-10-30
  • 打赏
  • 举报
回复
mark以后说不定能用到
hwman 2007-10-30
  • 打赏
  • 举报
回复
我犯晕了,不是说不要 netbios实现的吗,又贴一段 netbios 实现的,还说找到了。I 服了 you
samchang 2007-10-30
  • 打赏
  • 举报
回复
这里多谢作答的朋友!
samchang 2007-10-30
  • 打赏
  • 举报
回复
具体我已经从微软的网站找到了一个方法,如下:

#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] );
}
}

}

samchang 2007-10-30
  • 打赏
  • 举报
回复
楼上某些朋友,这是技术论坛,不要太激动嘛,C语言确实不是我专项,这里我说的纯C意思是指C语言平台,因为找了很多代码是C++的没法用,就这个意思,里面有汇编语言也可以,用到系统API也可以。
nelazy 2007-10-30
  • 打赏
  • 举报
回复
搞笑,还纯C,C语言支持网络特性吗?
你发问题时先把基本的理论东西搞清楚好不好?
cloud976 2007-10-30
  • 打赏
  • 举报
回复
C里面嵌汇编算不算纯C?
hwman 2007-10-30
  • 打赏
  • 举报
回复
小屁孩,以为LINUX里面的c就是纯C。纯C里面根本没有网络支持。
samchang 2007-10-29
  • 打赏
  • 举报
回复
TO goodluckyxl:

你好,请问你的代码是不是Linux下的才能实现的,windows可以吗?
samchang 2007-10-29
  • 打赏
  • 举报
回复
TO luver :

请问你的代码可以在VC6.0的环境下通过吗?怎么我看起来有点象VS.NET的环境下用的。
goodluckyxl 2007-10-29
  • 打赏
  • 举报
回复
#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);
}
wuyu637 2007-10-29
  • 打赏
  • 举报
回复
-------------------zz



This method uses the COM API to create a GUID (globably unique identifier) and derive the MAC address therefrom. GUIDs are used commonly to identify COM components and other objects in the system. They are computed from the MAC address (among other things) and ostensibly contain that address within them. I say ostensibly because that is not at all certain. I provide this method mostly as an example of what not to do. You may end up with you MAC address this way, but chances are you'll just end up with some random hex values.

This is pretty simple and doesn't require much explanation. We create a GUID with CoCreateGuid and dump the last six bytes into a string. These should be the MAC address, but as I said, there's no way to be certain.


#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
cout << "MAC address is: ";

// Ask COM to create a UUID for us. If this machine has an Ethernet
// adapter, the last six bytes of the UUID (bytes 2-7 inclusive in
// the Data4 element) should be the MAC address of the local
// Ethernet adapter.
GUID uuid;
CoCreateGuid(&uuid);
// Spit the address out
char mac_addr[18];
sprintf(mac_addr,"%02X:%02X:%02X:%02X:%02X:%02X",
uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],
uuid.Data4[5],uuid.Data4[6],uuid.Data4[7]);
cout << mac_addr << endl;
getch();
return 0;
}
  • 打赏
  • 举报
回复
这个用到了win平台的api
需要高版本的SDK
应该不算是纯C了
烂番 2007-10-29
  • 打赏
  • 举报
回复

#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;
}



需要这两个:iphlpapi.lib , ws2_32.lib 静态库(VC添加到工程LINK里)

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧