|
发表于 2005-3-14 10:07:07
|
显示全部楼层
最初由 funlove 发布
[B]CString GetHostID()
{
typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfo
DWORD index=0;
//函数指针
PGAINFO pGAInfo;
CString HostID;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
HostID=_T("");
hInst=LoadLibrary("iphlpapi.dll");
if(!hInst){
cout<<"iphlpapi.dll not supported in this platform!\n";
return 1;
}
//------------------------------------》获得网卡数据
pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo");
pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
pGAInfo(pInfo,&ulSize);
//遍历每一张网卡
while(pInfo)
{
if(pInfo->AddressLength==6){
int i,j;
unsigned char tempstr[16],modulestr[16];
for(i=0;i<20;i++) {modulestr='\0'; tempstr='\0'; }
for(i=0;i<10;i++) modulestr=0x30+i;
for(i=0;i<6;i++) modulestr[i+10]=0x41+i;
for(i=0;i<(int)pInfo->AddressLength;i++)
{
j=(((unsigned int)pInfo->Address) & 0xf0)>>4 ;
tempstr[i*2]=modulestr[j];
j=(((unsigned int)pInfo->Address) & 0x0f) ;
tempstr[i*2+1]=modulestr[j];
}
HostID = tempstr;
pInfo=NULL;
}
else
//将当前指针移向下一个
pInfo=pInfo->Next;
}
pInfoTemp;//回收无用内存
return HostID;
} [/B] |
|