简体   繁体   English

Python:使用ctypes从c ++检索枚举变量

[英]Python: retrieving an enum variable from c++ using ctypes

I have the following enum defined in C++ API: 我在C ++ API中定义了以下枚举:

typedef enum RESULT_ENUM
{
    SUCCESS,
    ERR_INVALID_PORT_DEFINITION,
    ERR_TOO_MANY_SAMPLES,
    ERR_RECORDING_THREAD_ALREADY_RUNNING,
    ERR_RECORDING_WITHOUT_APPLY_SETTINGS,
    ...
}RESULT;

I have a program in C++ that uses the API and creating: 我有一个使用API​​并创建的C ++程序:

RESULT res;

Then it uses functions from the API to set values inside res , for example: 然后,它使用API​​中的函数在res内部设置值,例如:

res = SetProfile(APP_PROFILE);
res = SetDynamicImageFilter(filterType);
res = StartCalibration();

I want to create a Python program that does the same (literally), using ctypes. 我想使用ctypes创建一个做同样的Python程序(从字面上看)。 How do I translate RESULT res; 如何翻译RESULT res; in a pythonic way? 以pythonic方式? How do I make it contain the desired results from the functions? 如何使它包含功能所需的结果?

EDIT: 编辑:

Those functions return values that match the RESULT enumerators. 这些函数返回与RESULT枚举数匹配的值。 I want to get those enumerators in Python, How can I do that? 我想用Python获取这些枚举器,该怎么办? I'm currently getting numbers corresponding to the enumerators values. 我目前正在获取与枚举器值相对应的数字。

The name to value mapping is not compiled into the binary. 名称到值的映射未编译到二进制文件中。

All ctypes code that needs the value of a enum hard codes that value in the python. 所有需要在python中赋值的枚举硬代码的ctypes代码。

If you wrap the C++ code in a python extension you can choose to expose the enum values as python symbols of your module. 如果将C ++代码包装在python扩展名中,则可以选择将枚举值公开为模块的python符号。

If you control the C++ implementation you are calling you could add a helper fucntion to return the value of the enum you need. 如果您控制的是C ++实现,则可以添加一个辅助函数以返回所需枚举的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM