473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use C enum in Python CTypes?

I am using Python's CTypes module to import functions from a DLL into
Python. Some of the DLL's functions take enum type arguments, eg:

olDaGetDASS (HDEV hDev, OLSS OlSs, UINT uiElement, PHDASS phDass);

//(this function is in C form)
>From the header file, I found that type OLSS is an enum, like this:

typedef enum olss_tag
{
OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT
}
OLSS;

I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
but I am not sure how translate a C enum into Python...

This site
http://python.net/crew/theller/ctype...emented-things

says that enumeration types is not implemented,
"Enumeration types are not implemented. You can do it easily yourself,
using c_int as the base class."

How do I do this? I am fairly new to Python, coming in from C++ (of
which I am utterly sick!)

Aug 7 '07 #1
4 18123
On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
typedef enum olss_tag
{
OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT
}
OLSS;

I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
but I am not sure how translate a C enum into Python...

This site
http://python.net/crew/theller/ctype...emented-things

says that enumeration types is not implemented,
"Enumeration types are not implemented. You can do it easily yourself,
using c_int as the base class."
I would just define constants:

(OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT) = map(ctypes.c_int, xrange(6))

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '07 #2
On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
typedef enum olss_tag
{
OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT
}
OLSS;
I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
but I am not sure how translate a C enum into Python...
This site
http://python.net/crew/theller/ctype...s-todo-and-non...
says that enumeration types is not implemented,
"Enumeration types are not implemented. You can do it easily yourself,
using c_int as the base class."

I would just define constants:

(OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT) = map(ctypes.c_int, xrange(6))

Ciao,
Marc 'BlackJack' Rintsch- Hide quoted text -

- Show quoted text -

Wouldn't that assign integer values 0 to 5 to the things? I don't know
if it'll give me the correct results.

Aug 7 '07 #3
On Tue, 07 Aug 2007 02:13:38 -0700, rozniy wrote:
On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
This site
http://python.net/crew/theller/ctype...s-todo-and-non...
says that enumeration types is not implemented,
"Enumeration types are not implemented. You can do it easily yourself,
using c_int as the base class."

I would just define constants:

(OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT) = map(ctypes.c_int, xrange(6))

Wouldn't that assign integer values 0 to 5 to the things?
Yes.
I don't know if it'll give me the correct results.
It should. Enumerations in C are more or less just integer constants.

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '07 #4
"rozniy" <ro****@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
typedef enum olss_tag
{
OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT
}
OLSS;
I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
but I am not sure how translate a C enum into Python...
This site
http://python.net/crew/theller/ctype...s-todo-and-non...
says that enumeration types is not implemented,
"Enumeration types are not implemented. You can do it easily yourself,
using c_int as the base class."

I would just define constants:

(OLSS_AD,
OLSS_DA,
OLSS_DIN,
OLSS_DOUT,
OLSS_SRL,
OLSS_CT) = map(ctypes.c_int, xrange(6))

Ciao,
Marc 'BlackJack' Rintsch- Hide quoted text -

- Show quoted text -


Wouldn't that assign integer values 0 to 5 to the things? I don't know
if it'll give me the correct results.
Yes, that's how C's and C++'s enums work, unless an override is given (which
I know you can do in C++ for sure). Otherwise, they are just numbers
starting at 0. The size of the intergers (byte, 2 bytes, 4 bytes, etc..) is
implemenation defined I believe, but an int normally works.
Aug 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Srijit Kumar Bhadra | last post by:
Hello, Here are code snippets to create and access shared memory in Python with and without ctypes module. With regards, Srijit Filename : SharedMemCreate.py import msvcrt, mmap
18
by: Paul Watson | last post by:
I need to call some Windows APIs. Is the only way to download ctypes or the win32 interfaces? Is there any plan to get ctypes batteries into the standard Python build?
1
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss...
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
0
by: Tim Grove | last post by:
Thanks for your advice Gerdus, but I have tried your suggestion with no success. It has at least been beneficial to discover a tool which I did not know about in 'Dependency Walker'; all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.