其实就是参数传递和API参数类型说明的问题
解决方案 »
- 关于VB,控件的问题?
- 如何用递归方法在treeview中查找
- 【问】如何把Image控件里的图片加载到ListView背景?(只有LV的控件句柄)
- 请教LCD顾客显示屏问题
- Datagrid怎样加复选框or按钮?
- 一个很菜的问题!我VB6调用OFFICE 97下的数据库ACCESS!时编程能通过,但在OFFICE2000下不能通过!!
- 就是常见的C/S B/S软件开发中的用户权限问题
- VB,如何自动填写论坛内容,,,
- 谁知道哪里有modem对modem的传输程序,很急,类似于windows的超级中端
- 怎样得到网卡地址(多个网卡)急急急!!!
- 如何做RTF文件词法分析器???
- 这个问题难道真的这么难,!!!!!!!!!!如果分不够可以再加。
也谢谢你启发,以后,VB难以解决的或许还可以到SDK.的例子中查找一下.
代码我翻译了,实际上工作量并不是很大,关键是要明白其中的对应关系.
调试的时间长了一点.我尽可能逐行翻译,以使你易于看懂.
我建议你最好能够再贴出一个贴子,写明这是针对CPU使用状态的,
因为CSDN不支持全文搜索,这样,以后有人要用可以找到!
以下是代码:Option Explicit'********************************************************************************
'Get CPU usage for Windows NT and Windows 2000
'********************************************************************************Private Declare Function GetProcAddress Lib "kernel32" _
(ByVal hModule As Long, _
ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" _
(ByVal lpModuleName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Const NO_ERROR = 0 ' dderror
Public kbhit As BooleanConst SystemBasicInformation = 0
Const SystemPerformanceInformation = 2
Const SystemTimeInformation = 3Type SYSTEM_BASIC_INFORMATION
dwUnknown1 As Long
uKeMaximumIncrement As Long
uPageSize As Long
uMmNumberOfPhysicalPages As Long
uMmLowestPhysicalPage As Long
uMmHighestPhysicalPage As Long
uAllocationGranularity As Long
pLowestUserAddress As Long
pMmHighestUserAddress As Long
uKeActiveProcessors As Long
bKeNumberProcessors As Byte
bUnknown2 As Byte
wUnknown3 As Integer
End TypePrivate Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End TypeType SYSTEM_PERFORMANCE_INFORMATION
liIdleTime As LARGE_INTEGER
dwSpare(76) As Long
End TypeType SYSTEM_TIME_INFORMATION
liKeBootTime As LARGE_INTEGER
liKeSystemTime As LARGE_INTEGER
liExpTimeZoneBias As LARGE_INTEGER
uCurrentTimeZoneId As Long
dwReserved As Long
End Type'// ntdll!NtQuerySystemInformation (NT specific!)
'// The function copies the system information of the
'// specified type into a bufferPrivate Declare Function NtQuerySystemInformation Lib "Ntdll.dll" ( _
ByVal SystemInformationClass As Long, _
SystemInformation As Long, _
ByVal SystemInformationLength As Long, _
Optional ReturnLength As Any) As Long'此处是对预定义宏的翻译
Private Function Li2Double(x As LARGE_INTEGER) As Double
'This function converts the LARGE_INTEGER data type to a double
'因为C中定义的是无符号,而Basic中是有符号的,所以要烦琐一些
Dim dblLo As Double, dblHi As Double
If x.LowPart < 0 Then
dblLo = 2 ^ 32 + x.LowPart
Else
dblLo = x.LowPart
End If
If x.HighPart < 0 Then
dblHi = 2 ^ 32 + x.HighPart
Else
dblHi = x.HighPart
End If
Li2Double = dblLo + dblHi * 2 ^ 32
End Function'这儿是原函数,是SDK的MAIN函数,为了简化,这里不作为启动函数,
'而可以由窗体接收键盘输入:_kbhitPublic Sub GetCpuUsage() Dim SysPerfInfo As SYSTEM_PERFORMANCE_INFORMATION
Dim SysTimeInfo As SYSTEM_TIME_INFORMATION
Dim SysBaseInfo As SYSTEM_BASIC_INFORMATION
Dim dbIdleTime As Double
Dim dbSystemTime As Double
Dim status As Long
Dim liOldIdleTime As LARGE_INTEGER
liOldIdleTime.HighPart = 0&
liOldIdleTime.LowPart = 0&
Dim liOldSystemTime As LARGE_INTEGER
liOldSystemTime.HighPart = 0&
liOldSystemTime.LowPart = 0&
Dim PROCNTQSI As Long
PROCNTQSI = GetProcAddress(GetModuleHandle("ntdll"), _
"NtQuerySystemInformation") If PROCNTQSI = 0 Then
Exit Sub
End If
'get number of processors in the system
status = NtQuerySystemInformation(SystemBasicInformation, _
ByVal VarPtr(SysBaseInfo), LenB(SysBaseInfo), Null)
If (status <> NO_ERROR) Then
Exit Sub
End If
'Because only just a demo, I replace then function printf with debug.print
Debug.Print "CPU Usage (press any key to exit): "
While (Not kbhit)
' get new system time (The & in c is same as ByVal VarPtr in VB)
status = NtQuerySystemInformation(SystemTimeInformation, _
ByVal VarPtr(SysTimeInfo), LenB(SysTimeInfo), 0)
If (status <> NO_ERROR) Then
Exit Sub
End If
' get new CPU's idle time
status = NtQuerySystemInformation(SystemPerformanceInformation, _
ByVal VarPtr(SysPerfInfo), LenB(SysPerfInfo), 0)
If (status <> NO_ERROR) Then
Exit Sub
End If ' if it's a first call - skip it
If (liOldIdleTime.HighPart <> 0 Or liOldIdleTime.LowPart <> 0) Then
' 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# - dbIdleTime * 100# / CDbl(SysBaseInfo.bKeNumberProcessors) + 0.5
'Because only just a demo, I replace then function printf with debug.print
'You could store the return value to a variable
Debug.Print CLng(dbIdleTime) & "%"
End If ' store new CPU's idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime
liOldSystemTime = SysTimeInfo.liKeSystemTime ' wait one second
Sleep (1000)
' For keyboard event
DoEvents
WendEnd Sub
运行时要添加一个Form1,以下是代码:Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
kbhit = True
Unload Me
End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
kbhit = True
Unload Me
End SubPrivate Sub Form_Load()
Show
GetCpuUsage
End SubPrivate Sub Form_Unload(Cancel As Integer)
kbhit = True
End Sub
http://www.csdn.net/expert/topic/493/493873.shtm
我怎么在MSDN里面找不到NtQuerySystemInformation函数呢?