如何才能正確擷取網路卡Mac AddRess |
尚未結案
|
ray_pong.tw
一般會員 發表:7 回覆:7 積分:2 註冊:2005-05-09 發送簡訊給我 |
大家好: 最近在作一擷取網路卡位址的小程式,但是遇到問題,在ComboBox1Change事件中我是指定在registry中網卡的機碼位置來讀取網卡的資料,但是若是換成其他的電腦或是不同型號的網卡,那我所指定的位置就會失效,請問各位前輩,該如何解決這個問題? 謝謝!! 小弟的程式碼如下:
TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { registry = new TRegistry; TStrings *Strings; try { registry->GetKeyNames(ComboBox1->Items); registry->RootKey = HKEY_LOCAL_MACHINE; if (registry->OpenKey("\\Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\",false)) { registry->CloseKey(); } else { ShowMessage("No Network Adapters"); Application->Terminate(); return; } } catch(...) { ShowMessage("Registry problem"); Application->Terminate(); return; } // 802.3 Objects (Ethernet) oid.insert(value_type(0x01010101,AnsiString("OID_802_3_PERMANENT_ADDRESS"))); } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { AnsiString d=oid[0x00010101]; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox1Change(TObject *Sender) { registry->OpenKey("\\Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\1\\",false); AnsiString DeviceName=registry->ReadString("ServiceName"); registry->CloseKey(); HANDLE hDevice; BOOL fResult; DWORD cb; hDevice = CreateFile(("\\\\.\\" DeviceName).c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDevice == NULL) { ShowMessage("Could not open network device"); Application->Terminate(); return; } NDIS_OID OidCode =OID_GEN_SUPPORTED_LIST; int OidData[400]; DWORD byteReturned; fResult=DeviceIoControl(hDevice,IOCTL_NDIS_QUERY_GLOBAL_STATS, &OidCode,sizeof(OidCode),OidData, sizeof(int)*400,&byteReturned,NULL); int ArrSize=byteReturned/sizeof(int); bool datatrue; //for (int i=0; i < ArrSize; i) // { int i=34; char retData[4000]; fResult=DeviceIoControl(hDevice,IOCTL_NDIS_QUERY_GLOBAL_STATS, &OidData[i],sizeof(int),retData, sizeof(char)*4000,&byteReturned,NULL); datatrue=true; if (!fResult) { DWORD err=GetLastError(); //StringGrid1->Cells[2][StringGrid1->RowCount-1]="Information unavailable"; datatrue=false; } switch(OidData[i]) { case OID_802_3_PERMANENT_ADDRESS: { //StringGrid1->Cells[3][StringGrid1->RowCount-1]="Permanent station address"; if(datatrue){ char adr[40]; sprintf(adr,"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",(unsigned char)retData[0],(unsigned char)retData[1],(unsigned char) (unsigned char)retData[2],(unsigned char)retData[3],(unsigned char)retData[4],(unsigned char)retData[5]); //StringGrid1->Cells[2][StringGrid1->RowCount-1]=adr; Edit1->Text=adr; } } break; default: { /* StringGrid1->Cells[1][StringGrid1->RowCount-1]="0x" IntToHex(OidData[i],8); StringGrid1->Cells[2][StringGrid1->RowCount-1]="OID unsupported "; StringGrid1->Cells[3][StringGrid1->RowCount-1]="OID unsupported "; */ Edit1->Text="無法取得網卡資訊"; } } //StringGrid1->RowCount ; // } CloseHandle(hDevice); } //--------------------------------------------------------------------------- |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
您好:
您可以利用[搜尋]功能
Ex.http://delphi.ktop.com.tw/topic.php?TOPIC_ID=39424
【BCB】【發表】取得網路卡的相關設定 by RaynorPao 版主 順心
|
暗黑破壞神
版主 發表:9 回覆:2301 積分:1627 註冊:2004-10-04 發送簡訊給我 |
|
ray_pong.tw
一般會員 發表:7 回覆:7 積分:2 註冊:2005-05-09 發送簡訊給我 |
|
ray_pong.tw
一般會員 發表:7 回覆:7 積分:2 註冊:2005-05-09 發送簡訊給我 |
大家好: 謝謝暗黑破壞神的文章,小弟終於了解如何使用GetAdaptersInfo函數了,
以下是小弟完成的程式,與大家分享:
typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO, PULONG); // 宣告一個函數指標 TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { HINSTANCE hInst; // 準備載入 DLL 用 BYTE *pbBuf1; ULONG ulLen1; DWORD index; AnsiString adr; PGAINFO pGAInfo; // 要承接傳回的函數指標用 hInst = LoadLibrary("c:\\WINNT\\system32\\iphlpapi.dll"); if (!hInst) ShowMessage("iphlpapi.dll not supported in this platform!"); else { pbBuf1 = new BYTE[ulLen1]; pGAInfo = (PGAINFO)GetProcAddress(hInst, "GetAdaptersInfo"); // 取得函數指標 PIP_ADAPTER_INFO pInfo = NULL; PIP_ADAPTER_INFO pInfoTemp; ULONG ulsize = 0; pGAInfo(pInfo, &ulsize); pInfoTemp = pInfo = (PIP_ADAPTER_INFO)pbBuf1; pGAInfo(pInfo, &ulsize); // 呼叫 GetAdaptersInfo 函數 do { index = pInfo->Index; //char buf[256]; adr.sprintf("X:X:X:X:X:X", (unsigned int)pInfo->Address[0], (unsigned int)pInfo->Address[1], (unsigned int)pInfo->Address[2], (unsigned int)pInfo->Address[3], (unsigned int)pInfo->Address[4], (unsigned int)pInfo->Address[5]); Edit1->Text=adr; pInfo = pInfo->Next; } while (pInfo!=NULL); } FreeLibrary(hInst); } //--------------------------------------------------------------------------- |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |