unit CommonUnit;
interface
uses
Windows, SysUtils, DateUtils;
Const
CPUVendorIDs: array [0 .. 5] of string = ('GenuineIntel', 'UMC UMC UMC',
'AuthenticAMD', 'CyrixInstead', 'NexGenDriven', 'CentaurHauls');
// 将CPU厂家信息转换成字串形式
CPUVendors: array [0 .. 5] of string = ('Intel', 'UMC', 'AMD', 'Cyrix',
'NexGen', 'CentaurHauls');
type
TVendor = array [0 .. 11] of AnsiChar;
{ 将AnsiString的乱码转换成能正常显示的Utf8编码的字符串 }
function DecodeUtf8Str(const S: string): WideString;
function DateToInt64(date: TDateTime): Int64;
function Int64ToDate(num: Int64): TDateTime;
function GetCPUID: string;
function GetIdeSerialNumber: string;
function GetCPUVendor: TVendor;
function GetCPUV: string;
implementation
function DecodeUtf8Str(const S: string): WideString;
var
lenSrc, lenDst: Integer;
begin
lenSrc := Length(S);
if (lenSrc = 0) then
Exit;
lenDst := MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, nil, 0);
SetLength(Result, lenDst);
MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, Pointer(Result), lenDst);
end;
function DateToInt64(date: TDateTime): Int64;
var
Bias: Integer;
a1, a2: Extended;
T1, T2: TDateTime;
TS, TS2: TTimeStamp;
pTime: _TIME_ZONE_INFORMATION;
begin
GetTimeZoneInformation(pTime); // 获取时区
Bias := pTime.Bias;
T1 := IncMinute(date, Bias);
T2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
TS := DateTimeToTimeStamp(T1);
TS2 := DateTimeToTimeStamp(T2);
a1 := TimeStampToMSecs(TS);
a2 := TimeStampToMSecs(TS2);
Result := StrToInt64Def(FloatToStr(a1 - a2), 0);
end;
function Int64ToDate(num: Int64): TDateTime;
var
Bias: Integer;
a1, a2: Extended;
T1, T2: TDateTime;
TS, TS2: TTimeStamp;
pTime: _TIME_ZONE_INFORMATION;
begin
GetTimeZoneInformation(pTime); // 获取时区
Bias := pTime.Bias;
// Bias := Bias + pTime.DaylightBias;
T2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
TS2 := DateTimeToTimeStamp(T2);
a2 := TimeStampToMSecs(TS2);
a1 := StrToFloat(IntToStr(num));
TS := MSecsToTimeStamp(a1 + a2);
T1 := TimeStampToDateTime(TS);
T1 := IncMinute(T1, -Bias);
Result := T1;
end;
function GetCPUID: string;
procedure SetCPU(Handle: THandle; CPUNO: Integer);
var
ProcessAffinity: Cardinal;
_SystemAffinity: Cardinal;
begin
GetProcessAffinityMask(Handle, ProcessAffinity, _SystemAffinity);
ProcessAffinity := CPUNO;
SetProcessAffinityMask(Handle, ProcessAffinity);
end;
const
CPUINFO = '%s-%.8x%.8x';
var
iEax: Integer;
iEbx: Integer;
iEcx: Integer;
iEdx: Integer;
begin
SetCPU(GetCurrentProcess, 1);
asm
push ebx
push ecx
push edx
mov eax, 1
DW $A20F// cpuid
mov iEax, eax
mov iEbx, ebx
mov iEcx, ecx
mov iEdx, edx
pop edx
pop ecx
pop ebx
end
;
Result := Format(CPUINFO, [GetCPUV, iEdx,iEax]);
end;
function GetCPUV: string;
var
Vendor: string;
VendorID, I: Integer;
begin
Vendor := GetCPUVendor;
{for I := 0 to High(CPUVendorIDs) do
begin
If Vendor = CPUVendorIDs[I] then
begin
Vendor := CPUVendorIDs[I];
VendorID := I;
break;
end;
end; }
Result := Vendor;
end;
// 获取CPU厂家信息,返回值为TVendor类型
function GetCPUVendor: TVendor;assembler;register;
asm
PUSH EBX
PUSH EDI
MOV EDI,EAX
MOV EAX,0
DW $A20F // CPUID指令
MOV EAX,EBX
XCHG EBX,ECX
MOV ECX,4
@1:
STOSB
SHR EAX,8
LOOP @1
MOV EAX,EDX
MOV ECX,4
@2:
STOSB
SHR EAX,8
LOOP @2
MOV EAX,EBX
MOV ECX,4
@3:
STOSB
SHR EAX,8
LOOP @3
POP EDI
POP EBX
end;
function GetIdeSerialNumber: string; // 获取硬盘的出厂系列号;
var
RootPath: array [0 .. 20] of char;
VolName: array [0 .. 255] of char;
SerialNumber: DWORD;
MaxCLength: DWORD;
FileSysFlag: DWORD;
FileSysName: array [0 .. 255] of char;
begin
RootPath := 'C:\';
GetVolumeInformation(RootPath, VolName, 255, @SerialNumber, MaxCLength,
FileSysFlag, FileSysName, 255);
Result := Format('%s', [IntToHex(SerialNumber, 8)]);
end;
end.
获取硬件、CPUID等信息
最新推荐文章于 2024-06-17 18:43:39 发布