473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ctypes:passing python class instances through c and back

Hi
I have C code with requires me to register a python callback. I am
able to get the callback working well using ctypes if I use global
functions without any context argument for the callback.

Now I want to register a python class member function for callback,
and give "self" as the context argument at the time of callback
registration. Then the c code can invoke the callback with self as the
context argument

class SomeClass(objec t):
def __init__(self):
self.msg = 'Hello'

def callbackMethod( context):
# self = somemagic(conte xt) <<<<<< how to convert this ptr
back to instance???
print self.msg

CALLBACK_FN_TYP E = WINFUNCTYPE(Non e,c_void_p)

s=SomeClass()
registerCallbac k(CALLBACK_FN_T YPE(s.callbackM ethod),py_objec t(self))

If I print context that comes in the callback, it is the address of
the instance 's'. Now how do I generate the class instance back from
the void* context that is passed into the callback?

Jul 26 '07 #1
1 2581
En Thu, 26 Jul 2007 12:18:04 -0300, m2i3k <m2*******@gmai l.comescribió:
I have C code with requires me to register a python callback. I am
able to get the callback working well using ctypes if I use global
functions without any context argument for the callback.

Now I want to register a python class member function for callback,
and give "self" as the context argument at the time of callback
registration. Then the c code can invoke the callback with self as the
context argument
Use a bound method as the callback -that is, get the method from an
instance, not from the class-.
Pass it the same way as you would pass a global function; you get the
"self" argument for free :)

--
Gabriel Genellina

Jul 30 '07 #2

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

Similar topics

2
1898
by: Somesh Bartakkay | last post by:
I downloaded and tried a small dialog-dll's application with venster+ctypes+python 2.3 its working very well, but is it reliable to use in big projects ? or part of big projects ? wot are limitations in use ? wot are advantages/disadvantages of this combination over win32all package of Mark Hammond ? plz guide me before i starting using it ../ thanX in +vance !
13
3861
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
2
1878
by: gerard5609 | last post by:
Hello, how to get at the function address of a WinFunctionType ctypes object ? With ctypes 1.0, I used just myfunc._as_parameter_ and all was well. With ctypes 1.0.1, that ships with python 2.5, WinFunctionType has no longer an _as_parameter_ attribute Where in the ChangeLog and the documentation is explained how to deal with this change ?
1
1476
by: thianseng | last post by:
Hi All, Urgent SOS!!! I developed my project using pyton 2.5 under winXP and had an issue in loading my dll if the target user is using Win2000. Initially, I thought it was caused by the dlls I created were not compatible with Win2k. Then, I wrote a simple MFC program to load the dlls. It works fine for MFC but not for ctypes(python 2.5). Can someone please help? Thanks...... Best regards, Vincent....
0
3220
by: malen | last post by:
Hi all! I'm building mouse movement filter program for Windows and Mac OS X. In Windows I use ctypes.windll.user32.getCursorPos(pointer) and ctypes.windll.user32.setCursorPos(x,y) to get and set the cursor position but I do not know what the library and functions are called in Mac OS X (I'm a complete noob when it comes to Mac :) ) To explain better (I hope) here is the windows code for a test program that forces the mouse to use a...
27
1743
by: Licheng Fang | last post by:
Python is supposed to be readable, but after programming in Python for a while I find my Python programs can be more obfuscated than their C/C ++ counterparts sometimes. Part of the reason is that with heterogeneous lists/tuples at hand, I tend to stuff many things into the list and *assume* a structure of the list or tuple, instead of declaring them explicitly as one will do with C structs. So, what used to be struct nameval { char *...
6
12933
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how I should declare the return type of the functions and read the fields. Any hint is appreciated. typedef struct { char *country_short; char *country_long;
4
8511
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value) err = fcntl.ioctl(eos_fd, request, args) Now to do the same with ctypes, I have one problem.
3
11667
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks, -a
0
9480
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
10313
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...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9944
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
8968
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
7494
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
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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...
3
2875
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.