获取主机的硬件信息,CPU的ID,MAC,以及硬盘的ID

前段时间给公司的软件加密,需要获取主机的一些硬件信息,从网上搜集得来的资料,忘记来源网址了,现在整理了记录以下:

一。获取CPU的ID

CString GetCpuId()
{
	CString CPUID;
	unsigned long s1,s2;
	unsigned char vendor_id[]="------------";
	char sel;

	sel='1';
	CString VernderID;
	CString MyCpuID,CPUID1,CPUID2;
	switch(sel)
	{
	case '1':
		__asm{
			xor eax,eax      //eax=0:取Vendor信息
				cpuid    //取cpu id指令,可在Ring3级使用
				mov dword ptr vendor_id,ebx
				mov dword ptr vendor_id[+4],edx
				mov dword ptr vendor_id[+8],ecx
		}
		VernderID.Format(_T("%s-"),vendor_id);
		__asm{

			mov eax,01h   //eax=1:取CPU序列号
				xor edx,edx
				cpuid
				mov s1,edx
				mov s2,eax
		}
		CPUID1.Format(_T("%08X-%08X"),s1,s2);
		__asm{

			mov eax,03h
				xor ecx,ecx
				xor edx,edx
				cpuid
				mov s1,edx
				mov s2,ecx
		}
		CPUID2.Format(_T("-%08X-%08X"),s1,s2);
		break;
	case '2':
		{
			__asm{
				mov ecx,119h
					rdmsr
					or eax,00200000h
					wrmsr
			}
		}
		AfxMessageBox(_T("CPU id is disabled."));
		break;
	}

	MyCpuID = VernderID+CPUID1+CPUID2;
	CPUID = MyCpuID;
	return CPUID;
}
二。获取MAC
BOOL GetMacAddress(CString& strMacAddress)
{
	BYTE szSystemInfo[4096]; // 在程序执行完毕后,此处存储取得的系统特征码
	UINT uSystemInfoLen = 0;
	UINT uErrorCode=0;
	IP_ADAPTER_INFO iai;
	ULONG uSize = 0;
	DWORD dwResult = GetAdaptersInfo( &iai, &uSize );
	CString MacAddress;
	if( dwResult == ERROR_BUFFER_OVERFLOW )
	{
		IP_ADAPTER_INFO* piai=( IP_ADAPTER_INFO* )HeapAlloc( GetProcessHeap( ), 0, uSize );
		if( piai != NULL )
		{
			dwResult = GetAdaptersInfo( piai, &uSize );
			if( ERROR_SUCCESS == dwResult ) 
			{
				IP_ADAPTER_INFO* piai2 = piai;

				CopyMemory(szSystemInfo+uSystemInfoLen,piai2->Address, piai2->AddressLength );
				uSystemInfoLen += piai2->AddressLength;
				for(int i = 0; i < uSystemInfoLen; i++)
				{	
					MacAddress.Format("%02X", szSystemInfo[i]);
					strMacAddress += MacAddress;	
				}
			}
			else
			{
				uErrorCode = 0xF0000000U + dwResult;
			}
			assert( HeapFree( GetProcessHeap( ), 0, piai ) );
		}
		else
		{
			return FALSE;
		}
	}
	else
	{
		uErrorCode = 0xE0000000U + dwResult;
	}
	if( uErrorCode != 0U )
	{
		return FALSE;
	}
	return TRUE;
}

三。获取硬盘的ID
 
  

 
  
CString GetDiskSeriesNumber()
{
	CString str;
	DWORD Serial; 
	DWORD Length; 
	GetVolumeInformation(_T("C:\\"),NULL,MAX_PATH,&Serial,&Length,NULL,NULL,MAX_PATH);	
	str.Format("%X",Serial); 
	return str;
}
另外,要使以上函数能编译,必须 #include

 
  

 
 

你可能感兴趣的:(Note,VC++)