本文主要是介绍获取各类硬件ID汇编代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
{-----------------------------------------------------------------------------
作者:sushengmiyan 2013.01.26
备注:
功能:硬件信息获取单元
-----------------------------------------------------------------------------}unit ApplicationHardWareInfo;interface
usesWindows, SysUtils, Classes, Registry, Nb30, WinSock;typeTCPUID = array[1..4] of Longint;function GetCPUID: TCPUID; assembler; register;
type//CPUID信息类TCPUIDInfo = classprivateFCPUID: TCPUID;FCPUIDStr: string;procedure SetCPU(AHandle: THandle; CpuNo: Integer);function CPUIDTostr(ACPUID: TCPUID): string;function GetComputerBasicFrequency: string;function GetCPUType: string;publicfunction GetCPUIDstr: string;property CPUFrequency: string read GetComputerBasicFrequency;property ProcessorType: string read GetCPUType;constructor Create;end;//mac地址信息类TMacAdressInfo = classprivateFMacAdress: string;function GetMacPhysicalAddress(Alana: Integer = 0): string;function GetMacAddress: string;publicproperty MacAdrress: string read GetMacAddress;constructor Create;end;//用户信息类TPCUserInfo = classprivatefunction GetUserName: string;function GetHostIP: string;function GetWindowsVertion: string;publicproperty UserName: string read GetUserName;property HostIP: string read GetHostIP;property WindowsVertion: string read GetWindowsVertion;constructor Create;end;implementation{ TCPUIDInfo }
{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:TCPUID
功能:汇编方式获取CPUID
\-----------------------------------------------------------------------------}function GetCPUID: TCPUID;
asmPUSH EBX {Save affected register}PUSH EDIMOV EDI,EAX {@Resukt}MOV EAX,1DW $A20F {CPUID Command}STOSD {CPUID[1]}MOV EAX,EBXSTOSD {CPUID[2]}MOV EAX,ECXSTOSD {CPUID[3]}MOV EAX,EDXSTOSD {CPUID[4]}POP EDI {Restore registers}POP EBX
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:ACPUID: TCPUID
返回:string
功能:将十六进制的CPUID转换为字符串
\-----------------------------------------------------------------------------}function TCPUIDInfo.CPUIDTostr(ACPUID: TCPUID): string;
beginResult := '';Result := IntToHex(ACPUID[1] , 8) + IntToHex(ACPUID[2] , 8) + IntToHex(ACPUID[3], 8) + IntToHex(ACPUID[4] , 8);
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string
功能:处理器类型
\-----------------------------------------------------------------------------}function TCPUIDInfo.GetCPUType: string;
varsysteminfo: SYSTEM_INFO;
begin//获得CPU型号GetSystemInfo(systeminfo);Result := IntToStr(systeminfo.dwProcessorType)
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string
功能:CPU频率
\-----------------------------------------------------------------------------}function TCPUIDInfo.GetComputerBasicFrequency: string;
constDelayTime = 500;
varTimerHi, TimerLo: DWORD;PriorityClass, Priority: Integer;dSpeed: Double;
beginPriorityClass := GetPriorityClass(GetCurrentProcess);Priority := GetThreadPriority(GetCurrentThread);SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);Sleep(10);asmdw 310Fh // RDTSC指令mov TimerLo, eaxmov TimerHi, edxend;Sleep(DelayTime);asmdw 310Fh // rdtscsub eax, TimerLosbb edx, TimerHimov TimerLo, eaxmov TimerHi, edxend;SetThreadPriority(GetCurrentThread, Priority);SetPriorityClass(GetCurrentProcess, PriorityClass);dSpeed := TimerLo / (1000.0 * DelayTime);Result := FormatFloat('0.00' , dSpeed / 1024) + ' GHz';
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:AOwner: TComponent
返回:None
功能:初始化
\-----------------------------------------------------------------------------}constructor TCPUIDInfo.Create;
beginFCPUID := GetCPUID;FCPUIDStr := CPUIDTostr(FCPUID);
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string CPUID字符串
功能:对外函数,调用这个方法就能得到CPUID
\-----------------------------------------------------------------------------}function TCPUIDInfo.GetCPUIDstr: string;
beginSetCPU(GetCurrentProcess , 1);Result := CPUIDTostr(GetCPUID);
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:AHandle: THandle; CpuNo: Integer
返回:None
功能:设置使用哪个cpu默认是第一个(推荐)
\-----------------------------------------------------------------------------}procedure TCPUIDInfo.SetCPU(AHandle: THandle; CpuNo: Integer);
varProcessAffinity: Cardinal;_SystemAffinity: Cardinal;
begin//通过设置进程或线程的亲缘性(affinity),使进程或线程在指定的CPU(核)上运行GetProcessAffinityMask(AHandle , ProcessAffinity, _SystemAffinity) ;ProcessAffinity := CpuNo;SetProcessAffinityMask(AHandle , ProcessAffinity);
end;{ TMacAdressInfo }
{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:None
功能:初始化
\-----------------------------------------------------------------------------}constructor TMacAdressInfo.Create;
beginFMacAdress := GetMacPhysicalAddress;
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string
功能:返回Mac地址
\-----------------------------------------------------------------------------}function TMacAdressInfo.GetMacAddress: string;
beginResult := FMacAdress;
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:Alana: Integer = 0
返回:string
功能:通过LANA号获取Mac物理地址 (内存方式获取),整体步骤如下:一、枚举系统上可用的所有LANA编号二、重置计划使用的每个LANA编号三、适配器命令获取网卡地址
\-----------------------------------------------------------------------------}function TMacAdressInfo.GetMacPhysicalAddress(Alana: Integer = 0): string;
varNCB: TNCB;//Netbios控制块AdapterStatus: TAdapterStatus;//取网卡状态LanaEnum: TLanaEnum; //LANA枚举值I: Integer;
beginResult := '';Try{ http://blog.csdn.net/sushengmiyan/article/details/8543811一、枚举LANA值①.申请分配一个TNCB结构 NCB: TNCB;②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));③.置命令为NCBENUM NCB.ncb_Command := chr(NCBENUM);④.为ncb_buffer分配LANA_ENUM NCB.ncb_buffer := @LANAENUM;⑤.为NCB_length制定长度 NCB.NCB_length := Sizeof(LANAENUM);⑥.调用Netbios函数获取Netbios CRC := NetBios(@NCB);⑦.返回值NRC_GOODRET表示成功 NCB.ncb_retcode = Chr(NRC_GOODRET)}ZeroMemory(@NCB , SizeOf(NCB));NCB.ncb_Command := Chr(NCBENUM);NCB.ncb_buffer := @LANAENUM;NCB.NCB_length := Sizeof(LANAENUM);NetBios(@NCB);if not (NCB.ncb_retcode = Chr(NRC_GOODRET)) then Exit;{ http://blog.csdn.net/sushengmiyan/article/details/8543811二、重置计划使用的每个LANA编号①.申请分配一个TNCB结构 NCB: TNCB;②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));③.置命令为NCBRESET NCB.ncb_Command := chr(NCBRESET);④.给命令设置LANA编号 NCB.ncb_lana_num := LanaEnum.lana[Alana];⑤.调用Netbios函数获取Netbios CRC := NetBios(@NCB);⑥.返回值NRC_GOODRET表示成功 NCB.ncb_retcode = Chr(NRC_GOODRET)}ZeroMemory(@NCB , SizeOf(NCB));NCb.ncb_Command := Chr(NCBRESET);NCB.ncb_lana_num := LanaEnum.lana[Alana];Netbios(@NCB);if not (NCB.ncb_retcode = Chr(NRC_GOODRET)) then Exit;{ http://blog.csdn.net/sushengmiyan/article/details/8543811三、使用TAdapterStatus结构获取网卡地址①.申请分配一个TNCB结构 NCB: TNCB;②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));③.置命令为NCBASTAT NCB.ncb_Command := chr(NCBASTAT);④.为ncb_buffer分配LANA_ENUM NCB.ncb_buffer := @LANAENUM;⑤.设置ncb_callname NCB.ncb_callname := '* ' + #0;⑥.为ncb_buffer分配AdapterStatus NCB.ncb_buffer := @AdapterStatus;⑦.为NCB_length制定长度 NCB.NCB_length := Sizeof(AdapterStatus);⑧.调用Netbios函数获取Netbios CRC := NetBios(@NCB);}ZeroMemory(@NCB , SizeOf(NCB));NCb.ncb_Command := chr(NCBASTAT);NCB.ncb_lana_num := LANAENUM.lana[Alana];NCB.ncb_callname[0] := '*';//不明白为何如此设置,*代表啥子?//有懂的可以邮件分享 429119108@qq.com O(∩_∩)O谢谢NCB.ncb_buffer := @AdapterStatus;NCB.ncb_length := SizeOf(AdapterStatus);NetBios(@NCB);//获取形如AA-BB-CC-DD-EE-FF形式的mac物理地址字符串Result := '';for I := 0 to 5 doif SameStr(Result , '') thenResult := Result + IntToHex(Integer(AdapterStatus.adapter_address[I]) , 2)elseResult := Result + '-' + IntToHex(Integer(AdapterStatus.adapter_address[I]) , 2);FinallyEnd;
end;{ TPCUserInfo }{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:None
功能:创建
\-----------------------------------------------------------------------------}constructor TPCUserInfo.Create;
beginend;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string
功能:得到主机IP
\-----------------------------------------------------------------------------}function TPCUserInfo.GetHostIP: string;
varsHostName: string;WSAData: TWSAData;HostEnt: PHostEnt;
beginsHostName := UserName;Result := '';WSAStartup(2 , WSAData);HostEnt := GetHostByName(PChar(sHostName));if HostEnt <> nil thenbeginwith HostEnt^ doResult:= Format('%d.%d.%d.%d' , [Byte(h_addr^[0]) , Byte(h_addr^[1]), Byte(h_addr^[2]) , Byte(h_addr^[3])]);end;WSACleanup;
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:None
功能:获取用户名
\-----------------------------------------------------------------------------}function TPCUserInfo.GetUserName;
varName: PChar;Size: DWORD;
beginGetMem(Name , 255);//申请内存Size := 255;GetComputerName(Name , Size);Result := Name;FreeMem(Name); //记得释放内存
end;{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:None
返回:string
功能:返回操作系统类型
\-----------------------------------------------------------------------------}function TPCUserInfo.GetWindowsVertion: string;function GetWindowsVersionString: string;varoSVersion: TOSVersionInfoA;beginResult := '';oSversion.dwOSVersionInfoSize := SizeOf(TOSVersionInfoA);if GetVersionExA(oSVersion) thenwith oSVersion doResult := Trim(Format('%s', [szCSDVersion]));end;
varAWin32Version: Extended;sWin: string;
beginsWin := 'Windows';AWin32Version := StrToFloat(Format('%d.%d', [Win32MajorVersion, Win32MinorVersion]));case Win32Platform ofVER_PLATFORM_WIN32s:Result := sWin + '32';VER_PLATFORM_WIN32_WINDOWS:beginif AWin32Version = 4.0 thenResult := sWin + '95'else if AWin32Version = 4.1 thenResult := sWin + '98'else if AWin32Version = 4.9 thenResult := sWin + 'Me'elseResult := sWin + '9x';end;VER_PLATFORM_WIN32_NT:beginif AWin32Version = 3.51 thenResult := sWin + 'NT 3.51'else if AWin32Version = 4.0 thenResult := sWin + 'NT 4.0'else if AWin32Version = 5.0 thenResult := sWin + '2000'else if AWin32Version = 5.1 thenResult := sWin + 'XP'else if AWin32Version = 5.2 thenResult := sWin + '2003'else if AWin32Version = 6.0 thenResult := sWin + 'Vista'else if AWin32Version = 6.1 thenResult := sWin + '7'elseResult := sWin;endelseResult := sWin;end;Result := Result + ' ' + GetWindowsVersionString;
end;end.
void CMyDlg::OnButton7() //VC 内联汇编获取CPU机器码。。强大
{
char h1[260];
unsigned long s1,s2,s3,s4;
__asm
{
mov eax,00h
xor edx,edx
cpuid
mov s1, edx
mov s2, eax
}
__asm
{
mov eax,01h
xor ecx,ecx
xor edx,edx
cpuid
mov s3, edx
mov s4, ecx
}
sprintf(h1,"%X%X%X%X",s1,s2,s3,s4);
a.MsgBox (h1);//这个MsgBox 函数不是VB的,我封装的,到时候会在VC板块里面放出,我写了一个VC梦类模块,把VC里面的常用操作封装为了一个开源的类,调用起来很方便的,会找到VB的感觉,VC6.0也会变得各种简单起来。。。
}
这篇关于获取各类硬件ID汇编代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!