473,699 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
"Enumeratio n 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 18264
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,
"Enumeratio n 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_in t, xrange(6))

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '07 #2
On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
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,
"Enumeratio n 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_in t, 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.net wrote:
>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,
"Enumeratio n 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_in t, 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.c omwrote in message
news:11******** **************@ e9g2000prf.goog legroups.com...
On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
>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,
"Enumeratio n 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_in t, 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
7652
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
2409
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
1918
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 something? Cya, Gerald -----Ursprüngliche Nachricht----- Von: python-list-bounces+gerald.klix=klix.ch@python.org Im Auftrag von
0
2271
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 say this will work for anyone else, but it worked for me... Using: http://ftp.mozilla.org/pub/mozilla.org/js/js-1.5.tar.gz First up:
12
10116
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, ctypes.c_void_p).value != None) Yes?
0
1437
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 dependencies appear okay. HOWEVER (!!!) I HAVE JUST HAD SUCCESS IN LOADING THE DLL !!! In searching through my 'C:WINDOWS\system32' directory I discovered a subdirectory named 'rsvg' containing a number of .dll's associated with that library, including...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8940
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7781
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.