HOWTO:GettheMACAddressforanEthernetAdapter ArticleID:Q118623 Theinformationinthisarticleappliesto: MicrosoftWin32SoftwareDevelopmentKit(SDK)onthefollowingplatfo rms: -MicrosoftWindowsNTversions3.1,3.5,3.51 -MicrosoftWindows95 SUMMARY ThisarticledemonstrateshowtogettheMediaAccessControl(MAC)ad dressforanethernetadapterprogrammaticallybyusingNetBIOS,ifyo urcardisboundtoNetBIOS. MOREINFORMATION TogettheMediaAccessControl(MAC)addressforanethernetadapter programmatically,usetheNetbios()NCBASTATcommandandprovidea"*" asthenameintheNCB.ncb_CallNamefield.Thisisdemonstratedinth esamplecodebelow. Formachineswithmultiplenetworkadaptersyouneedtoenumeratethe LANAnumbersandperformtheNCBASTATcommandoneach.Evenwhenyouh aveasinglenetworkadapter,itisagoodideatoenumeratevalidLAN AnumbersfirstandperformtheNCBASTATononeofthevalidLANAnumb ers.ItisconsideredbadprogrammingtohardcodetheLANAnumberto0 . Inaddition,otherhardwareandsoftwaremaybeassignedtheirownMAC addresses.Forexample,amodemcanhaveaMACaddress.Also,aRASc lientorservercaninstall"dummy"networkadaptersthatcorrespondt oadialuporserialconnection.Normally,theseMACaddressesareran domlygenerated.IfanadapterstatusiscalledonaLANAthatcorresp ondstooneoftheseadapterswhennoconnectionispresent,Netbiosr eturnserror0x34(NRC_ENVNOTDEF)evenifaresetwaspreviouslyperfo rmed. WiththeNetBEUIandIPXtransports,thesameinformationcanbeobtai nedatacommandpromptbyusing: netconfigworkstation TheIDgivenistheMACaddress. ThefollowingcodeenumeratesallLANAnumbers,performsareset(NCBR EST),andanadapterstatus(NCBASTAT). SampleCode : //header files lost #include #include #include #include #include //header files lost typedefstruct_ASTAT_ { ADAPTER_STATUSadapt; NAME_BUFFERNameBuff[30]; }ASTAT,*PASTAT; ASTATAdapter; voidmain(void) { NCBNcb; UCHARuRetCode; charNetName[50]; LANA_ENUMlenum; inti; memset(&Ncb,0,sizeof(Ncb)); Ncb.ncb_command=NCBENUM; Ncb.ncb_buffer=(UCHAR*)&lenum; Ncb.ncb_length=sizeof(lenum); uRetCode=Netbios(&Ncb); printf("TheNCBENUMreturncodeis:0x%x\n",uRetCode); for(i=0;ibr>{ memset(&Ncb,0,sizeof(Ncb)); Ncb.ncb_command=NCBRESET; Ncb.ncb_lana_num=lenum.lana[i]; uRetCode=Netbios(&Ncb); printf("TheNCBRESETonLANA%dreturncodeis: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("TheNCBASTATonLANA%dreturncodeis:0x%x\n", lenum.lana[i],uRetCode); if(uRetCode==0) { printf("TheEthernetNumberonLANA%dis: %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]); } } } Keywords:kbnetworkkbAPIkbNetBIOSkbNTOS310kbNTOS350kbNTOS351kbS DKPlatformkbWinOS95kbGrpNet LastReviewed:September7,1998 ?1999MicrosoftCorporation.Allrightsreserved.TermsofUse.
|