indows中通過ipconfig獲得網絡卡資訊

2021-04-13 12:44:52 字數 2807 閱讀 7379

我們在網路程式設計中有時可能會用mac位址,如果我們用vc++的話可以用uchar netbios(

pncbpncb   /* pointer to the network control block*/)獲取;但是我在實際的應用過程中發現它並不是每次都管用的.在乙個有無線網絡卡的筆記本上,我乙個裝有虛擬機器的筆記本上都現我了獲取不到mac的情況,也許我對這個函式的用法沒有深入了解吧!

我借用了java程式在通用的一種獲取mac的方法,借用ipconfig來獲得所有網絡卡的資訊。下面我們就來看一下具體方法。

首先我們定義乙個結構用於儲存網絡卡資訊:

typedef struct _tagadapterinfoadapterinfo,*padapterinfo;

我們定義乙個獲得本機所有網絡卡資訊的方法:

int getalladapterinfo(carray& adapterinfolist,

cstring& error);

下面讓我們來看一下實現:

int ctestdlg::getalladapterinfo(

carray& adapterinfolist,cstring error)

;lpstr p="ipconfig /all";

unsigned long lnreadnum;

int nindex=0,from=0,to=0,nloop=0;

startupinfo  sa;

process_information pinfo;

security_attributes sapipe;

sapipe.nlength= sizeof( security_attributes );

sapipe.lpsecuritydescriptor = null;

sapipe.binherithandle       = true; 

if (!createpipe(&hread,&hwrite,&sapipe,0))//建立管道

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

sa.cb=sizeof(sa);

sa.dwflags=startf_useshowwindow |startf_usestdhandles;

sa.wshowwindow = sw_hide;

sa.hstdoutput=hwrite;

sa.hstderror=hwrite;

//建立程序

nreturn=::createprocess(null,p,&sapipe,&sapipe,

true,0,null,null,&sa,&pinfo);

if (!nreturn)

//sleep(1000);

closehandle( pinfo.hthread );

closehandle( pinfo.hprocess );

closehandle( hwrite);

//讀即管道的輸出

do}while(nreturn&&lnreadnum);

adapterinfo info;

//輸出資訊裡查詢網絡卡資訊

do from=strcon.find(":",nindex)+2;

to=strcon.find("/n",from);

info.description=strcon.mid(from,to-from-1);

nindex=to;

nindex=strcon.find("physical address",nindex);

from=strcon.find(":",nindex)+2;

to=strcon.find("/n",from);

info.physicaladdr=strcon.mid(from,to-from-1);

nindex=to;

nloop=0;

while(nloop<64)

from=strcon.find(":",nindex)+2;

to=strcon.find("/n",from);

info.ipaddr[nloop]=strcon.mid(from,to-from-1);

nindex=to;

nindex=strcon.find("subnet mask",nindex);

from=strcon.find(":",nindex)+2;

to=strcon.find("/n",from);

info.subnetmask[nloop]=strcon.mid(from,to-from-1);

nloop++;

}info.lipnum=nloop;

nindex=to;

nindex=strcon.find("default gateway",nindex);

from=strcon.find(":",nindex)+2;

to=strcon.find("/n",from);

info.gateway=strcon.mid(from,to-from-1);

adapterinfolist.add(info);

}while(true);

return 0;

}方法只是管道的一種最基本的應用,拿來與大家分享一下!另外我們還可以用ip幫助函式來獲得ip

中軟面試通過,今天

前幾天從網上投乙份中軟的應聘書,第二天便接到了人事部的筆試通知。在準備了大約三天時間,今天下午準時到達中軟。從筆試的試卷來看,以c 的基礎中的基礎為主,以語言的細節主為。選擇題10題,自以為很簡單,不到10分鐘搞定。後來看到結果,錯了4道。至於為什麼錯了,現在也不知道。不過,我覺得都是考察應試者的細...

Android中通過typeface設定字型

android 系統預設支援三種字型,分別為 sans serif monospace 一般預設值是sans,除此之外還可以使用其他字型檔案 ttf 英文本型有差異,貌似中文字型沒有差異。一 使用預設字型 效果圖 二 使用其他字型 很多時候預設的三個字型沒法滿足我們的需求,就會使用其他字型。andr...

Unity中通過PlayerPrefs進行資料儲存

在unity中儲存資料也是通過鍵值對的方式來進行。主要通過playerprefs來完成。通過playerprefs可以用來儲存和訪問玩家的偏好設定。在mac os x中,playerprefs的資料儲存在 library preferences資料夾中,以unity.company name pro...