Wafeeq
Wafeeq

Reputation: 889

How to pass enum as argument in ctypes python?

I have a dll and a c header file .h. There are some functions which accepts c enum, for example;

enum Type
{
    typeA,
    typeB
};

and a function

bool printType(Type type );

Now I want to use this function of dll from a python script. I have imported the library. It is successful. I can access other functions and it works. So there is one funciton like I gave example above which takes an Enum as parameter.

What I have tried after looking into other posts here and other stuff on internet;

# -*- coding: utf-8 -*-
from ctypes import *
from enum import IntEnum

class CtypesEnum(IntEnum):
    @classmethod
    def from_param(cls, obj):
        return int(obj)

class Type(CtypesEnum):
    typeA     = 0
    typeB     = 1

I have used it like this.

setEnumtype = self.printType
setEnumtype.argtypes = [Type]
setEnumVale.restype = c.c_int
result = self.printType(setEnumtype.typeA)

Interpreter says, it does know IntEnum. I have installed enum by pip.

Is there another and straightforward way to do this?

Traceback:

from enum import IntEnum
ImportError: cannot Import Name IntEnum

Upvotes: 1

Views: 2438

Answers (1)

Wayne Werner
Wayne Werner

Reputation: 51817

If you read the enum docs on pip you'll see:

Python 3 now has in its standard library an enum implementation (also available for older Python versions as the third-party flufl.enum distribution) that supersedes this library.

You actually want enum34.

Upvotes: 1

Related Questions