python - 在 python 中使用 cppyy 时,指针变量引发错误为未知类型
问题描述
所以我有这个sdk,这里是c++中的文档,所以我安装了cppyy,但是当我在python中包含带有cppyy的头文件时,它会引发错误
// MysticLight_SDK.h : header file
//
#pragma once
typedef int (*LPMLAPI_Initialize)();
typedef int (*LPMLAPI_GetDeviceInfo)(SAFEARRAY** pDevType, SAFEARRAY** pLedCount);
typedef int (*LPMLAPI_GetDeviceName)(BSTR type, SAFEARRAY** pDevName);
typedef int (*LPMLAPI_GetDeviceNameEx)(BSTR type, DWORD index, BSTR* pDevName);
typedef int (*LPMLAPI_GetErrorMessage)(int ErrorCode, BSTR* pDesc);
typedef int (*LPMLAPI_GetLedName)(BSTR type, SAFEARRAY** pLedName);
typedef int (*LPMLAPI_GetLedInfo)(BSTR type, DWORD index, BSTR* pName, SAFEARRAY** pLedStyles);
typedef int (*LPMLAPI_GetLedColor)(BSTR type, DWORD index, DWORD* R, DWORD* G, DWORD* B);
typedef int (*LPMLAPI_GetLedStyle)(BSTR type, DWORD index, BSTR* style);
typedef int (*LPMLAPI_GetLedMaxBright)(BSTR type, DWORD index, DWORD* maxLevel);
typedef int (*LPMLAPI_GetLedBright)(BSTR type, DWORD index, DWORD* currentLevel);
typedef int (*LPMLAPI_GetLedMaxSpeed)(BSTR type, DWORD index, DWORD* maxLevel);
typedef int (*LPMLAPI_GetLedSpeed)(BSTR type, DWORD index, DWORD* currentLevel);
typedef int (*LPMLAPI_SetLedColor)(BSTR type, DWORD index, DWORD R, DWORD G, DWORD B);
typedef int (*LPMLAPI_SetLedColors)(BSTR type, DWORD AreaIndex, SAFEARRAY** pLedName, DWORD* R, DWORD* G, DWORD* B);
typedef int (*LPMLAPI_SetLedColorEx)(BSTR type, DWORD AreaIndex, BSTR pLedName, DWORD R, DWORD G, DWORD B, DWORD );
typedef int (*LPMLAPI_SetLedColorSync)(BSTR type, DWORD AreaIndex, BSTR pLedName, DWORD R, DWORD G, DWORD B, DWORD );
typedef int (*LPMLAPI_SetLedStyle)(BSTR type, DWORD index, BSTR style);
typedef int (*LPMLAPI_SetLedBright)(BSTR type, DWORD index, DWORD level);
typedef int (*LPMLAPI_SetLedSpeed)(BSTR type, DWORD index, DWORD level);
错误
Traceback (most recent call last):
File ".\check.py", line 5, in <module>
cppyy.include('MysticLight_SDK.h')
File "C:\Users\sarfaraz\AppData\Local\Programs\Python\Python37\lib\site-packages\cppyy\__init__.py", line 217, in include
raise ImportError('Failed to load header file "%s"%s' % (header, err.err))
ImportError: Failed to load header file "MysticLight_SDK.h"
In file included from input_line_18:1:
./MysticLight_SDK.h:7:38: error: unknown type name 'SAFEARRAY'
typedef int (*LPMLAPI_GetDeviceInfo)(SAFEARRAY** pDevType, SAFEARRAY** pLedCount);
^
./MysticLight_SDK.h:7:60: error: unknown type name 'SAFEARRAY'
typedef int (*LPMLAPI_GetDeviceInfo)(SAFEARRAY** pDevType, SAFEARRAY** pLedCount);
^
./MysticLight_SDK.h:8:38: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetDeviceName)(BSTR type, SAFEARRAY** pDevName);
^
./MysticLight_SDK.h:8:49: error: unknown type name 'SAFEARRAY'
typedef int (*LPMLAPI_GetDeviceName)(BSTR type, SAFEARRAY** pDevName);
^
./MysticLight_SDK.h:9:40: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetDeviceNameEx)(BSTR type, DWORD index, BSTR* pDevName);
^
./MysticLight_SDK.h:9:64: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetDeviceNameEx)(BSTR type, DWORD index, BSTR* pDevName);
^
./MysticLight_SDK.h:10:55: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetErrorMessage)(int ErrorCode, BSTR* pDesc);
^
./MysticLight_SDK.h:11:35: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedName)(BSTR type, SAFEARRAY** pLedName);
^
./MysticLight_SDK.h:11:46: error: unknown type name 'SAFEARRAY'
typedef int (*LPMLAPI_GetLedName)(BSTR type, SAFEARRAY** pLedName);
^
./MysticLight_SDK.h:12:35: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedInfo)(BSTR type, DWORD index, BSTR* pName, SAFEARRAY** pLedStyles);
^
./MysticLight_SDK.h:12:59: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedInfo)(BSTR type, DWORD index, BSTR* pName, SAFEARRAY** pLedStyles);
^
./MysticLight_SDK.h:12:72: error: unknown type name 'SAFEARRAY'
typedef int (*LPMLAPI_GetLedInfo)(BSTR type, DWORD index, BSTR* pName, SAFEARRAY** pLedStyles);
^
./MysticLight_SDK.h:13:36: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedColor)(BSTR type, DWORD index, DWORD* R, DWORD* G, DWORD* B);
^
./MysticLight_SDK.h:14:36: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedStyle)(BSTR type, DWORD index, BSTR* style);
^
./MysticLight_SDK.h:14:60: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedStyle)(BSTR type, DWORD index, BSTR* style);
^
./MysticLight_SDK.h:15:40: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedMaxBright)(BSTR type, DWORD index, DWORD* maxLevel);
^
./MysticLight_SDK.h:16:37: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedBright)(BSTR type, DWORD index, DWORD* currentLevel);
^
./MysticLight_SDK.h:17:39: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedMaxSpeed)(BSTR type, DWORD index, DWORD* maxLevel);
^
./MysticLight_SDK.h:18:36: error: unknown type name 'BSTR'
typedef int (*LPMLAPI_GetLedSpeed)(BSTR type, DWORD index, DWORD* currentLevel);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
cppyy 工作正常我在没有标题的情况下对其进行了测试,并且没有错误
编写代码来检查这个的实现
import cppyy
cppyy.include('MysticLight_SDK.h')
cppyy.cppdef("""
int main() {
int c;
int a = 10;
int b = 10;
c = a+b;
return c;
};""")
c = cppyy.gbl.main
print(c())
提前致谢
解决方案
看起来像SAFEARRAY
live in oaidl.h
,这需要在之前包含(以及从 C++MysticLight_SDK.h
使用时通常包含的任何其他标头)。MysticLight_SDK.h
cppyy 以预编译头文件的形式引入标准 C++ 头文件,但任何其他平台和项目头文件都需要显式包含,或者需要打包到单独的字典中(https://cppyy.readthedocs.io/en/ latest/utilities.html#dictionaries ) 以允许自动加载。
更准确地说,使用上面链接的 .zip 文件,如下:
import cppyy
cppyy.include("oaidl.h") # <-- this is the one
cppyy.include("MysticLight_SDK.h")
cppyy.load_library("MysticLight_SDK_x64")
为我工作。
除此之外,我不知道用例是什么,因为该MysticLight_SDK.h
标头仅包含函数指针的 typedef,没有实际的 API。
推荐阅读
- hyperledger-fabric - 如何为使用 API 创建签名
- scala - 使用 ScalikeJDBC 进行 SQL 插值
- reporting-services - 更改渲染显示以水平显示图表(x 列数)
- python - 如何知道文件是否被压缩
- jquery - 带有 2 个手柄的 jQuery UI 滑块 - 防止交叉
- azure - Azure IoT 远程监控问题
- javascript - 来自 url 变量的颜色 html 标记
- go - 如何告诉 Golang Gob 编码可以序列化包含没有导出字段的结构的结构
- reactjs - 当前日期,年份,月份通过函数做出反应
- sql-server - 创建列作为 XML 内容