在 python 中使用 cppyy 时指针变量作为未知类型引发错误

Pointer Variable raise error as unknown type when using cppyy in python

所以我有这个 sdk and here is the documentation,它是用 c++ 编写的,所以我安装了 cppyy,但是当我在 python 中包含带有 cppyy 的 header 文件时,它会引发错误

    // 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 工作正常我在没有 headers 的情况下对其进行了测试并且没有错误

编码以检查此

的实现
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 住在 oaidl.h,这需要在 MysticLight_SDK.h 之前包含(以及使用 [ 时通常包含的任何其他 header) =13=] 来自 C++).

cppyy 以预编译 header 的形式引入标准 C++ headers,但任何其他平台和项目 headers 都需要显式包含,或者需要打包成一个单独的字典(https://cppyy.readthedocs.io/en/latest/utilities.html#dictionaries)以允许自动加载。

更准确地说,使用上面 link 的 .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 header 只包含函数指针的类型定义,没有实际的 API。