// getmac.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <httpext.h>
#include <windef.h>
#include <Nb30.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"netapi32.lib")
//unsigned int veax;
//unsigned int vebx;
//unsigned int vedx;
//unsigned int vecx;
unsigned char vendor_id[]="------------";
unsigned long s1,s2;
/*
//执行CPUID指令
void cpuid(unsigned int veax1)
{
_asm{
mov eax,veax1
cpuid
mov veax,eax
mov vebx,ebx
mov vecx,ecx
mov vedx,edx
}
}
//做移位操作,把寄存器中的ASCII码,以字符形式输出
void LeftMove(unsigned int var)
{
printf("%c",var);
for(int i=0;i<3;i++)
{
var=var>>8;
printf("%c",var);
}
}
//做移位操作,把寄存器中的值以“%c”形式输出
void LM(unsigned int var)
{
printf("%x",var);
for(int i=0;i<3;i++)
{
var=var>>16;
printf("%x",var);
}
}
*/
//得到CPU的生产厂商(当EAX值为0时),依次存放在EBX,EDX,ECX中
void getCpuName()
{
_asm
{
xor eax,eax
cpuid
mov dword ptr vendor_id,ebx
mov dword ptr vendor_id[+4],edx
mov dword ptr vendor_id[+8],ecx
}
printf("CPU生产商: %s\n",vendor_id);
}
//获得CPU序列号,获得序列号需要两个步骤,首先用eax = 1做参数,返回的eax中存储序列号的高两个WORD。
//用eax = 3做参数,返回ecx和edx按从低位到高位的顺序存储前4个WORD。
void getCpuSeris()
{
_asm
{
mov eax,01h
xor edx,edx
cpuid
mov s1,edx
mov s2,eax
}
printf("CPUID: %08X%08X",s1,s2);
_asm
{
mov eax,03h
xor ecx,ecx
xor edx,edx
cpuid
mov s1,edx
mov s2,ecx
}
if(s1!=0)printf("%08X",s1);
if(s2!=0)printf("%08X",s2);
printf("\n");
}
/*
//获取IP地址
void CheckIP(void)
{
WSADATA wsaData;
char name[155];
char *ip;
PHOSTENT hostinfo;
if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )//确认开启socket没有问题
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{ //从hostinfo中读取IP地址的字段
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
printf("IP地址: %s\n",ip);
}
}
WSACleanup( );//清理Socket
}
}
*/
//获取MAC地址
int getMAC(char * mac)
{
NCB ncb;
int lana;
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, *PASTAT;
ASTAT Adapter;
typedef struct _LANA_ENUM
{
UCHAR length;
UCHAR lana[MAX_LANA];
}LANA_ENUM;
LANA_ENUM lana_enum;
UCHAR uRetCode;
memset(&ncb, 0, sizeof(ncb));
memset(&lana_enum, 0, sizeof(lana_enum));
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *)&lana_enum;
ncb.ncb_length = sizeof(LANA_ENUM);
uRetCode = Netbios(&ncb);
if(uRetCode != NRC_GOODRET)return uRetCode;
for(lana=0;lana<(lana_enum.length);lana++)
{
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_enum.lana[lana];
uRetCode = Netbios(&ncb);
if(uRetCode == NRC_GOODRET)
break;
}
if(uRetCode != NRC_GOODRET)return uRetCode;
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_enum.lana[0];
strcpy((char*)ncb.ncb_callname, "*");
ncb.ncb_buffer = (unsigned char *)&Adapter;
ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios(&ncb);
if(uRetCode != NRC_GOODRET)
return uRetCode;
sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",
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]
);
return 0;
}
void main()
{
char macid[200];
getCpuName();
getCpuSeris();
// CheckIP();
getMAC(macid);
printf("MAC地址: %s\n",macid);
// printf("Press ENTER to Exit...");
getchar();
}