其实就是参数传递和API参数类型说明的问题

解决方案 »

  1.   

    我讨厌C++,如果你有需要,将这段代码的意思直接用vb写出来都没转换麻烦!!!
      

  2.   

    请教:jessezappy太需要你的帮助,先谢过了
      

  3.   

    我刚看到,说实话,要真正成为一个高手,不懂VC,不懂SDK,编程是不可能的.
    也谢谢你启发,以后,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
      

  4.   

    to:巴顿帮我看一看好吗?
    http://www.csdn.net/expert/topic/493/493873.shtm
      

  5.   

    Bardo(巴顿)老兄: 
    我怎么在MSDN里面找不到NtQuerySystemInformation函数呢?