gpt4 book ai didi

winapi - WinAPI OpenQuery产生错误

转载 作者:行者123 更新时间:2023-12-03 11:46:59 37 4
gpt4 key购买 nike

我正在使用winapi-rs crate并尝试获取cpu的使用率,但是我什至无法做到这一点。
PdhCollectQueryData转换为十六进制时将返回“-2147481643”,然后错误代码为“0x800007D5(PDH_NO_DATA)没有要返回的数据。”这是我面临的问题。
有人可以帮助确定我做错了什么导致“x800007D5(PDH_NO_DATA)没有数据返回”吗?我可以在C++(下面提供的代码)中使它工作,但是当尝试将其转换为Rust代码时,一切都不会很好。

use wmi::{COMLibrary, Variant, WMIConnection};
use std::{collections::HashMap, ptr::null};
use winapi::{ctypes::{c_char, c_void},um::{pdh::*, winnt::{LPCSTR}}};

#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {

const CPU_TIME: LPCSTR = "\\Processor(_Total)\\% Processor Time".as_ptr() as *const i8;

unsafe
{
//cpuQuery
let mut hquery: PDH_HQUERY = std::ptr::null_mut();
let hquery_address: *mut PDH_HQUERY = &mut hquery as *mut *mut _ as *mut *mut c_void;

//cpuCounter
let mut hcounter: PDH_HCOUNTER = std::ptr::null_mut();
let hcounter_address: *mut PDH_HCOUNTER = &mut hcounter as *mut *mut _ as *mut *mut c_void;

//open query
PdhOpenQueryA(null(), 0, hquery_address);

PdhAddCounterA(hquery, CPU_TIME, 0, hcounter_address);

PdhCollectQueryData(hquery);

}
Ok(())
}
如果我打印出这些值,则在程序完成后返回的一些值的示例:
hquery: 0x2541b495e00
hquery_address: 0xce3dafef00
PdhOpenQueryA: 0
PdhCollectQueryData -2147481643
可用的C++代码(摘自另一个Stack Overflow答案,在我运行它时可以正常工作)
#include <TCHAR.h>
#include <pdh.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <chrono>
#include <thread>

#pragma comment(lib, "pdh.lib")
using namespace std;

static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
PdhOpenQuery(NULL, NULL, &cpuQuery);
PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PDH_FMT_COUNTERVALUE counterVal;

PdhCollectQueryData(cpuQuery);
this_thread::sleep_for(chrono::milliseconds(1000));
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
cout << counterVal.doubleValue << "\n";

getchar();
}
因此,当C++代码正在运行时,计数器就在那儿。对于查询收集的rust代码,我做错了什么,导致其无法正常工作并产生“x800007D5(PDH_NO_DATA)没有要返回的数据”。

最佳答案

这是使用 winapi crate将C++示例转换为Rust的字面意思。
正如IInspectable在评论中所说,您需要添加一些错误处理,在这种情况下,我添加了一些assert_eq!

use std::ptr;
// winapi = { version = "0.3.9", features = ["pdh", "winerror"] }
use winapi::{ctypes::c_char, shared::winerror::ERROR_SUCCESS, um::pdh::*};

fn main() {
unsafe {
let mut cpu_query: PDH_HQUERY = ptr::null_mut();
let mut cpu_total: PDH_HCOUNTER = ptr::null_mut();

let err = PdhOpenQueryA(ptr::null(), 0, &mut cpu_query);
assert_eq!(err, ERROR_SUCCESS as i32);

let err = PdhAddCounterA(
cpu_query,
b"\\Processor(_Total)\\% Processor Time\0".as_ptr() as *const c_char,
0,
&mut cpu_total,
);
assert_eq!(err, ERROR_SUCCESS as i32);

let mut counter_val: PDH_FMT_COUNTERVALUE = std::mem::zeroed();
let err = PdhCollectQueryData(cpu_query);
assert_eq!(err, ERROR_SUCCESS as i32);

std::thread::sleep(std::time::Duration::from_millis(1000));

let err = PdhCollectQueryData(cpu_query);
assert_eq!(err, ERROR_SUCCESS as i32);

let err = PdhGetFormattedCounterValue(
cpu_total,
PDH_FMT_DOUBLE,
ptr::null_mut(),
&mut counter_val,
);
assert_eq!(err, ERROR_SUCCESS as i32);

println!("{:?}", counter_val.u.doubleValue());
}
}

关于winapi - WinAPI OpenQuery产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65467622/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com