I'm trying to pass an pointer to an enum. I found a tutorial for ctypes and enum here [ctypes structures ][1] but my application is a little different. the pseudocode for my DLL looks like this
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#ifdef SOME_API_EXPORTS
#define SOME_API __declspec(dllexport)
#else
#define SOME_API __declspec(dllimport)
#endif
typedef enum
{
off,
on
} MyEnum;
SOME_API int32_t get_param(MyEnum* param1)
{
int status = error;
if (param1 == NULL)
{
return status;
}
//do some processing
status = done;
return status;
}
what I did in python looks similar to this:
import ctypes
from enum import IntEnum
test = ctypes.WinDLL('Project.dll')
if (test == 0):
print( " Could not open DLL")
class CtypesEnum(IntEnum):
@classmethod
def from_param(cls, obj):
return int(obj)
class myEnum(CtypesEnum):
off = 0
on = 1
getParam = test.get_param
getParam.argtypes = [ctypes.POINTER(ctypes.c_int)]
getParam.restype= ctypes.c_uint
getParam(myEnum.on)
The error I get now is
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
getParam(myEnum.on)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_c_long instance instead of myEnum
What's the correct way of passing an enum pointer using ctypes.I couldn't find an example and I'm kinda new to python :/
[1]: https://v4.chriskrycho.com/2015/ctypes-structures-and-dll-exports.html
This was my solution for the python part. I read 1 and 0 respectively.
import ctypes
from ctypes import *
test = ctypes.WinDLL('Project.dll')
if (test == 0):
print( " Could not open DLL")
class myEnum(c_int):
off = 0
on = 1
getParam = test.get_param
getParam.argtype = myEnum()
getParam.restype= ctypes.c_int
result = test.get_param(myEnum.on)
print(result)
result = test.get_param(myEnum.off)
print(result)