在线等,速求将以下VC转换成C#代码 获取物理网卡的MAC地址
VC 获取物理网卡的MAC地址获取网卡的MAC地址的方法很多,如:Netbios,SNMP,GetAdaptersInfo等。经过测试发现 Netbios 方法在网线拔出的情况下获取不到MAC,而 SNMP 方法有时会获取多个重复的网卡的MAC,试来试去还是 GetAdaptersInfo 方法比较好,网线拔出的情况下可以获取MAC,而且很准确,不会重复获取网卡。
GetAdaptersInfo 方法也不是十全十美,也存在些问题:
1)如何区分物理网卡和虚拟网卡;
2)如何区分无线网卡和有线网卡;
3)“禁用”的网卡获取不到。
关于问题1和问题2我的处理办法是:
区分物理网卡和虚拟网卡:pAdapter->Description中包含"PCI"是:物理网卡。(试了3台机器可以)
区分无线网卡和有线网卡:pAdapter->Type为71的是:无线网卡。(试了2个无线网卡也可以)
*/
#include"stdafx.h"
#include"iphlpapi.h"
#pragma comment(lib,"Iphlpapi.lib")
int main(int argc,char*argv[])
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter=NULL;
DWORD dwRetVal=0;
pAdapterInfo=(IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
ULONG ulOutBufLen=sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)!=ERROR_SUCCESS)
{
GlobalFree(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
}
if((dwRetVal=GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))==NO_ERROR)
{
pAdapter=pAdapterInfo;
while(pAdapter)
{
if(strstr(pAdapter->Description,"PCI")>0//pAdapter->Description中包含"PCI"为:物理网卡
||pAdapter->Type==71//pAdapter->Type是71为:无线网卡
)
{
printf("------------------------------------------------------------\n");
printf("AdapterName:%s\n",pAdapter->AdapterName);
printf("AdapterDesc:%s\n",pAdapter->Description);
printf("AdapterAddr:");
for(UINT i=0;i<pAdapter->AddressLength;i++)
{
printf("%02X%c",pAdapter->Address[i],
i==pAdapter->AddressLength-1?'\n':'-');
}
printf("AdapterType:%d\n",pAdapter->Type);
printf("IPAddress:%s\n",pAdapter->IpAddressList.IpAddress.String);
printf("IPMask:%s\n",pAdapter->IpAddressList.IpMask.String);
}
pAdapter=pAdapter->Next;
}
}
else
{
printf("CalltoGetAdaptersInfofailed.n");
}
return 0;
}
另一个
#include "stdafx.h"
#include "Lmwksta.h"
#include "Lmapibuf.h"
#include "Lm.h"
#pragma comment(lib, "Netapi32.lib")
unsigned int GetMACaddress(char strAddress[], unsigned int max)
{
unsigned char MACData[8];
char addr[20]; // Allocate data structure for MAC (6 bytes needed)
unsigned int nCount = 0;
WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure for Netbios
DWORD dwEntriesRead;
DWORD dwTotalEntries;
BYTE *pbBuffer;
// Get MAC address via NetBios's enumerate function
NET_API_STATUS dwStatus = NetWkstaTransportEnum(
NULL, // [in] server name
0, // [in] data structure to return
&pbBuffer, // [out] pointer to buffer
MAX_PREFERRED_LENGTH, // [in] maximum length
&dwEntriesRead, // [out] counter of elements actually enumerated
&dwTotalEntries, // [out] total number of elements that could be enumerated
NULL); // [in/out] resume handle
// ASSERT(dwStatus == NERR_Success);
pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer
for(DWORD i=1; i< dwEntriesRead && nCount <= max; i++) // first address is 00000000, skip it
{ // enumerate MACs and print
swscanf((wchar_t *)pwkti[i].wkti0_transport_address, L"%2hx%2hx%2hx%2hx%2hx%2hx",
&MACData[0], &MACData[1], &MACData[2], &MACData[3], &MACData[4], &MACData[5]);
sprintf(addr,"%02X-%02X-%02X-%02X-%02X-%02X",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
if(!strcmp(addr,_T("00-00-00-00-00-00")))
continue;
strcpy(&strAddress[nCount],addr);
nCount+=16;
}
// Release pbBuffer allocated by above function
dwStatus = NetApiBufferFree(pbBuffer);
// ASSERT(dwStatus == NERR_Success);
return nCount;
}
void main()
{
char strA[20];
GetMACaddress(strA,1);
}
补充:.NET技术 , C#