///////////////////////////////////////////////////////////////////////
linux 获得:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sa, &ifr.ifr_addr, sizeof(sin));
memcpy(mac, sa.sa_data, sizeof(mac));
fprintf(stdout, "%s mac: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
ETH_NAME, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 0;
}
/////////////////////////////////////////////////////////////////////
string v_ret;
try
{
// TODO Auto-generated method stub
FILE *stream;
char buf[1024];
memset( buf, 0, sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
stream = popen( "ifconfig|grep HWaddr", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
pclose( stream );
v_ret=buf;
cout << "len=" << v_ret.length() << endl;
v_ret=v_ret.substr(0,v_ret.length());
v_ret+="aaaa";
}
catch ( ... )
{
// TODO Auto-generated catch block
}
return v_ret;
阅读(1466) | 评论(0) | 转发(0) |