C译成Pascal,谢谢(100分)

  • 主题发起人 主题发起人 小二哥
  • 开始时间 开始时间

小二哥

Unregistered / Unconfirmed
GUEST, unregistred user!
#include <windows.h>
#include <conio.h>
#include <stdio.h>

#define SystemBasicInformation 0
#define SystemPerformanceInformation 2
#define SystemTimeInformation 3

#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))

typedef struct
{
DWORD dwUnknown1;
ULONG uKeMaximumIncrement;
ULONG uPageSize;
ULONG uMmNumberOfPhysicalPages;
ULONG uMmLowestPhysicalPage;
ULONG uMmHighestPhysicalPage;
ULONG uAllocationGranularity;
PVOID pLowestUserAddress;
PVOID pMmHighestUserAddress;
ULONG uKeActiveProcessors;
BYTE bKeNumberProcessors;
BYTE bUnknown2;
WORD wUnknown3;
} SYSTEM_BASIC_INFORMATION;

typedef struct
{
LARGE_INTEGER liIdleTime;
DWORD dwSpare[76];
} SYSTEM_PERFORMANCE_INFORMATION;

typedef struct
{
LARGE_INTEGER liKeBootTime;
LARGE_INTEGER liKeSystemTime;
LARGE_INTEGER liExpTimeZoneBias;
ULONG uCurrentTimeZoneId;
DWORD dwReserved;
} SYSTEM_TIME_INFORMATION;


// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQuerySystemInformation(
// IN UINT SystemInformationClass, // information type
// OUT PVOID SystemInformation, // pointer to buffer
// IN ULONG SystemInformationLength, // buffer size in bytes
// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
// // variable that receives
// // the number of bytes
// // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);

PROCNTQSI NtQuerySystemInformation;


void main(void)
{
SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
SYSTEM_TIME_INFORMATION SysTimeInfo;
SYSTEM_BASIC_INFORMATION SysBaseInfo;
double dbIdleTime;
double dbSystemTime;
LONG status;
LARGE_INTEGER liOldIdleTime = {0,0};
LARGE_INTEGER liOldSystemTime = {0,0};

NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
GetModuleHandle("ntdll"),
"NtQuerySystemInformation"
);

if (!NtQuerySystemInformation)
return;

// get number of processors in the system
status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
return;

printf("/nCPU Usage (press any key to exit): ");
while(!_kbhit())
{
// get new system time
status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
return;

// get new CPU's idle time
status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
return;

// if it's a first call - skip it
if (liOldIdleTime.QuadPart != 0)
{
// CurrentValue = NewValue - OldValue
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime = dbIdleTime / dbSystemTime;

// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;

printf("/b/b/b/b%3d%%",(UINT)dbIdleTime);
}

// store new CPU's idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSystemTime = SysTimeInfo.liKeSystemTime;

// wait one second
Sleep(1000);
}
printf("/n");
}
 
这样的能行吗?麻烦
 
哈哈

ULARGE_INTEGER = record
LowPart: DWORD;
HighPart: DWORD;
end;
x: ULARGE_INTEGER
至于 Li2Double(x) 是啥我还真不太明白!
 
const SystemBasicInformation = 0
const SystemPerformanceInformation = 2
const SystemTimeInformation = 3
 
SYSTEM_BASIC_INFORMATION = record
dwUnknown1 : DWORD ;
uKeMaximumIncrement : ULARGE_INTEGER ;
uPageSize : ULARGE_INTEGER ;
uMmNumberOfPhysicalPages : ULARGE_INTEGER ;
uMmLowestPhysicalPage : ULARGE_INTEGER ;
uMmHighestPhysicalPage : ULARGE_INTEGER ;
uAllocationGranularity : ULARGE_INTEGER ;
pLowestUserAddress : Pointer ;
pMmHighestUserAddress : Pointer ;
uKeActiveProcessors : ULARGE_INTEGER ;
bKeNumberProcessors : BYTE ;
bUnknown2 : BYTE ;
wUnknown3 : WORD ;
end;
ULARGE_INTEGER 你可以用我定义的,也可以 uses windows;
其它的相对来说就简单了,你自己应该可以改了!
 
http://bbs.2ccc.com/topic.asp?topicid=161955
 
楼主的是获得CPU使用率的吧.我照你的全部翻译成了Delphi的了.
新建一个控制台程序.代码如下:
program cpu;

{$APPTYPE CONSOLE}

uses
SysUtils, Windows;

const
SystemBasicInformation = 0;
SystemPerformanceInformation = 2;
SystemTimeInformation = 3;

Function Li2Double(x: LARGE_INTEGER):Double;
begin
Result := x.HighPart* 4.294967296E9 + x.LowPart;
end;

type
LONG = Longint;
NTSTATUS = LONG;

_SYSTEM_BASIC_INFORMATION = record // Information Class 0
dwUnknown1 : DWORD;
uKeMaximumIncrement:ULONG;
uPageSize:ULONG;
uMmNumberOfPhysicalPages:ULONG;
uMmLowestPhysicalPage:ULONG;
uMmHighestPhysicalPage:ULONG;
uAllocationGranularity:ULONG;
pLowestUserAddress:Pointer;
pMmHighestUserAddress:Pointer;
uKeActiveProcessors:ULONG;
bKeNumberProcessors:Byte;
bUnknown2:Byte;
wUnknown3:WORD;
end;
SYSTEM_BASIC_INFORMATION = _SYSTEM_BASIC_INFORMATION;
PSYSTEM_BASIC_INFORMATION = ^SYSTEM_BASIC_INFORMATION;


_SYSTEM_PERFORMANCE_INFORMATION = record // Information Class 2
liIdleTime : LARGE_INTEGER;
dwSpare : array[0..75] of DWORD;
end;
SYSTEM_PERFORMANCE_INFORMATION = _SYSTEM_PERFORMANCE_INFORMATION;
PSYSTEM_PERFORMANCE_INFORMATION = ^SYSTEM_PERFORMANCE_INFORMATION;

_SYSTEM_TIME_INFORMATION = record
liKeBootTime : LARGE_INTEGER;
liKeSystemTime : LARGE_INTEGER;
liExpTimeZoneBias : LARGE_INTEGER;
uCurrentTimeZoneId : ULONG;
dwReserved : DWORD;
end;
SYSTEM_TIME_INFORMATION = _SYSTEM_TIME_INFORMATION;
PSYSTEM_TIME_INFORMATION = ^SYSTEM_TIME_INFORMATION;

PROCNTQSI = function(
SystemInformationClass : ULONG;
SystemInformation : Pointer;
SystemInformationLength : ULONG;
ReturnLength : PULONG
): NTSTATUS; stdcall;
var
NtQuerySystemInformation : PROCNTQSI = Nil;

SysPerfInfo : SYSTEM_PERFORMANCE_INFORMATION ;
SysTimeInfo : SYSTEM_TIME_INFORMATION ;
SysBaseInfo : SYSTEM_BASIC_INFORMATION ;
dbIdleTime : double ;
dbSystemTime : double ;
status : NTSTATUS ;
liOldIdleTime : LARGE_INTEGER = (LowPart:0;HighPart:0);
liOldSystemTime : LARGE_INTEGER = (LowPart:0;HighPart:0);
begin
NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll'), 'NtQuerySystemInformation');
if not Assigned(NtQuerySystemInformation) then
Exit;
status := NtQuerySystemInformation(SystemBasicInformation,@SysBaseInfo,sizeof(SysBaseInfo),nil);
if (status <> NO_ERROR)then
Exit;
write(#13'CPU Usage (press any key to exit): ');


while True do //我这里不知道Delphi有没有检测键盘的相当于VC的_kbhit()的函数.就写成了死循环.关闭的时候只好关闭控制台窗口了
begin
// get new system time
status := NtQuerySystemInformation(SystemTimeInformation,@SysTimeInfo,sizeof(SysTimeInfo),0);
if (status <> NO_ERROR)then
Exit;
// get new CPU's idle time
status := NtQuerySystemInformation(SystemPerformanceInformation,@SysPerfInfo,sizeof(SysPerfInfo),Nil);
if (status <> NO_ERROR)then
Exit;
// if it's a first call - skip it
if (liOldIdleTime.QuadPart <> 0) then
begin
// CurrentValue = NewValue - OldValue
dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime := dbIdleTime / dbSystemTime;

// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;

write(format(#8#8#8#8'%3d%%',[Trunc(dbIdleTime)]));
end;
liOldIdleTime := SysPerfInfo.liIdleTime;
liOldSystemTime := SysTimeInfo.liKeSystemTime;

// wait one second
Sleep(800);
end;
end.
 
呵呵自己实现了一个_kbhit()
program cpu;

{$APPTYPE CONSOLE}

uses
SysUtils, Windows;

const
SystemBasicInformation = 0;
SystemPerformanceInformation = 2;
SystemTimeInformation = 3;

Function _kbhit():Boolean;
var
I : Integer;
KS : Smallint;
begin
Result := False;
for I := 0 to 255 do
begin
KS := GetKeyState(I);
if (KS and $80)=$80 then
begin
Result := True;
Exit;
end;
end;
end;

Function Li2Double(x: LARGE_INTEGER):Double;
begin
Result := x.HighPart* 4.294967296E9 + x.LowPart;
end;

type
LONG = Longint;
NTSTATUS = LONG;

_SYSTEM_BASIC_INFORMATION = record // Information Class 0
dwUnknown1 : DWORD;
uKeMaximumIncrement:ULONG;
uPageSize:ULONG;
uMmNumberOfPhysicalPages:ULONG;
uMmLowestPhysicalPage:ULONG;
uMmHighestPhysicalPage:ULONG;
uAllocationGranularity:ULONG;
pLowestUserAddress:Pointer;
pMmHighestUserAddress:Pointer;
uKeActiveProcessors:ULONG;
bKeNumberProcessors:Byte;
bUnknown2:Byte;
wUnknown3:WORD;
end;
SYSTEM_BASIC_INFORMATION = _SYSTEM_BASIC_INFORMATION;
PSYSTEM_BASIC_INFORMATION = ^SYSTEM_BASIC_INFORMATION;


_SYSTEM_PERFORMANCE_INFORMATION = record // Information Class 2
liIdleTime : LARGE_INTEGER;
dwSpare : array[0..75] of DWORD;
end;
SYSTEM_PERFORMANCE_INFORMATION = _SYSTEM_PERFORMANCE_INFORMATION;
PSYSTEM_PERFORMANCE_INFORMATION = ^SYSTEM_PERFORMANCE_INFORMATION;

_SYSTEM_TIME_INFORMATION = record
liKeBootTime : LARGE_INTEGER;
liKeSystemTime : LARGE_INTEGER;
liExpTimeZoneBias : LARGE_INTEGER;
uCurrentTimeZoneId : ULONG;
dwReserved : DWORD;
end;
SYSTEM_TIME_INFORMATION = _SYSTEM_TIME_INFORMATION;
PSYSTEM_TIME_INFORMATION = ^SYSTEM_TIME_INFORMATION;

PROCNTQSI = function(
SystemInformationClass : ULONG;
SystemInformation : Pointer;
SystemInformationLength : ULONG;
ReturnLength : PULONG
): NTSTATUS; stdcall;
var
NtQuerySystemInformation : PROCNTQSI = Nil;

SysPerfInfo : SYSTEM_PERFORMANCE_INFORMATION ;
SysTimeInfo : SYSTEM_TIME_INFORMATION ;
SysBaseInfo : SYSTEM_BASIC_INFORMATION ;
dbIdleTime : double ;
dbSystemTime : double ;
status : NTSTATUS ;
liOldIdleTime : LARGE_INTEGER = (LowPart:0;HighPart:0);
liOldSystemTime : LARGE_INTEGER = (LowPart:0;HighPart:0);
begin
NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll'), 'NtQuerySystemInformation');
if not Assigned(NtQuerySystemInformation) then
Exit;
status := NtQuerySystemInformation(SystemBasicInformation,@SysBaseInfo,sizeof(SysBaseInfo),nil);
if (status <> NO_ERROR)then
Exit;
write(#13'CPU Usage (press any key to exit): ');


while not _kbhit() do //我这里不知道Delphi有没有检测键盘的相当于VC的_kbhit()的函数.就写成了死循环.关闭的时候只好关闭控制台窗口了
begin
// get new system time
status := NtQuerySystemInformation(SystemTimeInformation,@SysTimeInfo,sizeof(SysTimeInfo),0);
if (status <> NO_ERROR)then
Exit;
// get new CPU's idle time
status := NtQuerySystemInformation(SystemPerformanceInformation,@SysPerfInfo,sizeof(SysPerfInfo),Nil);
if (status <> NO_ERROR)then
Exit;
// if it's a first call - skip it
if (liOldIdleTime.QuadPart <> 0) then
begin
// CurrentValue = NewValue - OldValue
dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime := dbIdleTime / dbSystemTime;

// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;

write(format(#8#8#8#8'%3d%%',[Trunc(dbIdleTime)]));
end;
liOldIdleTime := SysPerfInfo.liIdleTime;
liOldSystemTime := SysTimeInfo.liKeSystemTime;

// wait one second
Sleep(800);
end;
end.
 
由于Sleep的时候不响应,所以按键要按得时间长一点点.
 
wr960204辛苦啦!感动ing......
 
谢谢大家
 
后退
顶部