VC 中怎样编程才能获取到系统的MAC地址,IP,端口呢?

nizqsky 2008-03-25 03:51:57
VC++ 中怎样编程才能获取到系统的MAC地址,IP,端口呢? 请各位前高手帮忙!
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT_lau 2008-03-25
  • 打赏
  • 举报
回复
佩服,
wonder2007 2008-03-25
  • 打赏
  • 举报
回复
貌似c没有函数可以直接获得 ,c#有函数可以直接获得
我做过用winpcap获得ip mac,不过这样太麻烦了点
我还试过用system("ipconfig")重定向以后用文件实现,不过更麻烦。。
Supper_Jerry 2008-03-25
  • 打赏
  • 举报
回复
// need include file: #include <Nb30.h>

// need libary: Netapi32.lib

int GetAllLocalAdapterMacAddr(std::list<std::vector<unsigned char> >& mac)

{

NCB ncb;

LANA_ENUM AdapterList;



memset(&ncb, 0, sizeof(ncb));

ncb.ncb_command = NCBENUM;

ncb.ncb_buffer = (unsigned char *)&AdapterList;

ncb.ncb_length = sizeof(AdapterList);

Netbios(&ncb);



mac.resize(0);



for (int i = 0; i < AdapterList.length ; ++i )

{

struct ASTAT

{

ADAPTER_STATUS adapt;

NAME_BUFFER psz_name[30];

} Adapter;



// 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 = AdapterList.lana[i];



if (Netbios(&Ncb) != NRC_GOODRET)

continue;



// Prepare to get the adapter status block

memset(&Ncb, 0, sizeof(Ncb)) ;

Ncb.ncb_command = NCBASTAT;

Ncb.ncb_lana_num = AdapterList.lana[ i ];

strcpy((char *)Ncb.ncb_callname, "*" );



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 )

{

std::vector<unsigned char> v6;

v6.resize(6);

for (int i=0; i<6; i++)

v6[i] = Adapter.adapt.adapter_address[i];

if (v6[0] == 0)

{

std::list<std::vector<unsigned char> >::iterator i = mac.begin();

for (; i!=mac.end(); i++) if (*i == v6)

break;

if (i==mac.end())

mac.push_back(v6);

}

}

else

break;

}

return 0;

}


64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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