You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用macOS IntelPowerGadget框架:PG_ReadSample与PGSample_GetPackageTemperature函数的正确定义及调用问题

Python调用macOS IntelPowerGadget框架:PG_ReadSample与PGSample_GetPackageTemperature函数的正确定义及调用问题

我刚接触Python第二天,现在想调用macOS上的IntelPowerGadget框架里的函数,折腾两天了还是没搞定,想来求助一下。

目前我已经写出了下面的代码,能成功加载框架并调用PG_Initialize():

import objc
import sys

if sys.platform == 'darwin':
    bundle = objc.loadBundle(
            "IntelPowerGadget",
            bundle_path="/Library/Frameworks/IntelPowerGadget.framework",
            module_globals=globals(),
    )
    functions = [('PG_Initialize', objc._C_VOID),]
                 #('PG_ReadSample',...),
                 #('PGSample_GetPackageTemperature',...)]
    objc.loadBundleFunctions(bundle, globals(), functions)
    PG_Initialize()

但我搞不定PG_ReadSample(函数签名:bool PG_ReadSample(int iPackage, PGSampleID* sampleID))和PGSample_GetPackageTemperature(函数签名:bool PGSample_GetPackageTemperature(PGSampleID sampleID, double* temp))这两个函数——不知道该怎么正确描述它们的签名,也不知道调用的时候该传什么参数。另外我也没找到足够的相关文档,如果有靠谱的资料方向也麻烦指个路。


更新:参考了@ali-saberi的回答后,我修改了代码如下:

import objc
import ctypes
import sys

if sys.platform == 'darwin':
  # Load the Intel Power Gadget framework
  bundle = objc.loadBundle(
    "IntelPowerGadget",
    bundle_path="/Library/Frameworks/IntelPowerGadget.framework",
    module_globals=globals(),
  )

# Define PGSampleID as ctypes type (assuming it's an integer or a pointer type)
PGSampleID = ctypes.c_void_p  # Assuming PGSampleID is a pointer. Adjust if it's another type.

# Define the functions
functions = [
    ('PG_Initialize', objc._C_BOOL),
    ('PG_ReadSample',  objc._C_BOOL + objc._C_INT + objc._C_PTR + objc._C_ULNG_LNG),
    ('PGSample_GetPackageTemperature', objc._C_BOOL + objc._C_ULNG_LNG + objc._C_PTR + objc._C_DBL),
]

# Load the functions
objc.loadBundleFunctions(bundle, globals(), functions)

# Initialize the library
if not PG_Initialize():
    print("Failed to initialize Intel Power Gadget")
    sys.exit(1)

# Use PG_ReadSample
sample_id = PGSampleID()  # Placeholder for PGSampleID pointer
iPackage = 0  # Assume we're reading from package 0

if PG_ReadSample(iPackage, ctypes.byref(sample_id)):
    print("Sample read successfully")

    # Use PGSample_GetPackageTemperature
    temperature = ctypes.c_double()
    if PGSample_GetPackageTemperature(sample_id, ctypes.byref(temperature)):
        print(f"Package Temperature: {temperature.value}°C")
    else:
        print("Failed to get package temperature")
else:
    print("Failed to read sample")

但现在运行到if PG_ReadSample(iPackage, ctypes.byref(sample_id)):这一行时,出现了错误:

ValueError: depythonifying 'pointer', got 'CArgObject'

还是没解决问题,求各位帮忙看看哪里错了。


备注:内容来源于stack exchange,提问作者tremp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:23:06